Data Structures & AlgorithmsHardGoogle · Flipkart interview

Shortest paths in a large grid where each move costs either 0 or 1. Which algorithm is the most efficient correct choice?

  1. A. Plain BFS, because all costs are small
  2. B. 0-1 BFS with a deque (0-cost moves to the front), O(V + E)
  3. C. Bellman–Ford, because some costs are zero
  4. D. DFS that follows 0-cost edges first

Correct answer: B. 0-1 BFS with a deque (0-cost moves to the front), O(V + E)

A deque keeps nodes in non-decreasing distance order, mimicking Dijkstra without a heap. Plain BFS counts edges, not costs; Bellman–Ford is O(V·E); greedy DFS can lock in a non-shortest route.

More Coding Interview Problems questions

Exams that ask this topic