Decide reachability with buses and limited walking
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This question evaluates the ability to combine graph reachability with geometric distance constraints, testing competencies in graph algorithms, Manhattan-distance reasoning, and constrained-path modeling within the Coding & Algorithms domain.
Constraints
- 0 <= len(stations) <= 10^5
- 0 <= len(edges) <= 2 * 10^5
- Coordinates and k may be negative or non-negative integers within 64-bit range
- edges reference valid 0-indexed positions in stations; routes are bidirectional
- Riding between any two stations in the same connected component is free
Examples
Input: ([0, 0], [10, 10], 6, [[1, 1], [8, 8]], [[0, 1]])
Expected Output: True
Explanation: Direct walk = 20 > 6. Stations 0 and 1 are connected. Walk start->station0 (cost 2), ride free to station1, walk station1->end (cost |8-10|+|8-10|=4). Total walking = 6 <= 6, so True.
Input: ([0, 0], [10, 10], 3, [[1, 1], [8, 8]], [[0, 1]])
Expected Output: False
Explanation: Best bus plan costs 2 + 4 = 6 and direct walk is 20; both exceed k=3, so False.
Input: ([0, 0], [5, 5], 10, [], [])
Expected Output: True
Explanation: No stations, but the direct Manhattan walk is 10 <= 10, so option (b) succeeds.
Input: ([0, 0], [100, 100], 5, [], [])
Expected Output: False
Explanation: No stations and direct walk = 200 > 5, so unreachable.
Input: ([0, 0], [20, 20], 4, [[1, 1], [9, 9], [19, 19]], [[0, 1]])
Expected Output: False
Explanation: Components are {0,1} and {2}. {0,1}: entry 2 (station0) + exit min(38,22)=22 = 24. {2}: 38 + 2 = 40. Direct = 40. All > 4, so False (the far station 19,19 is in its own component, not bridged).
Input: ([0, 0], [20, 20], 4, [[1, 1], [9, 9], [19, 19]], [[0, 1], [1, 2]])
Expected Output: True
Explanation: Now all three stations are one component. Best entry = 2 (station0), best exit = 2 (station 19,19). Total = 4 <= 4, so True.
Input: ([0, 0], [0, 0], 0, [[5, 5]], [])
Expected Output: True
Explanation: start == end, direct walk = 0 <= 0, so True with no walking needed.
Input: ([-3, -3], [3, 3], 4, [[-2, -2], [2, 2]], [[0, 1]])
Expected Output: True
Explanation: Negative coordinates. Direct walk = 12 > 4. Stations connected: entry = 2 (to (-2,-2)), exit = 2 (from (2,2)), total = 4 <= 4, so True.
Input: ([0, 0], [10, 0], 1, [[5, 0]], [])
Expected Output: False
Explanation: Single isolated station: entry 5 + exit 5 = 10; direct = 10; both > 1, so False.
Input: ([0, 0], [10, 0], 10, [[5, 0]], [])
Expected Output: True
Explanation: Direct walk = 10 <= 10, so reachable via option (b) (the lone station offers no improvement here).
Hints
- Riding is free within a connected component, so the only costs are the walk from start into a component and the walk out of a component to end. Group stations into components first (union-find or BFS/DFS).
- For each component C, the cheapest plan that uses it is min_{s in C} d(start, s) + min_{t in C} d(t, end) — the best entry plus the best exit, since s and t may differ.
- Don't forget the no-bus option: a direct walk d(start, end) <= k. Take the minimum over the direct walk and every component's entry+exit, then compare to k.