Reported December 2025
Zipdynamic programming

Maximum Nonadjacent Sum in a General Tree

Reported by candidates from Zip's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Zip OA. Under 2s to a working solution.
Founder's read

Zip reported this one in December 2025, and it's the House Robber problem wearing a tree costume. You get a rooted tree as a parent array, pick nodes with the biggest total value, and can't take a node alongside its direct child. If your OA lands in the next day or two, know this: it's a tree DP with two states per node. Nothing exotic. The parent-before-child ordering makes it even easier than it looks. And if you blank mid-assessment, StealthCoder runs invisibly on your desktop and can hand you the solution live.

The problem

Each node in a rooted tree has a nonnegative value. Choose any set of nodes with the largest possible total value, subject to one rule: a chosen node and its direct child cannot both be chosen. The tree can have any number of children per node.
The input nodes[i] = [value, parent] describes node i. Node 0 is the root and has parent -1. For every other node, its parent appears earlier in the array. Return the maximum total. Choosing no nodes is allowed.

Function
maxTreeSum(nodes: int[][]) → int

Examples
Example 1
nodes = [[5,-1],[4,0],[6,0],[7,1],[2,1],[3,2]]
return = 17
Choosing the root, both grandchildren of node 1, and the grandchild of node 2 gives 5 + 7 + 2 + 3 = 17.
Example 2
nodes = [[1,-1],[10,0],[10,0],[10,1],[10,2]]
return = 21
Choosing the root and both leaf grandchildren gives 21; choosing the two children gives 20.

Constraints
1 <= nodes.length <= 10000.
Each row contains exactly two integers: [value, parent].
0 <= value <= 1000.
nodes[0][1] == -1; for i > 0, 0 <= nodes[i][1] < i.
The answer fits a signed 32-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is two values per node: best sum if you take it, and best sum if you skip it. Take means value plus the sum of each child's skip. Skip means the sum over children of max(take, skip). The answer is max(take, skip) at the root. Since every parent index is less than its child index, you don't need recursion at all. Build the children lists or just iterate i from n-1 down to 1, adding each node's result into its parent's accumulators. That dodges stack overflow on a 10000-node chain, which is the classic pitfall here. The other mistake is greedy, like picking every other level. Example 2 shows why that fails. Initialize take[i] to value[i] and skip[i] to 0, then fold children upward. StealthCoder is the hedge if the DP recurrence slips away under the clock.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Maximum Nonadjacent Sum in a General Tree 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Zip's OA.

Zip reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Nonadjacent Sum in a General Tree FAQ

What's the trick to this Zip tree sum problem?+

Track two states per node: taken and skipped. Taken is its value plus the skipped totals of its children. Skipped is the sum of max(taken, skipped) across children. The root's max of the two is the answer. It's House Robber generalized to a tree.

Do I need recursion for this?+

No. Each parent index is smaller than its children's indices, so loop from the last node down to node 1 and push each node's results into its parent. It's iterative, O(n), and avoids recursion depth trouble on a 10000-node chain.

Why doesn't a greedy approach work?+

Taking the biggest value or alternating levels can lose to mixed choices. In Example 1 the best set mixes the root with deeper nodes from different branches. You have to compare take versus skip locally at every node, which is what the DP does.

How hard is this really?+

Medium. If you've seen House Robber III, it's the same idea with a general tree and an array input. The only new part is building or skipping the children structure. Most people stumble on the state definition, not the code.

How do I prepare in 48 hours?+

Write the two-state recurrence from memory, then code it on both examples by hand. Check a single node, a chain, and a star. Confirm you handle zero values and the root's parent of -1. That covers nearly every edge case this input allows.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Zip.

OA at Zip?
Invisible during screen share
Get it