Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: The undirected graph on vertices 0, 1, 2 has edges 0-1 (5), 1-2 (5) and 0-2 (12). What is the shortest path length from 0 to 2?

  1. A. 12
  2. B. 5
  3. C. 10
  4. D. 22

Correct answer: C. 10

Via vertex 1 the cost is 5 + 5 = 10, cheaper than the direct edge 12. Taking the single direct edge gives 12, 5 is one hop, and 22 adds every edge.

More Coding Interview Problems questions

Exams that ask this topic