Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: Consider the undirected graph with edges 0-1, 1-2, 2-0, 2-3, 3-4 and 4-5, 5-3. A DFS from vertex 0 (visiting neighbours in increasing order) records each vertex when first entered. In what order are the vertices entered? Report the 5th vertex entered (1-based).

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

Correct answer: B. 4

Recursive DFS enters a vertex, then immediately explores its smallest unvisited neighbour. The order is 0, 1, 2, 3, 4, 5, so the fifth vertex entered is 4.

More Coding Interview Problems questions

Exams that ask this topic