Data Structures & AlgorithmsHardTCS NQT Digital · Infosys

Problem: A road map has junctions 0–6 and two-way roads with lengths: 0-1 (4), 0-2 (1), 2-1 (2), 1-3 (5), 2-3 (8), 3-4 (3), 2-5 (10), 4-6 (2), 5-6 (1). What is the length of the shortest route from junction 0 to junction 6?

  1. A. 12
  2. B. 13
  3. C. 14
  4. D. 3

Correct answer: A. 12

Dijkstra: dist(2) = 1, dist(1) = 3, dist(3) = 8, dist(5) = 11, dist(4) = 11, and dist(6) = min(11 + 1, 11 + 2) = 12 via 0-2-5-6. The route through 3 and 4 gives 13, 0-1-3-4-6 gives 14, and 3 counts roads instead of length.

More Coding Interview Problems questions

Exams that ask this topic