Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: A graph's vertices 0–4 are connected by edges 0-1 (1), 1-2 (1), 2-3 (1), 3-4 (1), 0-4 (5), 1-3 (3). What is the largest shortest-path distance from vertex 0 to any other vertex?

  1. A. 5
  2. B. 4
  3. C. 3
  4. D. 6

Correct answer: B. 4

Dijkstra from vertex 0 gives distances 1, 2, 3, 4 along the path 0-1-2-3-4 (the direct edge of weight 5 is never used). The largest distance is the one to vertex 4, which is 4.

More Coding Interview Problems questions

Exams that ask this topic