You are given rental requests, where each request is represented as [pickupTime, returnTime].
A single car cannot serve overlapping requests. If one request ends exactly when another begins, the same car may serve both.
Return one deterministic minimum-car assignment using the following canonical rules:
- Process requests in ascending order of
pickupTime, breaking ties byreturnTime. - When multiple cars are available, reuse the available car with the smallest car id.
- When no existing car is available, create a new car with the next unused id starting from
0.
Each returned string must use the format "carId: (p1,r1) (p2,r2) ...". The number of returned lines is the minimum number of cars needed.
Complete the function assignMinimumCars in the editor below.
assignMinimumCars has the following parameter:
int[][] requests: rental intervals[pickupTime, returnTime]
Returns
String[]: the deterministic assignment lines in ascending car id order.
requests = [[1, 3], [2, 4], [4, 6]] return = ["0: (1,3) (4,6)", "1: (2,4)"]
Two cars are necessary because the first two requests overlap. Car 0 becomes available at time 3 and can serve the request starting at time 4.
requests = [[1, 2], [2, 3], [3, 4], [4, 5]] return = ["0: (1,2) (2,3) (3,4) (4,5)"]
Each request starts exactly when the previous one ends, so one car can serve all of them.
1 <= requests.length <= 2 * 10^50 <= pickupTime < returnTime <= 10^9- The returned assignment must follow the deterministic rules stated above.
- Fountain SafetyONSITE INTERVIEW · Seen Jun 2026
- Consolidate On-Call RotationsOA · Seen Jun 2026
- Detonate Bombs with Chain ReactionsONSITE INTERVIEW · Seen May 2026
- Evaluate a Nested Math ExpressionONSITE INTERVIEW · Seen May 2026
- Tic-Tac-Toe Game StatusPHONE SCREEN · Seen May 2026
- Longest Dictionary TokenizationPHONE SCREEN · Seen May 2026
- Shortest Path with Mandatory WaypointONSITE INTERVIEW · Seen Apr 2026
- Count Divisible Coin SelectionsOA · Seen Dec 2025
public String[] assignMinimumCars(int[][] requests) {
// write your code here
}