KatInterview

Simulate a Multi-Lane Toll Plaza Queue

Stacks & QueuesAuto-graded · 16 test cases
HardAuto-gradedStacks & Queuesqueuesimulationgreedy

Last updated

Tests queue simulation skills by modeling vehicles routing through toll lanes with dynamic overflow and lane-closing logic.

Simulate a Multi-Lane Toll Plaza Queue

A toll plaza has k lanes, numbered 0 through k-1. Vehicles arrive one at a time and are processed according to the following rules:

Lane Assignment

When a vehicle arrives, it joins the lane with the fewest vehicles currently waiting (including the one being served). Ties are broken by choosing the lowest-numbered lane.

Service

Each lane serves vehicles one at a time. A vehicle arriving at tick t cannot be served until tick t (it joins first, then the lane may serve). Each vehicle has a service time (number of ticks it occupies the front of its lane). A vehicle finishes service at the tick it entered service plus its service time (exclusive end). The lane then immediately begins serving the next vehicle in the queue (if any).

Lane Closing

After all arrivals have been assigned, any lane that is empty (no vehicles waiting or being served) is immediately closed and cannot receive future vehicles. Lanes close only after all arrivals have been processed, so this does not affect assignment.

Your Task

Given a list of (arrival_tick, service_time) pairs (already sorted by arrival_tick, ties in arrival order by index) and the number of lanes k, simulate the plaza and return a list of departure ticks — one per vehicle, in the same order as the input.

The departure tick of a vehicle is the tick at which it finishes being served (i.e., start-of-service tick + service time).

Details & Edge Cases

  • Vehicles in the input are given in non-decreasing order of arrival_tick.
  • service_time is always ≥ 1.
  • A lane's "next available" tick is max(current_time, lane_free_at) where current_time is the vehicle's arrival_tick. That is, if a lane is idle when the vehicle arrives, service starts immediately at arrival_tick.
  • Multiple vehicles can arrive at the same tick; they are assigned in input order (index 0 first), each seeing the updated state after prior assignments.
  • If k = 1, all vehicles queue in the single lane.
  • If the input list is empty, return an empty list.

Constraints

  • 1 ≤ k ≤ 50
  • 0 ≤ len(vehicles) ≤ 2000
  • 0 ≤ arrival_tick ≤ 10^6
  • 1 ≤ service_time ≤ 10^4
  • vehicles is sorted in non-decreasing order by arrival_tick

Examples

Example 1

Input: simulate_toll_plaza([[0, 3], [0, 2], [1, 4], [5, 1]], 2)

Output: [3, 2, 7, 6]

Example 2

Input: simulate_toll_plaza([[0, 5], [0, 5], [0, 5]], 1)

Output: [5, 10, 15]

Example 3

Input: simulate_toll_plaza([], 3)

Output: []

Hints

Hint 1

Track two things per lane: how many vehicles are currently queued/being-served, and the tick at which the lane becomes free. When a vehicle arrives, pick the lane with the smallest queue length (break ties by lane index).

Hint 2

The departure tick for a new vehicle is: lane_free_at = max(arrival_tick, lane_free_at) + service_time. Update the lane's queue count by +1 on arrival; you will need to track each vehicle's departure tick so you can reconstruct per-lane occupancy at any point — or simply maintain a running count.

Hint 3

Maintain a per-lane list of scheduled departure ticks. When assigning a new vehicle, the lane's current queue depth is simply len(departures_in_lane). The lane's free-at time is the last departure tick in its list (or 0 if empty). This makes both pieces of info O(1) to look up.