Data Structures & AlgorithmsModerateICPC regional · Product Company SDE

Problem: Using the parent array par[i] for nodes 0–9 with par = [−1, 0, 0, 1, 1, 3, 3, 5, 6, 8], what is the 3rd ancestor of node 9 (the parent is the 1st ancestor; use node index if no such ancestor exists, and report −1 if it does not exist)?

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

Correct answer: B. 3

Walk up the parent pointers the required number of times, stopping if the root is passed. Counting the ancestors starting from the root gives a different node.

More Coding Interview Problems questions

Exams that ask this topic