Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: The rooted tree (root 0) has children 0: [1, 2], 1: [3, 4], 2: [5], 4: [6, 7], 5: [8]. Nodes are numbered in DFS pre-order with children visited in list order. What is the pre-order index (0-based) of node 6?

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

Correct answer: D. 4

Pre-order lists a node before its children, and the whole subtree of node 1 (including nodes 3 and 4) is finished before node 6 is reached. Counting only the siblings of node 6 misses those earlier descendants.

More Coding Interview Problems questions

Exams that ask this topic