Data Structures & AlgorithmsHardProduct Company SDE · ICPC regional

Problem: A directed graph has arcs 0→1 (0), 1→2 (3), 0→2 (5), 2→3 (0), 1→3 (7), 3→4 (2), 2→4 (4), 4→5 (1), 0→5 (9). Arc weights may be zero but there are no negative weights. What is the shortest distance from vertex 0 to vertex 5?

  1. A. 9
  2. B. 6
  3. C. 8
  4. D. 10

Correct answer: B. 6

The cheapest route is 0→1→2→3→4→5 with cost 0 + 3 + 0 + 2 + 1 = 6. The direct arc costs 9 (fewest arcs, not least weight), 0→2→3→4→5 costs 8, and 0→1→3→4→5 costs 10.

More Coding Interview Problems questions

Exams that ask this topic