Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA

Problem: A city grid has V = 10⁵ junctions; every road takes either 0 minutes (flyover) or 1 minute. What is the most efficient way to get shortest times from one source?

  1. A. Plain BFS with a queue – O(V + E) and exact
  2. B. 0-1 BFS with a deque: push 0-weight neighbours to the front, 1-weight to the back – O(V + E)
  3. C. Floyd–Warshall – O(V³)
  4. D. DFS that keeps the first distance found

Correct answer: B. 0-1 BFS with a deque: push 0-weight neighbours to the front, 1-weight to the back – O(V + E)

With only 0/1 weights the deque stays sorted by distance, so it behaves like Dijkstra without the log factor. Plain BFS counts edges and would treat a 0-minute road as 1. Floyd–Warshall is 10¹⁵ operations. DFS order does not give shortest distances.

More Coding Interview Problems questions

Exams that ask this topic