Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: Count the pairs of nodes at the same depth (root depth 0) that are NOT siblings (no common parent), in the tree with parent array [−1, 0, 0, 1, 1, 2, 2, 3] for nodes 0–7. Each unordered pair counts once.
- A. 4
- B. 5
- C. 3
- D. 6
Correct answer: A. 4
Group nodes by depth, then subtract the sibling pairs within each depth group. Counting all same-depth pairs includes the siblings the question excludes.
More Coding Interview Problems questions
- Problem: Insert the keys 50, 20, 70, 10, 30, 60, 90, 25, 35, 65 into a binary search tree in this order. How many keys in the tree lie in…
- Problem: Count the ways to segment "catsanddog" into a sequence of words, each taken from the dictionary {cat, cats, and, sand, dog}…
- Problem: How many distinct subsequences (as distinct strings, including the empty string) does "aba" have?
- Problem: A binary string "1100101011" is to be changed so that it alternates (no two adjacent characters are equal). Each operation flips…
- Problem: Count the ordered pairs of words (i, j) with i ≠ j in ["abc", "cba", "bca", "abc", "cab", "xyz"] such that the second word is the…
- Problem: For the string "AABABBA" and k = 1, you may replace at most one character with any other letter. What is the length of the…