FastPrepRoom Occupancy at Query Times

Room Occupancy at Query Times

WhatNot logoWhatNot● MediumINTERNONSITE INTERVIEW
Learn

Problem statement

You are given room visits as pairs [entry, exit] and an array queryTimes. Calculate how many visits are in progress at each query time.

For this exercise, each row represents a separate person's visit, and the person is inside exactly when entry <= time < exit. Identical intervals count separately. Return the counts in the original order of queryTimes.

Function

countRoomOccupancy(visits: int[][], queryTimes: int[]) → int[]

Examples

Example 1

visits = [[1,5],[3,7],[5,8]]queryTimes = [0,3,5,7,8]return = [0,2,2,1,0]

At time 5, the first visit has ended and the third has begun. At time 8, every visit has ended.

Example 2

visits = []queryTimes = [4,4]return = [0,0]

An empty visit list leaves the room empty at every query.

Example 3

visits = [[2,4],[2,4]]queryTimes = [4,2,3]return = [0,2,2]

The two equal intervals describe separate visits. Answers preserve query order.

Constraints

  • For this exercise, assume 0 <= visits.length <= 100 and 0 <= queryTimes.length <= 500.
  • Every visit has exactly two integers with 0 <= entry < exit <= 10^9.
  • Every query time is between 0 and 10^9. Inputs need not be sorted.

More WhatNot problems

See WhatNot hiring insights
public int[] countRoomOccupancy(int[][] visits, int[] queryTimes) {
    // write your code here
}
visits[[1,5],[3,7],[5,8]]
queryTimes[0,3,5,7,8]
expected[0,2,2,1,0]
Checking account…