Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: A rooted tree is given by parent array p = [-1, 0, 0, 1, 1, 2, 5, 5, 3] for nodes 0–8 (node 0 is the root). How many nodes are at depth exactly 3?
- A. 4
- B. 3
- C. 2
- D. 5
Correct answer: B. 3
Walking up parent pointers gives each node's depth. Counting depth from 1 (instead of the root at 0) shifts the answer to the nodes at depth 2 — the usual off-by-one.
More Coding Interview Problems questions
- Problem: A max-heap starts with the values 9, 8, 6, 5, 4, 3, 1 (inserted in that order). Then three times in succession: pop the maximum…
- Problem: The string "abcbaxyzzyxq" is given. What is the minimum number of characters that must be deleted so the remaining string is a…
- Problem: A string is formed by writing the letters of "kayak" then "level" then "rotor". How many substrings of length 5 of…
- Problem: Starting from the string "00000" of five zeros, operations flip a substring: each operation chooses positions l ≤ r and flips…
- Problem: The number of ways to choose 3 vertices from a graph's vertex set of size n = 12 such that no two chosen vertices are adjacent in…
- Problem: A stream of 12 numbers 5, 1, 4, 4, 2, 5, 3, 1, 2, 3, 3, 4 arrives. How many distinct values appear at least twice in the stream?