Most Frequently Booked Vehicle
Arrays & HashingAuto-graded · 16 test casesLast updated
Count how many booking records each vehicle has and return the ID with the highest count, breaking ties by the smallest ID.
Most Frequently Booked Vehicle
A rental company keeps a log of bookings. Each booking is a list [pickup, dropoff, vehicle_id]. A vehicle is considered occupied during the half-open window [pickup, dropoff), so a vehicle returned at the exact moment another booking starts is free to take it.
Find the vehicle that appears in the largest number of bookings and return its vehicle_id. If several vehicles share the highest number of bookings, return the smallest vehicle_id among them.
Every entry in the log counts as exactly one booking of its vehicle.
Function
def most_booked_vehicle(bookings: List[List[int]]) -> int:
bookings: a list of[pickup, dropoff, vehicle_id]entries.- Returns: a single integer, the
vehicle_idwith the most bookings (smallest ID on ties).
Only vehicle IDs that actually appear in bookings can be returned.
Constraints
1 <= len(bookings) <= 10^50 <= pickup < dropoff <= 10^91 <= vehicle_id <= 1000
Examples
Example 1
Input: most_booked_vehicle([[1, 5, 10], [2, 6, 20], [5, 10, 10]])
Output: 10
Example 2
Input: most_booked_vehicle([[1, 2, 1], [2, 3, 2], [3, 4, 1]])
Output: 1
Hints
Hint 1
Think about which parts of each booking actually influence the answer. Do the times matter for counting bookings per vehicle?
Hint 2
Use a dictionary (or Counter) from vehicle_id to the number of bookings in a single pass.
Hint 3
To pick the winner, compare by highest count first and smallest ID second; a composite key such as (-count, vehicle_id) with min handles both at once.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The pickup and dropoff times, and the rule about back-to-back bookings, do not change the answer. The question only asks which vehicle has the most booking records, and each record counts once regardless of overlap. So the task is a frequency count with a tie-break.
- Walk through the log once, and increment a counter for each
vehicle_id. - Select the vehicle with the largest count. On equal counts, select the smaller ID. Using
minwith the key(-count, vehicle_id)does this: a larger count gives a smaller negative number, and ties fall through to comparing IDs.
Why it is correct
- A vehicle's total is exactly the number of records carrying its ID, which is what the counter computes.
- Only IDs present in the dictionary are candidates, so the function never returns a vehicle that was not booked. Since the log has at least one entry, the dictionary is non-empty and
minis safe. - Tuple comparison is lexicographic, so the key enforces "highest count first, then smallest ID".
Complexity
- Time: O(n) for counting plus O(k) for the selection, where k <= min(n, 1000) is the number of distinct vehicles.
- Space: O(k) for the dictionary.
Reference solution
from typing import List
def most_booked_vehicle(bookings: List[List[int]]) -> int:
counts = {}
for _, _, vehicle_id in bookings:
counts[vehicle_id] = counts.get(vehicle_id, 0) + 1
return min(counts, key=lambda v: (-counts[v], v))