Data Structures & AlgorithmsHardProduct Company SDE · ICPC regional
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 value. Tree: root r=1 (value 3), children 2 (value 4) and 3 (value 5); node 2 has children 4 (value 1) and 5 (value 6); node 3 has child 6 (value 2). What is the maximum total value?
- A. 12
- B. 9
- C. 21
- D. 15
Correct answer: A. 12
With (take, skip) per node, the best is 12, reached both by the root 3 with nodes 4, 5, 6 (1 + 6 + 2) and by 5 + 6 + 1 without the root. Taking only the middle level gives 9, ignoring the constraint gives 21, and 4 + 5 + 6 = 15 forgets that node 5 is node 2's child.
More Coding Interview Problems questions
- 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?
- Problem: Find the lexicographically smallest rotation of the string "dcbadcb". What is its starting index (0-based) in the original…
- Problem: The longest common subsequence of "ABCBDAB" and "BDCABA" has length L. What is L?