Number of Students Doing Homework at a Given Time — Solution
Number of Students Doing Homework at a Given Time solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.
Number of Students Doing Homework at a Given Time is an easy coding problem on arrays, asked in Amazon and TCS interviews.
- Difficulty: Easy
- Topics: Arrays
- Asked at: Amazon, TCS
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
The i-th student starts homework at startTime[i] and finishes at endTime[i] — inclusive at both ends.
Return how many students are doing homework at time queryTime.
Example 1
Input: startTime = [1,2,3], endTime = [3,2,7], queryTime = 4
Output: 1
Explanation: Only the third student, whose window is [3,7], is busy at time 4.
Example 2
Input: startTime = [4], endTime = [4], queryTime = 4
Output: 1
Explanation: The window is a single instant and it contains the query.
Example 3
Input: startTime = [1,1,1,1], endTime = [1,3,2,4], queryTime = 7
Output: 0
Constraints
1 <= startTime.length == endTime.length <= 1001 <= startTime[i] <= endTime[i] <= 10001 <= queryTime <= 1000
Hints for Number of Students Doing Homework at a Given Time
Hint 1
The window is closed on both sides — the endpoints count.
Hint 2
One pass with a counter is enough at these limits.
Hint 3
The test is simply
startTime[i] <= queryTime && queryTime <= endTime[i].
How to solve Number of Students Doing Homework at a Given Time
Each student is an independent closed interval; the answer is how many of them contain the query instant.
Approach
- Walk the two arrays in lockstep.
- Increment a counter whenever
startTime[i] <= queryTime <= endTime[i]. - Return the counter.
Why it works
The intervals do not interact, so the count is a plain sum of independent membership tests.
Complexity
- Time —
O(n) - Space —
O(1)
Pitfalls
- Using strict inequalities excludes students who start or finish exactly at
queryTime. - A difference array is overkill here but is the right tool if many queries were asked instead of one.
Reference solution
Python
from typing import List
def busyStudent(startTime: List[int], endTime: List[int], queryTime: int) -> int:
count = 0
for s, e in zip(startTime, endTime):
if s <= queryTime <= e:
count += 1
return countJava
public static int busyStudent(int[] startTime, int[] endTime, int queryTime) {
int count = 0;
for (int i = 0; i < startTime.length; i++) {
if (startTime[i] <= queryTime && queryTime <= endTime[i]) count++;
}
return count;
}C++
int busyStudent(vector<int>& startTime, vector<int>& endTime, int queryTime) {
int count = 0;
for (int i = 0; i < (int) startTime.size(); i++) {
if (startTime[i] <= queryTime && queryTime <= endTime[i]) count++;
}
return count;
}JavaScript
var busyStudent = function(startTime, endTime, queryTime) {
let count = 0;
for (let i = 0; i < startTime.length; i++) {
if (startTime[i] <= queryTime && queryTime <= endTime[i]) count++;
}
return count;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays