Data Structures & AlgorithmsVery hardProduct company OA
Each node of this binary tree holds a value; how many DOWNWARD paths (starting at any node, moving only to children, ending at any node) have a sum of exactly 8? Tree (parent: left, right): 10: 5, −3; 5: 3, 2; −3: –, 11; 3: 3, −2; 2: –, 1. Use the prefix-sum + hash-map method.
- A. 3
- B. 2
- C. 4
- D. 1
Correct answer: A. 3
Valid paths: 5 → 3, 5 → 2 → 1, and −3 → 11. The prefix-sum map counts, at each node, how many ancestors' running sums equal (current sum − 8). Counting only root-to-node paths finds none of them (they sum to 10, 15, 18, 21, 13, 17, 7, 18), and 3 → 3 + … paths reach 6 or 1, not 8.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- Weighted undirected graph: A–B 1, B–C 2, C–D 3, D–A 4, A–C 5, B–D 6. The MST is unique. What is the weight of the SECOND-best spanning…
- Word ladder: change one letter at a time, every intermediate word must be in the dictionary {cold, cord, card, ward, warm, word, worm…
- In this grid, how many islands of 1s are there with 4-directional connectivity, and how many with 8-directional (diagonals also connect)?…
- What is the maximum path sum in this binary tree, where a path joins any two nodes along parent–child links and need not pass through the…
- A binary trie over 4-bit numbers finds the maximum XOR of any pair from [3, 10, 5, 9, 2, 8]. What is the maximum XOR?
- In a binary tree whose root is at level 0, what is the maximum number of nodes at level 5?