KatInterview

Most Frequently Booked Vehicle

Arrays & HashingAuto-graded · 16 test cases
EasyAuto-gradedArrays & Hashinghash-mapcountingtie-breaking

Last 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_id with the most bookings (smallest ID on ties).

Only vehicle IDs that actually appear in bookings can be returned.

Constraints

  • 1 <= len(bookings) <= 10^5
  • 0 <= pickup < dropoff <= 10^9
  • 1 <= 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.