Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: Dijkstra is run on a directed graph with arcs 0→1 (5), 0→2 (2), 2→1 (1), 1→3 (2), 2→3 (7), 3→4 (1), 2→4 (9). Each vertex is settled once, in order of distance. In what order are vertices 0–4 settled? Report the vertex that is settled third.

  1. A. 1
  2. B. 2
  3. C. 0
  4. D. 3

Correct answer: A. 1

Dijkstra settles vertices in increasing distance; here vertex 2 is reached cheaply and then vertex 1 improves through it. Settling vertices in the order they are first discovered gives a different third vertex.

More Coding Interview Problems questions

Exams that ask this topic