Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: Count the pairs of nodes at the same depth (root depth 0) that are NOT siblings (no common parent), in the tree with parent array [−1, 0, 0, 1, 1, 2, 2, 3] for nodes 0–7. Each unordered pair counts once.

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

Correct answer: A. 4

Group nodes by depth, then subtract the sibling pairs within each depth group. Counting all same-depth pairs includes the siblings the question excludes.

More Coding Interview Problems questions

Exams that ask this topic