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)?
- A. 4
- B. 3
- C. 2
- 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
- Problem: Count the indices i of the string "abacaba" for which the suffix s[i:] is a palindrome.
- Problem: Deleting exactly one character from "aabca" produces some strings. How many distinct strings can result?
- Problem: Find the maximum sum of a strictly increasing subsequence of [1, 101, 2, 3, 100, 4, 5].
- Problem: How many subsets of {1, 2, ..., 8} contain no two consecutive integers (the empty set counts)?
- Problem: How many ways can the total 10 be formed as an unordered multiset of parts chosen from {1, 2, 3, 5} (repetition allowed)?
- Problem: A 3×3 matrix is [[2,1,3],[6,5,4],[7,8,9]]. A falling path moves from a cell in row r to a cell in row r+1 in the same column or…