← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026Remote

Summary

Amazon SWE coding round, one question the whole time: multi-source BFS. Seemed straightforward at first but there are enough edge cases to trip you up if you haven't seen it before.

Questions Asked (1)

Q1

Given a graph with multiple source nodes, implement multi-source BFS to find the shortest distance from any source node to every other node in the graph. Return a dictionary mapping each node to its minimum distance.

Algorithms & Data Structures
Author's notes

The setup looks clean until you realize you need to seed the queue with all sources at distance zero simultaneously, not run separate BFS passes and take the min afterward.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that multi-source BFS initializes the queue with all source nodes at distance 0, then performs standard BFS level by level. Emphasize that this computes shortest distances from the nearest source in O(V+E) time, and discuss handling disconnected nodes by assigning infinity or -1.

Pro tip: Mention that multi-source BFS is equivalent to adding a super-source connected to all sources with zero-weight edges, which justifies the correctness. Also, clarify how to handle duplicate sources and unreachable nodes to show attention to edge cases.

1. Clarify requirements and assumptions

Confirm whether the graph is directed or undirected, weighted or unweighted, and how to represent unreachable nodes. Ask if sources can be repeated or if the graph is guaranteed connected.

2. Initialize data structures

Create a distance dictionary mapping each node to infinity (or -1), set all source nodes to 0, and initialize a queue with all sources. Use a visited set or rely on distance to avoid revisiting.

3. Perform BFS level by level

While the queue is not empty, pop a node, and for each neighbor not yet visited, set its distance to current distance + 1 and enqueue it. This ensures the first time a node is reached, it's via the shortest path from any source.

4. Return the distance dictionary

After BFS completes, return the dictionary. Nodes still at infinity (or -1) are unreachable from any source.

5. Analyze complexity and edge cases

State that time complexity is O(V+E) and space is O(V). Discuss edge cases: empty graph, no sources, sources covering all nodes, and disconnected components.

Key Points to Mention

  • Multi-source BFS initializes the queue with all sources at distance 0, effectively simulating a super-source.
  • BFS guarantees shortest path in unweighted graphs because it explores nodes in increasing order of distance.
  • Use a dictionary to map nodes to distances, initializing with infinity or -1 for unreachable nodes.
  • Time complexity is O(V+E) and space complexity is O(V) for the queue and distance map.
  • Handle edge cases: no sources (all distances infinity), sources overlapping, and disconnected graphs.
  • Avoid revisiting nodes by checking if distance is already set or using a visited set.

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