Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: An undirected graph has edges 0-1, 0-2, 1-3, 2-3, 3-4, 4-5, 2-5 (7 edges, 6 vertices 0–5). How many distinct shortest paths (by number of edges) go from vertex 0 to vertex 5?

  1. A. 1
  2. B. 2
  3. C. 0
  4. D. 3

Correct answer: A. 1

BFS that adds path counts along edges into the next layer counts shortest paths exactly. Counting simple paths of any length would include longer routes through vertex 4.

More Coding Interview Problems questions

Exams that ask this topic