DDSA Solutions

1450. Number of Students Doing Homework at a Given Time

Time: O(n)
Space: O(1)

Problem Overview

A student is doing homework at queryTime if they started on or before that minute and have not finished yet (end time is still on or after queryTime).

Intuition

A student is doing homework at queryTime if they started on or before that minute and have not finished yet (end time is still on or after queryTime). Linear scan with two interval checks per student.

Algorithm

  1. 1Initialize count = 0.
  2. 2For each index i: if startTime[i] <= queryTime && endTime[i] >= queryTime, count++.
  3. 3Return count.

Example Walkthrough

Input: start = [1,2,3], end = [3,2,7], queryTime = 4

  1. 1.Student 0: 1≤4≤3? end 3 < 4 — no.
  2. 2.Student 2: 3≤4≤7 — yes.

Output: 1

Common Pitfalls

  • Both inequalities are inclusive on start and end.
  • O(n) scan is fine for constraints.
  • Do not confuse with overlap of intervals across students.
1450.cs
C#
// Approach: Simple range check; count students whose [startTime, endTime] interval includes queryTime.
// Time: O(n) Space: O(1)

public class Solution
{
    public int BusyStudent(int[] startTime, int[] endTime, int queryTime)
    {
        int n = startTime.Length;
        int ans = 0;

        for (int i = 0; i < n; ++i)
        {
            if (startTime[i] <= queryTime && queryTime <= endTime[i])
                ++ans;
        }

        return ans;
    }
}
Was this solution helpful?

Related Problems