KatInterview

Transit Fleet Simulator: Sampling, Seating Limits, and Fast-Lane Riders

Data Structure DesignModel answer
MediumModel answerData Structure Designsimulationheapstratified-sampling

Last updated

Build a bus network simulator: downsample riders by origin-destination pair, board and drop under a capacity cap, validate event logs, and give priority-pass riders precedence.

Transit Fleet Simulator

You are extending a small simulator for a city bus network. Each bus follows a fixed route, and riders show up at stops wanting to reach another stop.

Buses. Each bus has a unique bus_id, a shared seat limit capacity, and a timetable: an ordered list of (time, stop_id) visits.

Riders. Each rider has a unique passenger_id, an appear_time, a from_stop, a to_stop, and a boolean has_priority_pass.

Time is discrete and all times are non-negative integers. Everything fits in memory.

Scale. Up to 100,000 riders, up to 1,000 buses, and up to 1,000 timetable entries per bus.

Core visit rule. When a bus reaches (time, stop_id):

  1. Every rider on that bus whose to_stop == stop_id gets off.
  2. Riders waiting at stop_id with appear_time <= time who have never boarded any bus may get on, as long as the bus never holds more than capacity riders.
  3. The bus's on-board count must be updated after every single board and drop.

All buses start empty. Each event is recorded as a LogEntry(time, bus_id, stop_id, passenger_id, action) with action being "board" or "drop".

Part 1: Shrink the rider population

downsamplePassengers(events, factor) -> List<PassengerEvent>

With 0 < factor <= 1, return about factor * events.size() riders while keeping the mix of (from_stop, to_stop) pairs close to the original. State precisely how you decide which riders to keep.

Part 2: Boarding and dropping with seat limits

simulate(busSchedules, passengers, capacity) -> List<LogEntry>

Process all bus visits in chronological order, applying the visit rule above. Waiting riders board first-come-first-served. Your solution must be fast enough for the stated limits.

Part 3: Check a log

validateLog(log, capacity) -> boolean   // or return the first error found

The check must detect at least these problems:

  • A bus's occupancy goes below zero.
  • A bus's occupancy goes above capacity.
  • A rider gets off before getting on.
  • A rider boards more than once (on a second bus, or the same bus again).

Describe what state you maintain while scanning and how it changes with each entry.

Part 4: Priority passes

Change the boarding step of Part 2 so that riders with has_priority_pass == true board before those without. Within each group, riders board by appear_time ascending, with ties broken consistently (for example by passenger_id). Capacity must still be strictly respected and counts must stay accurate.

What to deliver

Name the data structures you use for each part, explain why each solution is correct, and give time and space complexity. Mention edge cases you would handle.

Hints

Hint 1

For the sampling part, plain random sampling can lose rare origin-destination pairs. Think about grouping riders by (from_stop, to_stop) and giving each group its own share of the sample, then deciding how to distribute the rounding leftovers.

Hint 2

Do not scan every rider at every bus visit. Flatten all timetables into one sorted list of visits, sort riders by appear_time, and move a pointer forward to release riders into a per-stop waiting structure as time advances. For the bus, store riders bucketed by their destination stop so a drop is a direct lookup.

Hint 3

A min-heap per stop handles both FIFO and priority: use key (appear_time, passenger_id) for plain boarding, and (0 if priority else 1, appear_time, passenger_id) for priority boarding. For the validator, track occupancy per bus, which bus each rider is currently on, and the set of riders who have ever boarded; do not re-sort the log.