Interview Query

Minimum Parking Spots

Start Timer

0:00:00

Upvote
3
Downvote
Save question
Mark as completed
View comments (3)
Next question

Given a list of tuples, each containing two integers representing the arrival and departure time of busses, write a function minimum_parking_spots that computes the minimum number of parking spots needed to accommodate all the busses.

The arrival and departure times are given in hours, ranging from 0 (12:00 AM) to 24 (11:59 PM). Each tuple follows the format (arrival_time, departure_time).

You may assume that:

  1. The arrival and departure times are integers.
  2. A bus only leaves on the next hour after its arrival.

Note: A bus can only use a parking spot strictly after it is emptied. This means that a bus leaving at 4 and a bus arriving at 4 require at least two parking spaces.

Example 1:

Input:

busses = [(1, 2), (3, 4), (5, 6)]

Output:

def minimum_parking_spots(busses) -> 1

Explanation: The first bus arrives at 1 and departs at 2, the second bus arrives at 3 and departs at 4, and the third bus arrives at 5 and departs at 6. At most, there is only one bus at the station at any given time.

Example 2:

Input:

busses = [(1, 5), (2, 3), (4, 5)]

Output:

def minimum_parking_spots(busses) -> 2

Explanation: The first bus arrives at 1 and departs at 5, the second bus arrives at 2 and departs at 3, and the third bus arrives at 4 and departs at 5. At the fourth hour, there are two buses at the station.

.
.
.
.
.


Comments

Loading comments..