Skip to content

Practice Number of Students Doing Homework at a Given Time

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#1450
LeetCode Problem

Number of Students Doing Homework at a Given Time

Problem Statement

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.

Source: Number of Students Doing Homework at a Given Time on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
startTime = [1,2,3], endTime = [3,2,7], queryTime = 4
Output
1
Explanation

Pair start and end values by student index, giving intervals [1, 3], [2, 2] and [3, 7]. Only the third interval contains time 4, so one student is doing homework. Check startTime[i] <= queryTime <= endTime[i] for each student. Sorting start and end arrays independently before pairing them would lose which endpoint belongs to which student.

# Example 2

Input
startTime = [4], endTime = [4], queryTime = 4
Output
1
Explanation

The only student has interval [4, 4]. Both endpoints are included, so time 4 belongs to that interval and the count is one. A zero-length interval is still a valid match at its single time. Using strict inequalities at either endpoint would incorrectly exclude this student.

Constraints

startTime.length == endTime.length
1 <= startTime.length <= 100
1 <= startTime[i] <= endTime[i] <= 1000
1 <= queryTime <= 1000

How to solve Number of Students Doing Homework at a Given Time

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

Each student is an independent closed interval, so the answer is simply how many intervals contain queryTime; one inclusive comparison per student settles it with no sorting and no auxiliary structure.

Why brute force is not enough

A tempting over-engineered route is to build a timeline of all 1000 time units, mark every unit each student covers, and read the count at queryTime; that costs O(n times 1000) work for a single query and gains nothing. With startTime.length at most 100 the direct scan is already optimal, so the real risk in this problem is correctness at the boundaries rather than speed.

Approach

  1. Pair the two arrays by index: student i owns the interval from startTime[i] to endTime[i], and the arrays must never be sorted or reordered independently because that destroys the pairing.
  2. Walk the indices once while keeping a counter of matching students.
  3. For each student, test whether queryTime is at least the start and at most the end; both comparisons are inclusive because the problem states the interval is closed.
  4. Increment the counter on a match and return it after the scan; if no student matches, the counter is simply zero.

Walkthrough of Example 1

startTime = [1,2,3], endTime = [3,2,7], queryTime = 4 → 1

  1. Student 0 has interval [1, 3]; queryTime 4 exceeds 3, so no match.
  2. Student 1 has interval [2, 2]; 4 lies outside it, so no match.
  3. Student 2 has interval [3, 7]; 3 <= 4 <= 7 holds, so the counter becomes 1.
  4. The scan ends and the answer is 1.

Time and space

Time O(n) · Space O(1). n is the number of students; each is examined once with constant work, and only a counter is kept.

Interview pitfalls

  • Using strict inequalities at either end, which drops a student whose homework starts or ends exactly at queryTime; example 2 is the zero-length interval [4, 4] and must count.
  • Sorting startTime and endTime separately before pairing; the i-th smallest start does not belong with the i-th smallest end.
  • Returning early on the first match instead of counting all of them, or returning -1 when nobody matches rather than 0.
  • Assuming intervals never overlap because each student has a single interval; they can overlap freely, and each overlapping student counts separately.

Follow-up questions interviewers ask

  • If there are many queryTime values against the same students, how would you preprocess with a difference array and prefix sums to answer each query in O(1)?
  • How would you find the time at which the most students are working simultaneously using a sweep over sorted start and end events?
  • If intervals arrive as a stream and queries interleave with insertions, what structure keeps both operations logarithmic?

Related LeetCode Problems

Practice more problems similar to Number of Students Doing Homework at a Given Time.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.