Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: A tree with 6 nodes is given by parent array p = [−1, 0, 0, 2, 2, 4] for nodes 0–5 (root 0). What is the number of nodes whose subtree has size at least 2?
- A. 4
- B. 2
- C. 3
- D. 5
Correct answer: C. 3
Subtree sizes accumulate from the deepest nodes upward; a node has subtree size at least 2 exactly when it has at least one child. Counting leaves instead gives the complement count.
More Coding Interview Problems questions
- Problem: The string "xyzzyxzz" is considered. How many of its substrings of length 3 are palindromes (counted by index)?
- Problem: Among the strings "abc", "bca", "cab", "acb", "bac", "cba", how many distinct pairs (x, y) with x ≠ y are rotations of each other…
- Problem: Find the length of the longest substring of "aaabbbbcccd" that contains at most two distinct letters.
- Problem: How many numbers from 1 to 1000 have a digit sum divisible by 9?
- Problem: How many ways are there to write 20 as an ordered sum of parts from {2, 3, 5}?
- Problem: The sequence a0 = 0 and a(n) = a(n−1) + (n mod 3) for n ≥ 1 is defined. What is a(30)?