Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: A rooted binary tree stored as a parent array over nodes 0–9: parent of node i is [-1, 0, 0, 1, 1, 2, 3, 3, 4, 5][i] (node 0 is the root). What is the lowest common ancestor of nodes 8 and 7?
- A. 2
- B. 0
- C. 1
- D. 3
Correct answer: C. 1
List the ancestors of one node, then walk up from the other until hitting that set. Reporting the parent of the first node instead of the first shared ancestor is a common slip.
More Coding Interview Problems questions
- Problem: A rooted tree of 8 nodes (root 1) is given by children lists: 1: [2, 3], 2: [4, 5], 3: [6], 4: [], 5: [7, 8], 6: [], 7: [], 8…
- Problem: The house-robber rule applies on a tree: you pick a set of nodes with no parent–child pair both chosen, maximising the total…
- Problem: The Z-function of a string s is defined as z[i] = the length of the longest common prefix of s and s[i:]; z[0] is conventionally…
- Problem: Count the distinct non-empty substrings of "abcabc" (two substrings are the same if their letters are identical, regardless of…
- Problem: How many windows of length 3 in the text "bacacbaab" are anagrams of the pattern "aab" (same letters with the same…
- Problem: What is the length of the longest substring of "eceebaabbccbdeeabe" that contains at most 2 distinct characters?