KatInterview

Minimum Arrival Time in a Scheduled Transit Network

Trees & GraphsModel answer
MediumModel answerTrees & Graphsgraph-searchdijkstrabfs

Last updated

Find the earliest time a traveler can reach a destination station given a fixed train timetable, then reconstruct the full itinerary with transfers and waits.

Minimum Arrival Time in a Scheduled Transit Network

You have a complete timetable for a regional train network. Every train follows a fixed sequence of stops, each tagged with an integer arrival time (in minutes):

train_id -> [(station_name, time), (station_name, time), ...]

Times within a single train's schedule are strictly increasing. A traveler can board a train at a station at exactly the minute the train is listed there.

Input

  • trains: a dictionary mapping a train ID to its ordered list of (station, time) pairs.
  • start: the name of the departure station.
  • start_time: the integer minute at which the traveler arrives at the start station.
  • dest: the name of the destination station.

Movement rules

  • The traveler may wait at their current station; time advances to the next scheduled event there.
  • Any train whose schedule lists the traveler's current station at the current time may be boarded.
  • Once on a train, the traveler may ride it to any subsequent stop.
  • At any stop, the traveler may disembark and immediately board a different train that is present at the same station and time.
  • Your solution must terminate — unbounded minute-by-minute waiting is not acceptable.

Part 1

Return the earliest arrival time at dest, or None if it cannot be reached.

earliest_arrival(trains, start, start_time, dest) -> int | None

Part 2

Extend the solution to also return the full itinerary: the ordered list of legs, where each leg describes either a ride on a specific train (from one station/time to another) or a wait at a station. Return None if the destination is unreachable.

find_route(trains, start, start_time, dest)
    -> (arrival_time, legs) | None

A leg is a dict with at minimum:

{"kind": "ride" | "wait", "train": str | None,
 "from": (station, time), "to": (station, time)}

Example

trains = {
    "T1": [("A", 0), ("B", 10), ("C", 20)],
    "T2": [("B", 10), ("D", 15)],
    "T3": [("A", 5),  ("D", 30)],
}

earliest_arrival(trains, "A", 0, "D")  # -> 15
# Route: ride T1 A→B (0→10), transfer to T2, ride T2 B→D (10→15)

earliest_arrival(trains, "A", 0, "C")  # -> 20
# Route: ride T1 straight through A→B→C

Constraints

  • Station names are non-empty strings; train IDs are unique strings.
  • All times are non-negative integers; times within one train are strictly increasing.
  • Up to ~1 000 trains, each with up to ~500 stops.
  • start may not appear in the timetable at all (traveler arrives and no train ever calls there).
  • start == dest is a valid input.

Hints

Hint 1

Model each (station, time) pair as a node in a graph. From any such node there are two kinds of outgoing edges: ride to the next stop on the same train, or wait at the same station until the next scheduled event there. Because every edge strictly increases time, the first time you reach any node is already optimal — so Dijkstra (or even BFS ordered by time) works directly.

Hint 2

You don't need to track which train the traveler is currently on to find the optimal time. Boarding, transferring, and staying on a train all reduce to the same thing: taking a ride edge out of the current (station, time) node. Build an index from (station, time) to a list of trains present there, then iterate over their next stops to generate ride edges.

Hint 3

To avoid infinite waiting loops, build a sorted list of event times for each station and use binary search to jump to the next one. For path reconstruction in Part 2, store a parent pointer — (previous_node, edge_kind, train_id) — the first time each node is discovered. Walk the pointers back from the destination, reverse the list, then merge consecutive edges that share the same kind and train ID…