Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: A directed graph on vertices 0–4 has arcs 0→1 (2), 0→2 (6), 1→2 (3), 1→3 (9), 2→3 (1), 3→4 (2), 2→4 (8). What is the length of the shortest path from 0 to 4?

  1. A. 8
  2. B. 9
  3. C. 13
  4. D. 14

Correct answer: A. 8

0→1→2→3→4 costs 2 + 3 + 1 + 2 = 8. Taking the 6-arc first (0→2→3→4) costs 9, 0→1→3→4 and 0→1→2→4 both cost 13, and 0→2→4 costs 14.

More Coding Interview Problems questions

Exams that ask this topic