Four-Exit Roundabout Simulator
Model deterministic vehicle movement, entrance queues, and traffic statistics for a four-exit roundabout.
Design a small in-memory simulator for a roundabout with four entrances and exits. A user schedules vehicle arrivals, advances the simulated clock, and reads traffic statistics. First ask which real-world behaviors matter, then explain what your simplified model loses.
Sketch the object interactions and implement the central tick transition in a language of your choice or precise pseudocode.
For this exercise, assume entrances and exits are numbered 0, 1, 2, 3 clockwise. There is one circular lane represented by four segments, one beside each entrance; a segment holds at most one vehicle. Each vehicle has a unique ID, arrival tick, entrance, and a different destination exit. Trips move clockwise.
For each integer tick: (1) append that tick's arrivals to their entrance FIFO queues, using input order for ties; (2) all vehicles already on the ring move one segment clockwise simultaneously, and a vehicle whose next segment is its destination leaves instead; (3) each entrance may admit one queued vehicle into its adjacent segment if that segment is empty after movement.
Newly admitted vehicles do not move until the next tick. Tick 0 starts with an empty ring. This uniform-speed, discrete model deliberately omits lane changes, acceleration, pedestrians, and random driver behavior.
Scheduling accepts a batch atomically: reject the batch without changing state if it repeats a vehicle ID, names an entrance/exit outside 0..3, uses the same entrance and exit, or schedules an arrival at or before the current tick. Before the first tick, arrival ticks may be 0 or later. Clock advancement includes every tick through its target, cannot go backward, and repeating the current target changes nothing.
A snapshot reports scheduled arrivals still in the future, current entrance queue lengths, ring occupancy, completed vehicles, average waiting time over admitted vehicles, and average total journey time over completed vehicles.
Waiting time is admission tick minus arrival tick; journey time is exit tick minus arrival tick. Return an absent average when its denominator is zero. Snapshots must not let callers mutate the live simulation. No UI, persistence, network service, or continuous physics engine is required.