Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: A rooted tree (root 1) with children: 1: [2, 3], 2: [4, 5], 3: [6], 4: [7], 5: [], 6: [], 7: []. Children are visited in increasing order (pre-order DFS). Which node is visited 5th (the root is 1st)?

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

Correct answer: C. 5

Pre-order DFS visits a node and then its children in order. Pushing children in increasing order onto a stack reverses the visiting order, which is the usual slip.

More Coding Interview Problems questions

Exams that ask this topic