Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: For the complete binary tree with 10 nodes stored in heap order (node i has children 2i and 2i+1, root is node 1), what is the sum of the depths of all nodes (root depth 0)?
- A. 19
- B. 29
- C. 22
- D. 9
Correct answer: A. 19
Node i has depth ⌊log₂ i⌋: one node at depth 0, two at 1, four at 2 and three at 3, so 0 + 2 + 8 + 9 = 19. Using 1-based depths adds 10 (29), assuming a full last level of four nodes gives 22, and 9 counts edges.
More Coding Interview Problems questions
- 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…
- 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)?