You are given a partially implemented codebase for simulating bus routes in a city. Buses travel along fixed routes with scheduled arrival times at each stop. Passengers appear at stops and want to travel to other stops.
Each bus has:
bus_id
.
C
(maximum number of passengers allowed on board at any time).
(time, stop_id)
pairs indicating when the bus arrives at each stop.
Each passenger has:
passenger_id
.
appear_time
: when the passenger appears at a stop.
from_stop
: the stop where they start.
to_stop
: the stop where they want to get off.
has_priority_pass
: a boolean indicating whether the passenger has a priority pass.
The simulation runs in discrete time. At each scheduled bus arrival (time, stop_id) for a bus:
to_stop
equals
stop_id
.
stop_id
whose
appear_time <= time
and who have not yet boarded any bus.
C
.
The codebase defines basic data structures like BusSchedule and PassengerEvent, but several key functions are left for you to implement.
You may assume:
E
is up to 100,000.
B
is up to 1,000.
The full list of passenger events may be very large, making the simulation slow. Implement a function to downsample the passenger events to reduce the number of simulated passengers while approximately preserving the demand distribution across origin–destination pairs.
API (conceptual):
where:
0 < factor <= 1
is a scale factor (e.g.,
0.1
means simulate ~10% of the passengers).
factor * events.size()
passengers.
(from_stop, to_stop)
pairs as much as reasonably possible.
Specify clearly how you choose which passengers to keep so that the distribution is roughly preserved.
Implement the main simulation logic that handles bus boarding and dropping, ensuring capacities are never exceeded and on-board counts are correctly tracked.
API (conceptual):
where each LogEntry has:
time
bus_id
stop_id
passenger_id
action
in {"board", "drop"}
Simulation rules:
(time, stop_id)
in each
BusSchedule
, process events in
time order
.
to_stop == stop_id
(emit
"drop"
log entries).
stop_id
with
appear_time <= time
and not yet boarded any bus, pick up passengers
without ever exceeding capacity
.
board
/
drop
.
Your implementation should be efficient enough to handle the stated constraints.
You are given a log generated by some (possibly buggy) version of the simulation. You need to verify whether the log respects the bus capacity constraints and basic consistency rules.
API (conceptual):
The validator should check at least the following:
capacity
.
You may also return the first error found (e.g., by returning an error description instead of just boolean), but at minimum you must be able to detect whether the log is valid or not.
Describe the state you need to track while scanning the log and how you update it.
Extend your boarding logic in Task 2 to support priority passes:
has_priority_pass == true
should be boarded
before
non-priority passengers when a bus arrives at a stop.
appear_time
ascending, breaking ties consistently, e.g., by
passenger_id
).
Update simulate(...) so that log entries reflect this behavior, and the on-board passenger counts remain correct.
For all tasks, clearly define and use appropriate data structures, and design your solution to run efficiently for the given input sizes.