Number of Students Doing Homework at a Given Time

Easy
496.

Number of Students Doing Homework at a Given Time

Number of Students Doing Homework at a Given Time is an easy coding problem on arrays, asked in Amazon and TCS interviews.

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 <= 100
  • 1 <= startTime[i] <= endTime[i] <= 1000
  • 1 <= queryTime <= 1000
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} startTime
 * @param {number[]} endTime
 * @param {number} queryTime
 * @return {number}
 */
var busyStudent = function(startTime, endTime, queryTime) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

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 <= 100
  • 1 <= startTime[i] <= endTime[i] <= 1000
  • 1 <= queryTime <= 1000

Hints for Number of Students Doing Homework at a Given Time

  1. Hint 1

    The window is closed on both sides — the endpoints count.

  2. Hint 2

    One pass with a counter is enough at these limits.

  3. 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

  1. Walk the two arrays in lockstep.
  2. Increment a counter whenever startTime[i] <= queryTime <= endTime[i].
  3. 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 count

Java

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