Assign the Minimum Fleet to Rental Reservations
GoogleSoftware Engineer · OnsiteLast updated
Determine the minimum fleet size and a deterministic vehicle assignment for a list of rental reservations using interval scheduling and priority queues.
Problem Statement
You are managing a car rental fleet. You are given a list of reservations, where reservations[i] = [pickup_i, dropoff_i] denotes the pickup and dropoff times of the i-th reservation.
A vehicle assigned to reservation i is occupied in the half-open interval [pickup_i, dropoff_i). This means the vehicle is returned and becomes available for another reservation at time dropoff_i (i.e., another customer can pick it up at pickup_j if pickup_j >= dropoff_i).
Your goal is to assign each reservation to a vehicle such that:
- No two reservations assigned to the same vehicle overlap in time.
- The total number of vehicles used is minimized.
- Assignments are deterministic: reservations are processed in ascending order of their
pickuptime. If multiple reservations have the same pickup time, process them by their original reservation indexiin ascending order. - When multiple vehicles are idle and available at the time of pickup, always assign the vehicle with the smallest vehicle ID (vehicles are numbered
1, 2, 3, ...). If no idle vehicle is available, assign the next smallest unused vehicle ID.
Return a tuple (fleet_size, assignments), where fleet_size is an integer representing the minimum number of vehicles needed, and assignments is a list of integers where assignments[i] is the vehicle ID assigned to reservation i.
Function Signature
def assign_fleet(reservations: list[list[int]]) -> tuple[int, list[int]]:
pass
Constraints
0 <= len(reservations) <= 10^50 <= pickup_i < dropoff_i <= 10^9- All inputs are integers.
Examples
Example 1
Input: reservations = [[1, 4], [2, 5], [4, 7], [3, 6]]
Output: (3, [1, 2, 1, 3])
Explanation:
- Reservation 0 [1, 4]: Assigned to Vehicle 1.
- Reservation 1 [2, 5]: Vehicle 1 is busy; assigned to Vehicle 2.
- Reservation 3 [3, 6]: Vehicles 1 and 2 are busy; assigned to Vehicle 3.
- Reservation 2 [4, 7]: At t = 4, Vehicle 1 is returned and becomes available. Assigned to Vehicle 1.
Fleet size is 3, assignments are [1, 2, 1, 3].
Example 2
Input: reservations = [[1, 3], [3, 5]]
Output: (1, [1, 1])
Explanation:
Vehicle 1 is returned at time 3 and can immediately be reused at time 3.
Edge Cases
reservationsis empty: return(0, []).- Simultaneous start and end times: ensure vehicles returned at time
tare freed before any new vehicle is assigned at timet.
Hints
Hint 1
Sort the reservations primarily by pickup time and secondarily by original reservation index, but remember to preserve original indices to output the final assignment array.
Hint 2
Use two heaps: a min-heap of active vehicles sorted by dropoff time to know when cars become free, and a min-heap of available vehicle IDs to always pick the smallest ID.
Hint 3
Before assigning a vehicle for the current reservation at pickup time `t`, pop all vehicles from the active heap whose dropoff time is `<= t` and push their IDs into the available vehicles heap.
Follow-up questions
- How would you adjust your solution if each vehicle requires a turnaround cleaning time of `C` minutes between dropoff and pickup?
- If different reservations require different car classes (e.g., Sedan, SUV) and higher classes can satisfy lower class requests, how would you model the assignment?
- How can you handle an online streaming version where reservations arrive dynamically in real time rather than all upfront?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.