Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: Given the parent array p = [−1, 0, 0, 1, 1, 2, 4, 4] for nodes 0–7 (node 0 is the root), how many nodes have exactly one child?

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

Correct answer: D. 1

Count each node's children from the parent array, then tally those with exactly one. Counting leaves (zero children) is a different quantity.

More Coding Interview Problems questions

Exams that ask this topic