Highest Engagement Path Sum in a Binary Recommendation Tree
Last updated: November 29, 2026
Quick Overview
A binary tree models chained recommendation branches, where each node holds an engagement score that can be negative (for example, a skip penalty). Return the maximum sum along any path, where a path connects any two nodes through parent-child links and visits each node at most once.
TikTok
November 29, 20260
0
0 solved
A binary tree models chained recommendation branches, where each node holds an engagement score that can be negative (for example, a skip penalty). Return the maximum sum along any path, where a path connects any two nodes through parent-child links and visits each node at most once.
TikTok onsite coding rounds often include a hard tree problem where the trick is separating what a subtree returns to its parent from what it records as a global answer. Interviewers check negative-value handling and whether you can explain that split clearly.
What the Interviewer Expects
- Distinguishes the downward gain returned to the parent from the best path through a node
- Clamps negative child gains to zero
- Updates a global best that allows the path to bend at the current node
- Handles all-negative trees by returning the largest single node
- Gives O(n) time and discusses recursion depth
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return the nodes on the best path?
- What if the path must start at the root?
- How would you handle a tree too deep for recursion?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Node:
value,left,right; values can be negative. - Output: maximum path sum over non-empty paths.
- Tree is non-empty....
High-Level Design
For each node compute gain(node): the best sum of a path that starts at this node and goes down one side. That is node.value + max(0, gain(left), gain(right)). A negative child gain is dropped bec...