def can_reach(schedule: list[list[str]], start: str, dest: str) -> bool:
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).
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.
Two of the three reports posted the full English prompt, and the second worked example appears only in the later one. One onsite candidate, after passing their own phone screen, got this question in the onsite coding round; that interviewer said there were two parts and not both had to be finished, and the candidate only discussed how to print the path. A phone-screen report says the hard parts are building station -> list of (train, index) and, in the search, skipping trains that have already left while letting the passenger wait. The tricky case candidates flag is getting off at B and changing to a train that is there at the same time.
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
Alternative approaches: BFS over (station, time) states with a wait-one-step move (the approach one onsite candidate describes); Earliest arrival per station with a heap (Dijkstra on time, the reference); A BFS that marks stations visited on first sight (wrong: it can record a late arrival and miss earlier trains)
Also reported as Train System can_reach
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.
How would you print the whole path the passenger takes?
| Approach | Notes |
|---|---|
| BFS over (station, time) states with a wait-one-step move | the approach one onsite candidate describes |
| Earliest arrival per station with a heap | Dijkstra on time, the reference |
| A BFS that marks stations visited on first sight | wrong: it can record a late arrival and miss earlier trains |
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
If you have had this round, tell us how close this page was. It is how we keep the pages accurate.
Send them this page. It is free to read, no account needed.
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.