Data Structures & AlgorithmsHardTCS NQT · Accenture

Problem: In a binary-tree-free DFS on the undirected graph with edges 0-1, 0-2, 1-3, 2-4, 3-4, 4-5, starting from vertex 0 and always visiting the smallest unvisited neighbour first, what is the last vertex visited?

  1. A. 6
  2. B. 4
  3. C. 7
  4. D. 5

Correct answer: D. 5

Iterative DFS with a stack visits neighbours in the order they are pushed; pushing in reverse sorted order makes the smallest neighbour come off first. The last vertex is the leaf reached at the end of the deepest branch.

More Coding Interview Problems questions

Exams that ask this topic