Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: A rooted tree is given by parent array p = [-1, 0, 0, 1, 1, 2, 5, 5, 3] for nodes 0–8 (node 0 is the root). How many nodes are at depth exactly 3?

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

Correct answer: B. 3

Walking up parent pointers gives each node's depth. Counting depth from 1 (instead of the root at 0) shifts the answer to the nodes at depth 2 — the usual off-by-one.

More Coding Interview Problems questions

Exams that ask this topic