Reach a destination by bus with limited walking
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's coding round, last asked Aug 2025. It tests graphs, bfs and manhattan distance.
You are given a start point, an end point, a walking budget k and a bus network. Each bus station has grid coordinates, and the network lists two-way links between stations. Riding between linked stations is free, so you can move between any stations in the same connected part of the network at no cost. Walking between any two points costs their Manhattan distance. Decide whether you can get from start to end using any mix of walks and bus rides with a total walking distance of at most k.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
can_reach(start: tuple[int, int], end: tuple[int, int], k: int, stations: list[tuple[int, int]], links: list[tuple[int, int]]) -> bool—linksholds pairs of station indexes.
Example
can_reach((0, 0), (10, 0), 2, [(1, 0), (9, 0)], [(0, 1)]) -> True # walk 1, ride, walk 1
can_reach((0, 0), (10, 0), 1, [(1, 0), (9, 0)], [(0, 1)]) -> False # needs 2 walking
can_reach((0, 0), (10, 10), 3, [(1, 0), (5, 5), (9, 9)], [(0, 1), (1, 2)]) -> True # walk 1 + walk 2
can_reach((0, 0), (10, 10), 2, [(1, 0), (5, 5), (9, 9)], [(0, 1), (1, 2)]) -> False
Constraints
- Coordinates are integers and may be negative;
0 <= k. -
0 <=number of stations; links are two-way and may form any graph. - Walking straight from start to end is always allowed.
Clarifying questions
- Is
kthe total walking distance, or the limit for each single walk? - Can you walk between two stations to change lines, and does that walk count toward
k? - How many stations: is an O(S²) graph of all walking pairs acceptable?
- Do you need the route, or only yes or no?
What a strong answer covers
- Model start, end and every station as nodes; walking edges weigh the Manhattan distance, bus links weigh 0.
- Collapse each connected component of the bus network (union-find or BFS) into one node, then run Dijkstra (or 0-1 BFS ideas) on the walking cost; answer whether the distance to the end is at most
k. - With
kper walk instead of in total: a plain BFS or union-find where a walking edge exists only when its length is at mostk. - Complexity: O(S²) walking edges in the dense version, and how to cut it (only walks between components, nearest-station queries with a k-d tree or sorting by coordinates).
- Edge cases: no stations, start equals end, start or end on a station,
k = 0.
Follow-ups
- Return the minimum walking distance, or the route itself.
- Each bus ride also takes time; minimize total travel time with walking limited to
k. - Many queries on the same network with different start and end points.
Where it's asked
- Amazon · Coding · Last asked Aug 202555 min · Usually two rounds; one is often an object-oriented design built in steps · No AI
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
