algoadvance

You are given two integer arrays startTime and endTime, and an integer queryTime.

The i-th student started doing their homework at the time startTime[i] and finished it at endTime[i].

Return the number of students doing their homework at queryTime. More formally, return the number of students where queryTime lays in the interval [startTime[i], endTime[i]] inclusive.

Example

Example 1:

Input: startTime = [1,2,3], endTime = [3,2,7], queryTime = 4
Output: 1
Explanation: One student was doing their homework at the queryTime.

Example 2:

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

Constraints:

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

Clarifying Questions

  1. Are we guaranteed that the startTime and endTime lists are of the same length?
    • Yes, based on the constraints.
  2. Is the queryTime guaranteed to fall within the range from 1 to 1000?
    • Yes.

Strategy

Code

def busyStudent(startTime, endTime, queryTime):
    count = 0
    for i in range(len(startTime)):
        if startTime[i] <= queryTime <= endTime[i]:
            count += 1
    return count

Time Complexity

Test Cases

  1. Test Case 1:
    startTime = [1,2,3]
    endTime = [3,2,7]
    queryTime = 4
    Output: 1
    
  2. Test Case 2:
    startTime = [4]
    endTime = [4]
    queryTime = 4
    Output: 1
    
  3. Test Case 3:
    startTime = [1,1,1,1,1]
    endTime = [10,10,10,10,10]
    queryTime = 5
    Output: 5
    
  4. Test Case 4:
    startTime = [1,2,3,4,5]
    endTime = [5,5,5,5,5]
    queryTime = 5
    Output: 5
    
  5. Test Case 5:
    startTime = [1,2,3,4,5]
    endTime = [5,6,7,8,9]
    queryTime = 1
    Output: 1
    

We can now confidently proceed to check the implementation through these test cases and validate its correctness.

Try our interview co-pilot at AlgoAdvance.com