Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: The rooted tree (root 1) has children 1: [2, 3], 2: [4, 5], 3: [6], 4: [7], 6: [8]. How many nodes have a depth divisible by 3 (the root has depth 0)?

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

Correct answer: A. 3

Carry the depth through a traversal and count nodes whose depth is a multiple of three, including the root at depth 0. Forgetting the root gives one fewer.

More Coding Interview Problems questions

Exams that ask this topic