Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 D
Problem: In a binary tree with 7 nodes in level order [10, 5, 15, 2, 7, 12, 20] (missing children are shown as absent, so the array encodes a complete tree), compute the sum over all edges of the smaller of the two subtree sizes below that edge.
- A. 32
- B. 17
- C. 10
- D. 6
Correct answer: C. 10
Cutting a root edge leaves a 3-node subtree (smaller side 3) and cutting each of the four leaf edges leaves 1, so the sum is 3 + 3 + 1 + 1 + 1 + 1 = 10. Taking the larger side gives 4 + 4 + 6·4 = 32, summing every node's subtree size including the root gives 17, and 6 just counts edges.
More Coding Interview Problems questions
- Problem: Three sorted rows are [1, 5, 9, 14], [2, 6, 10, 15] and [3, 7, 11, 16]. Merging all rows into one sorted sequence, what is the…
- Problem: What is the length of the longest substring of "abcabcbb" with all characters distinct?
- Problem: The shortest palindrome obtainable by inserting characters only at the front of "abacd" has what length?
- Problem: How many distinct palindromic substrings does "abacaba" contain (single characters count as palindromes, and distinct strings…
- Problem: Among all distinct permutations of the letters A, B, C, D, arranged in dictionary order starting from rank 1, what is the rank of…
- Problem: For the words ["flowchart", "flow", "flight", "flower"], what is the length of their longest common prefix?