Simulate a Multi-Lane Toll Plaza Queue
Stacks & QueuesAuto-graded · 16 test casesLast 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_timeis always ≥ 1.- A lane's "next available" tick is
max(current_time, lane_free_at)wherecurrent_timeis the vehicle'sarrival_tick. That is, if a lane is idle when the vehicle arrives, service starts immediately atarrival_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 ≤ 500 ≤ len(vehicles) ≤ 20000 ≤ arrival_tick ≤ 10^61 ≤ service_time ≤ 10^4vehiclesis sorted in non-decreasing order byarrival_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.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
We need to track two things per lane at the moment a new vehicle arrives:
- Queue depth — how many vehicles are still waiting or being served in that lane.
- Free-at tick — the earliest tick at which the lane can start serving the next vehicle.
Both pieces of information can be maintained with a per-lane deque of scheduled departure ticks.
Algorithm
Data structures:
lane_free[i]: the tick at which laneifinishes its last queued vehicle (starts at 0).lane_depth[i]: current number of vehicles in lanei(waiting + being served).lane_departures[i]: a deque of future departure ticks for lanei, in order.
For each arriving vehicle at arrival_tick:
- Prune stale departures: For every lane, pop departure ticks from the front of its deque that are
≤ arrival_tick(those vehicles have already left), decrementinglane_depthaccordingly. - Choose lane:
best_lane = argmin (lane_depth[i], i)— minimum depth, lowest index for ties. - Compute departure:
start = max(arrival_tick, lane_free[best_lane]),departure = start + service_time. - Update state: set
lane_free[best_lane] = departure, incrementlane_depth[best_lane], appenddeparturetolane_departures[best_lane]. - Record
departurein the result list.
Why It Is Correct
- We always assign to the lane with the shortest current queue, breaking ties by index — exactly as specified.
- The departure formula
max(arrival_tick, lane_free) + service_timecorrectly handles both the case where the vehicle waits behind others and the case where the lane is idle. - Pruning at arrival time keeps
lane_depthaccurate without requiring us to simulate every tick.
Complexity
- Time: O(n log n) in the worst case per assignment due to pruning — but each departure tick is inserted and removed from its deque exactly once, so total prune work across all vehicles is O(n). Overall: O(n · k) for the
minover lanes each step, which is O(n · k) total. With k ≤ 50 and n ≤ 2000 this is well within limits. - Space: O(n + k) for the deques and auxiliary arrays.
from typing import List
from collections import deque
def simulate_toll_plaza(vehicles: list, k: int) -> list:
if not vehicles:
return []
lane_free = [0] * k
lane_depth = [0] * k
lane_departures: List[deque] = [deque() for _ in range(k)]
results = []
for arrival_tick, service_time in vehicles:
# Prune vehicles that have already departed
for i in range(k):
while lane_departures[i] and lane_departures[i][0] <= arrival_tick:
lane_departures[i].popleft()
lane_depth[i] -= 1
# Choose the best lane
best_lane = min(range(k), key=lambda i: (lane_depth[i], i))
# Compute departure tick
start_tick = max(arrival_tick, lane_free[best_lane])
departure_tick = start_tick + service_time
# Update lane state
lane_free[best_lane] = departure_tick
lane_depth[best_lane] += 1
lane_departures[best_lane].append(departure_tick)
results.append(departure_tick)
return results