KatInterview

Earliest-Arrival Reachability on Timed Transit Routes

Trees & GraphsAuto-graded · 24 test cases
MediumAuto-gradedTrees & Graphsgraph searchBFSgreedy

Last updated

Tests whether a candidate can model time-respecting graph search over indexed transit routes to determine if a destination is reachable from a starting stop.

Earliest-Arrival Reachability on Timed Transit Routes

You are given a transit network as a list of routes. Each route is itself a list of stop names. The position of a stop within a route acts as its absolute departure time: a stop at index t is visited at time t. Every route begins operating at time 0.

A traveler begins at stop origin at time 0.

At any point the traveler may:

  • Board a vehicle at their current stop, provided the vehicle visits that stop at the traveler's current time or later.
  • Ride that same vehicle forward to any subsequent stop on its route (earlier stops on the same route cannot be revisited by riding backward).
  • Disembark at any stop and wait there indefinitely.
  • Board a different vehicle at the same stop at any future time.

Write can_reach_destination(routes, origin, destination) that returns True if the traveler can eventually arrive at destination, and False otherwise.


Key Rules

  • A vehicle at a stop departs at exactly the time equal to its index in the route list.
  • Travel is forward-only within a single route: you cannot ride a vehicle to an earlier index on the same route.
  • Waiting at a stop is free and has no time limit; a traveler who arrives at stop S at time T can board any vehicle that visits S at any time ≥ T.
  • If origin equals destination, the traveler is already there.
  • origin and destination are both guaranteed to appear at least once somewhere across all routes.

Constraints

  • 1 ≤ len(routes) ≤ 500
  • 1 ≤ len(routes[i]) ≤ 1000 for each route i
  • Stop names are non-empty strings.
  • Total number of stop-visits across all routes: at most 500 × 1000 = 500 000
  • Multiple routes may visit the same stop at the same or different times.
  • A single route may list the same stop more than once.

Examples

Example 1

Input: can_reach_destination([['A', 'B', 'C'], 'A', 'C'], 'A', 'C')

Output: True

Example 2

Input: can_reach_destination([['A', 'X', 'B'], ['B', 'Y', 'C']], 'A', 'C')

Output: False

Example 3

Input: can_reach_destination([['A', 'B'], ['B', 'A']], 'A', 'A')

Output: True

Example 4

Input: can_reach_destination([['A', 'B', 'C'], ['C', 'B', 'A']], 'C', 'A')

Output: True

Hints

Hint 1

Build a lookup from each stop name to all the (time, route_id, position) triples where that stop appears — this lets you quickly find every vehicle you could board after arriving at a stop at time T.

Hint 2

Think in terms of earliest arrival: if you can reach stop S at time T1 and also at time T2 > T1, the later arrival is strictly worse because waiting is free. A min-heap ordered by arrival time naturally explores better states first.

Hint 3

When you board a vehicle at position p on a route, you can only continue forward (to positions > p). Track the earliest position at which each route has already been boarded so you never process the same forward segment twice.