Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional
Problem: For the binary tree with root value 2 and children: 2 has left child 3 and right child 9; 3 has left child 1 and right child 5; 9 has right child 7; 5 has left child 4. Using the max-path-sum definition (a path may start and end at any node, go up at most once), what is the maximum path sum?
- A. 30
- B. 31
- C. 18
- D. 26
Correct answer: A. 30
All values are positive, so the best path is the longest leaf-to-leaf chain 4 → 5 → 3 → 2 → 9 → 7 = 30. Adding every node (including 1, which would need a branch) gives 31, the best root-to-leaf path is 2 + 9 + 7 = 18, and stopping at 5 instead of 4 gives 26.
More Coding Interview Problems questions
- Problem: The string "banana" has its longest repeated substring (a substring occurring at least twice, overlaps allowed) of length L. What…
- Problem: How many subsequences equal to the string "abc" are contained in "aabbcbcac" (a subsequence picks positions in increasing order…
- Problem: The string "aabcccccaaa" is run-length encoded (each run becomes its letter followed by its run length, e.g. "a2b1..."). What is…
- Problem: Find the length of the shortest substring of "xaybzcxyabc" that contains all the letters a, b and c at least once.
- Problem: A Conway's Game of Life grid on a 4×4 board with alive cells at (1,0), (1,1), (1,2), (0,2) is advanced by one generation…
- Problem: A list of 6 customers arrives at a bank counter with service times 4, 1, 3, 2, 6, 5 (in that arrival order). The counter serves…