← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Amazon SWE interview with a graph/shortest-path problem that looked deceptively clean on the surface but had a lot of moving parts once you started thinking about correctness. One question, but it kept going for a while.

Questions Asked (1)

Q1

You have a start point, an end point, a threshold K, and an undirected graph of bus stations with integer coordinates. Walking costs Manhattan distance; riding any connected sequence of bus stations costs zero. Given that you can walk to a station, ride buses freely across connected stations, then walk to the destination (or just walk the whole way), design an algorithm to determine whether the total walking distance can be kept at or below K. Justify correctness, analyze complexity, and implement a function returning true or false.

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

My first instinct was Dijkstra but I second-guessed myself because the bus edges are zero-cost and I wasn't sure if that broke anything.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a graph where bus stations are nodes and connected components represent zero-cost travel. The total walking distance is the minimum over all choices of start station and end station of the Manhattan distance from start to start station plus the Manhattan distance from end station to end, or simply the direct Manhattan distance if walking all the way. Use union-find to identify connected components, then check if any pair of stations (one from a component reachable from start, one from a component reachable from end) yields a total walking distance ≤ K.

Pro tip: Emphasize that you can walk to any station, not necessarily the nearest one, and that the optimal solution might involve walking to a station in a component that is not the closest but connects to a component near the destination. Also, consider the trivial case of walking directly without using buses.

1. Understand the problem and clarify constraints

Restate the problem: given start and end points, a threshold K, and an undirected graph of bus stations with coordinates, determine if total walking distance can be ≤ K. Clarify that riding buses is free within connected components, and walking cost is Manhattan distance.

2. Model as graph components and walking costs

Use union-find to compute connected components of the bus graph. For each component, we can enter at any station and exit at any station for free. The walking cost is the sum of distances from start to entry station and from exit station to end, or direct walking.

3. Formulate the condition and algorithm

The condition is: min( Manhattan(start, end), min_{c1,c2} ( min_{s in c1} Manhattan(start, s) + min_{t in c2} Manhattan(t, end) ) ) ≤ K, where c1 and c2 are components (possibly same). Compute for each component the minimum distance from start to any station in it, and from end to any station in it. Then check all pairs of components (including same component) for sum ≤ K.

4. Analyze complexity and optimize

Naively checking all pairs of components is O(C^2) where C is number of components. But we can optimize: for each component, store minStartDist and minEndDist. Then we need to check if there exist two components with minStartDist + minEndDist ≤ K. This can be done by sorting or using a two-pointer approach after sorting, or simply by keeping track of the minimum minEndDist seen so far while iterating over components sorted by minStartDist. Complexity: O(N log N) for sorting, plus O(N α(N)) for union-find.

5. Justify correctness and implement

Prove that any valid path corresponds to walking from start to some station in a component, riding within components (possibly multiple components if they are connected? Actually, riding is free only within connected components; to move between components you must walk. So the path is: walk from start to station s1 in component C1, ride within C1 to station s2, walk to station s3 in component C2, ride within C2 to s4, walk to end. But since walking between components costs Manhattan distance, the optimal is to use at most one component? Wait: if you use multiple components, you walk between them, which is equivalent to walking directly from s2 to s3. But you could also just walk from start to s1, ride to s2, then walk to end. Using multiple components might help if the walk between components is shorter than walking directly? Actually, if you use two components, you walk from start to C1, ride to some station, walk to C2, ride to some station, walk to end. The total walking is dist(start, entry1) + dist(exit1, entry2) + dist(exit2, end). This is at least dist(start, entry1) + dist(exit2, end) by triangle inequality? Not necessarily, because dist(exit1, entry2) could be small. But note that you could also just walk from start to entry1, then walk to entry2, then walk to end? That would be dist(start, entry1) + dist(entry1, entry2) + dist(entry2, end) which is ≥ dist(start, end) by triangle inequality. But with buses, you skip the ride within components. However, the walking between components is still there. So using multiple components might reduce walking compared to walking directly? Consider start and end far apart, but there are two components: one near start, one near end, and the distance between the components is small. Then walking start->C1 (small), ride within C1 (free), walk C1->C2 (small), ride within C2 (free), walk C2->end (small). Total walking = small + small + small. Direct walking = large. So using multiple components can help. But wait: if you use multiple components, you are essentially walking from exit of C1 to entry of C2. That walking is Manhattan distance. So the total walking is dist(start, entry1) + dist(exit1, entry2) + dist(exit2, end). This is equivalent to walking from start to entry1, then to entry2, then to end, but with free rides within components. However, note that you could also just walk from start to entry1, then walk to entry2, then walk to end, which is the same walking distance. So the buses don't help reduce the walking between components; they only help within components. So the optimal strategy is to use at most one component? Let's test: Suppose we use two components. The walking is d1 + d2 + d3 where d1 = dist(start, s1), d2 = dist(s2, t1), d3 = dist(t2, end). But we could instead just walk from start to s1, then walk to t1, then walk to end? That would be d1 + dist(s1, t1) + d3. But d2 = dist(s2, t1) and s2 is in same component as s1, so dist(s1, s2) is not necessarily zero; but we can ride from s1 to s2 for free. So the walking is d1 + dist(s2, t1) + d3. If we skip the first component, we could walk from start to s2 directly? That would be dist(start, s2) which might be larger than d1 + dist(s1, s2)? Actually, by triangle inequality, dist(start, s2) ≤ dist(start, s1) + dist(s1, s2) = d1 + dist(s1, s2). But we don't know dist(s1, s2). However, we can choose s1 and s2 arbitrarily within the component. So to minimize d1 + dist(s2, t1), we can choose s1 to minimize dist(start, s1) and s2 to minimize dist(s2, t1). But these are independent? Actually, we can choose any s1 and s2, and we ride from s1 to s2 for free. So the cost is min_{s1 in C1} dist(start, s1) + min_{s2 in C1} dist(s2, t1) + d3. But note that min_{s1} dist(start, s1) + min_{s2} dist(s2, t1) is not necessarily equal to min_{s1,s2} (dist(start, s1) + dist(s2, t1)) because the choices are independent. So we can achieve the sum of the two minima. Now, if we skip C1, we would walk from start to t1 directly: dist(start, t1). By triangle inequality, dist(start, t1) ≤ dist(start, s1) + dist(s1, t1) for any s1. But we don't have dist(s1, t1) in our cost; we have dist(s2, t1) where s2 is another station. So it's not directly comparable. However, we can always choose s1 = s2, then the cost becomes dist(start, s1) + dist(s1, t1) + d3 ≥ dist(start, t1) + d3 by triangle inequality. So using C1 with s1=s2 is worse than just walking from start to t1. But we can choose s1 and s2 differently to potentially get a smaller sum than dist(start, t1). For example, if start is near s1 and t1 is near s2, but s1 and s2 are far apart, then dist(start, s1) + dist(s2, t1) could be small, while dist(start, t1) is large. So using the component can help. So multiple components can help. Therefore, the optimal path may involve multiple components. But wait: if we use multiple components, we are walking between components. That walking is Manhattan distance. So the total walking is the sum of walking segments. This is equivalent to: we have a set of components, and we can walk from start to any station in any component, then ride within that component to any station, then walk to any station in another component, etc., and

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.