Minimum Cars for Rental Requests
GoogleSoftware Engineer · OnsiteLast updated
Determine the minimum number of rental cars required to service a given list of booking requests with half-open time intervals.
Problem Statement
You are managing a car rental platform. You receive a collection of rental requests. Each request has a unique identifier, a pickup_time, and a return_time.
A single car can serve multiple requests as long as their rental periods do not overlap. If a car is returned at time t, it is immediately cleaned and available for another customer to pick up at time t (i.e., the interval is half-open: [pickup_time, return_time)).
Write a function that calculates the minimum number of cars required to satisfy all rental requests.
Function Signature
from typing import NamedTuple
class RentalRequest(NamedTuple):
request_id: str
pickup_time: int
return_time: int
def min_cars_needed(requests: list[RentalRequest]) -> int:
pass
Input & Output Format
- Input: A list of
RentalRequestobjects, each containing:request_id: A string representing the unique request ID.pickup_time: An integer representing the pickup timestamp.return_time: An integer representing the return timestamp (pickup_time < return_time).
- Output: An integer representing the minimum number of cars required.
Examples
Example 1
requests = [
RentalRequest("req_1", 1, 5),
RentalRequest("req_2", 5, 10),
RentalRequest("req_3", 2, 6)
]
# Output: 2
Explanation: Car A handles req_1 from time 1 to 5, and then immediately serves req_2 starting at time 5. req_3 overlaps with req_1 between times 2 and 5, so Car B is required. Total cars needed = 2.
Example 2
requests = [
RentalRequest("req_A", 10, 20),
RentalRequest("req_B", 12, 18),
RentalRequest("req_C", 15, 25),
RentalRequest("req_D", 20, 30)
]
# Output: 3
Explanation: At time 15, req_A, req_B, and req_C are all active concurrently, requiring 3 cars. At time 20, req_A ends as req_D begins, allowing the same car to be reused. The peak concurrency is 3.
Constraints
0 <= len(requests) <= 100,0000 <= pickup_time < return_time <= 10^9- All
request_idvalues are unique.
Edge Cases to Consider
- Empty input:
requestsis empty; return0. - Simultaneous boundary times: Multiple drop-offs and pickups occurring at the exact same timestamp (returns must be processed before pickups at that same timestamp).
- Identical intervals: Multiple requests with the exact same
pickup_timeandreturn_time.
Hints
Hint 1
Model this as an interval overlap problem where you want to find the maximum number of overlapping intervals at any point in time.
Hint 2
If using a sweep-line approach with events, ensure that return events are processed before pickup events when they share the same timestamp.
Hint 3
Alternatively, sort requests by pickup time and use a min-heap to track the earliest available car return times.
Follow-up questions
- How would you modify your solution to return the actual assignment mapping each car ID (1 to K) to its list of request IDs?
- Suppose each vehicle requires a turnaround/cleaning window of C minutes before it can be picked up again. How does this change the logic?
- If the fleet is capped at M cars and cannot fulfill all requests, how would you select a subset of requests to maximize total rental duration?
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.