Garage occupancy at every timestamp
I was asked this
No reports yet. Sign in to say where you were asked it.
A parking garage records occupancy over n discrete timestamps, numbered 0 to n - 1. You get a list of parking tickets. Ticket [entry, exit] means one car arrived at timestamp entry and left at timestamp exit.
A car counts as present at every timestamp t with entry <= t <= exit. It still counts at its exit timestamp and stops counting at exit + 1.
Implement:
def garage_occupancy(n: int, tickets: list[list[int]]) -> list[int]:
...
Return a list of length n where position t is the number of cars in the garage at timestamp t.
Constraints
-
0 <= n <= 10^5,0 <= len(tickets) <= 10^5 0 <= entry <= exit <= n - 1- Several cars can share the same entry or exit timestamp.
Example 1
n = 5
tickets = [[0, 2], [1, 1], [2, 4]]
output = [1, 2, 2, 1, 1]
At timestamp 2, the first car is leaving and the third car is arriving. Both count, so the total is 2.
Example 2
n = 4
tickets = [[3, 3]]
output = [0, 0, 0, 1]
Aim for better than O(n * len(tickets)) time.
Reconstructed from the reported question; details may differ from what interviewers ask.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.
