AO
Back

Train Schedule Reachability with Transfers and Waiting

Also reported as Train System can_reach

Phone ScreenPhone ScreenLast reported October 2026Low Frequency

Problem Overview

The round: three Glean reports in 2026, two phone screens (one for an ML role whose recruiter said to expect ordinary data structures and algorithms) and one 60-minute onsite coding round. Two of them posted the prompt in full; the statement below is that prompt in our words, with its examples unchanged. The onsite report describes a second part, printing the whole path, and says the interviewer told them the question has two parts and they need not finish both. Two graded parts.

Background

A train system spans stations named by strings. Starting from time 0, each train visits a list of stations, and the index in the list is the time it arrives there. At the end of its list the train stops for the day.

schedule = [
    ["A", "B", "C"],  # train 0: A at time 0, ends at C at time 2
    ["D", "B", "E"],  # train 1: D at time 0, ends at E at time 2
]

A passenger can board a train and get off at any later station on it. They can also wait at a station for a train that arrives later. Above, a passenger at A at time 0 can ride train 0 to C, or get off at B at time 1 and change to train 1 to reach E.

Part 1 — can the passenger get there?

def can_reach(schedule: list[list[str]], start: str, dest: str) -> bool:

Start at start at time 0; return whether dest can be reached at any time. You may assume both stations appear in the schedule; the tests only use stations that do (the reference raises ValueError for a start station that does not, untested).

can_reach(schedule, "A", "C")  # True
can_reach(schedule, "A", "E")  # True
can_reach(schedule, "C", "A")  # False
can_reach(schedule, "A", "D")  # False

A second reported example, where the passenger has to wait at B:

schedule = [
    ["A", "B", "C", "D", "E"],
    ["W", "X", "Y", "B", "Z"],
]
can_reach(schedule, "A", "Z")  # True: off at B at time 1, train 1 leaves B at time 3
can_reach(schedule, "W", "E")  # False: train 0 left B at time 1
can_reach(schedule, "B", "Z")  # True

A train that is at a station at exactly the time you arrive there can be boarded (that is how A reaches E in the first example).

Part 2 — print the path

def find_path(schedule: list[list[str]], start: str, dest: str) -> list[tuple] | None:

Return one journey that reaches dest, as a list of legs (train, board_station, board_time, alight_station, alight_time) in travel order. For the first example, find_path(schedule, "A", "E") is [(0, "A", 0, "B", 1), (1, "B", 1, "E", 2)]. Return [] when start == dest and None when dest cannot be reached. The journey must arrive at the earliest possible time; when several do, any of them passes the tests.

Notes: the statement and the Part 1 examples are reported. find_path, the leg format, the earliest-arrival rule, the [] and None returns and the ValueError are ours: the report only says Part 2 asks how you would print the whole path.

Follow-up Arc

Interviewers escalate through these phases. The order varies, but most candidates see at least one from each bucket.
Trade-off discussion · 1
Trade-off discussion

How would you print the whole path the passenger takes?

Probes for: after can_reach works

Approach Trade-offs

Approaches actually attempted in reports — including ones that lost candidates time. Pick deliberately.
ApproachNotes
BFS over (station, time) states with a wait-one-step movethe approach one onsite candidate describes
Earliest arrival per station with a heapDijkstra on time, the reference
A BFS that marks stations visited on first sightwrong: it can record a late arrival and miss earlier trains

What Reports Emphasize

What candidates report: The hard part is the graph: index each station to the trains that stop there and the time (index) they do. In the search, skip trains that have already left and let the passenger wait at the station. Candidates flag the case where the passenger gets off at B and changes to another train that is there at the same time. One onsite candidate searched over (station, time) states, choosing between getting off to change and staying until time + 1; for the path part they only discussed the idea, after the interviewer said not both parts need finishing.

Edge cases to test: A change at the same time as arrival; Waiting at a station for a later train; A train that left before you arrived; A station a train visits twice; Start equal to destination; Reaching a station late first and early later in the search

Practice

Write your own against 10 test cases, or read the worked solution — approach, complexity, and code that runs.
Did this match your interview?

If you have had this round, tell us how close this page was. It is how we keep the pages accurate.

Prepping for Glean with friends?

Send them this page. It is free to read, no account needed.

More Glean Questions

Free preview

Every question in the Glean catalog gets this depth

What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 12 Glean questions, refreshed monthly from new candidate reports.

$59/mo — or $50/mo with the 3-month pass · cancel anytime
Glean · Phone Screen · Reported 3× across candidate reports
Is this helpful?