Binary Tree: Subtree Sum, Maximum Path Sum, and Path Nodes
Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Uber asked this in February 2026: given a binary tree encoded as two arrays, compute three separate results on the same traversal. You need the total sum of all nodes, the maximum path sum (any path, root to leaf or internal), and the actual node sequence that forms that max path. The real trick is that you're doing three different tree walks in one pass. If you blank on the reconstruction step, StealthCoder will feed you the path-building pattern in real time.
The problem
Given a binary tree where each node stores an integer value, compute three results for the same tree. The tree is encoded with two arrays. values[i] is the value of node i. children[i] = [left, right] stores the child indices of node i, and -1 means that child does not exist. Node 0 is the root. Return a String[] of length 3 containing the total sum, the maximum path sum, and one maximum path as space-separated node values. The tree sum is 1 + 2 + 3 = 6. The best path is 2 -> 1 -> 3, which also sums to 6.
Reported by candidates. Source: FastPrep
Pattern and pitfall
This is a tree traversal problem that stacks three requirements. Total sum is trivial: DFS and add every node. Maximum path sum requires you to track the best path seen so far and compare paths at every subtree (a classic postorder DP pattern). The third part is the hedge: you must reconstruct and return the actual node indices that form that path. The common miss is not storing parent pointers or the path itself during traversal, then panicking when you have the sum but no way to trace it back. Use a helper function that returns both the max path sum and the path itself as a list. StealthCoder becomes critical here because path reconstruction logic often fails under pressure, and you'll have the structure right there if you freeze.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Binary Tree: Subtree Sum, Maximum Path Sum, and Path Nodes cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as binary tree maximum path sum. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Uber's OA.
Uber reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Binary Tree: Subtree Sum, Maximum Path Sum, and Path Nodes FAQ
Is this root-to-leaf paths only, or any path in the tree?+
Any path. The example shows 2 -> 1 -> 3, which is not necessarily root to leaf. You must compare all possible paths at every node during postorder traversal. This is the subtle part that catches people.
Do I have to return the path as node values or indices?+
The problem says 'space-separated node values' in the output string. So if the path is nodes 2, 1, 3, the output is '2 1 3'. Not indices. The encoding uses indices internally, but the result shows the actual integer values stored in those nodes.
What if multiple paths tie for maximum sum?+
The problem doesn't specify. Return any one of them. Most solutions just return the first maximum found during traversal. Uber's test cases likely have a unique maximum, so don't overthink it.
Can the maximum path be a single node?+
Yes. If all values are negative, the best path is the single node with the least negative value. Your code must handle the base case where a node is its own path.
How do I reconstruct the path without TLE on large trees?+
Store the path as a list inside your helper function. When you find a new maximum, do list.copy() to save it. Don't rebuild from scratch. This is O(n) per update but necessary for correctness. Your helper returns both the max sum and the path list.