Watch 10 video solutions for Number of Students Doing Homework at a Given Time, a easy level problem involving Array. This walkthrough by CodeJulian has 497 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given two integer arrays startTime and endTime and given an integer queryTime.
The ith student started doing their homework at the time startTime[i] and finished it at time endTime[i].
Return the number of students doing their homework at time queryTime. More formally, return the number of students where queryTime lays in the interval [startTime[i], endTime[i]] inclusive.
Example 1:
Input: startTime = [1,2,3], endTime = [3,2,7], queryTime = 4 Output: 1 Explanation: We have 3 students where: The first student started doing homework at time 1 and finished at time 3 and wasn't doing anything at time 4. The second student started doing homework at time 2 and finished at time 2 and also wasn't doing anything at time 4. The third student started doing homework at time 3 and finished at time 7 and was the only student doing homework at time 4.
Example 2:
Input: startTime = [4], endTime = [4], queryTime = 4 Output: 1 Explanation: The only student was doing their homework at the queryTime.
Constraints:
startTime.length == endTime.length1 <= startTime.length <= 1001 <= startTime[i] <= endTime[i] <= 10001 <= queryTime <= 1000Problem Overview: You get two arrays: startTime and endTime. Each pair represents when a student starts and finishes homework. Given a queryTime, count how many students are still working at that moment (where startTime[i] ≤ queryTime ≤ endTime[i]).
Approach 1: Brute Force Interval Check (O(n) time, O(1) space)
Iterate through every student and check whether the query time falls inside that student's homework interval. For each index i, verify the condition startTime[i] ≤ queryTime ≤ endTime[i]. If the condition holds, increment the counter. This direct scan works because the intervals are independent and you only need a simple comparison per student. The algorithm performs a single pass over the array, so the time complexity is O(n) with constant extra memory.
Approach 2: Binary Search on Sorted Times (O(n log n) preprocessing, O(log n) query)
If you need to answer multiple queries, sorting becomes useful. Store all start times and end times separately and sort them. Use binary search to count how many students started homework at or before queryTime, and how many finished before queryTime. The difference between these two counts gives the number of active students. Each query runs in O(log n) time after sorting, with O(n) extra storage for the sorted arrays. This technique treats intervals as events and leverages efficient search instead of scanning every element.
Recommended for interviews: The brute force scan is usually the expected solution because the constraints are small and the logic is clean. Interviewers want to see that you correctly interpret the inclusive interval condition. The binary search variation demonstrates deeper understanding of interval counting and sorting, especially when the problem extends to multiple queries.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Interval Check | O(n) | O(1) | Best for a single query or small input sizes. Simple and interview-friendly. |
| Binary Search on Sorted Times | O(n log n) preprocessing, O(log n) query | O(n) | Useful when handling multiple query times or when the intervals are reused repeatedly. |