Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: On a rooted tree (root 1) with children: 1: [2, 3], 2: [4, 5, 6], 3: [7], 5: [8, 9]. How many nodes have exactly 2 descendants (descendants exclude the node itself)?

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

Correct answer: A. 1

Count descendants recursively, excluding the node itself. Counting the subtree size (which includes the node) shifts the target by one.

More Coding Interview Problems questions

Exams that ask this topic