Sum Nodes with an Even-Valued Grandparent

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

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

The mistake that sinks a first attempt on this one is adding the grandparent's value instead of the grandchild's. SambaNova Systems candidates reported it in June 2022, and it's a clean binary tree traversal with a small twist. You sum every node whose grandparent exists and is even. Depth zero and one nodes never count. The pattern is depth-first search with parent and grandparent carried down. It's short, but people lose points on the details. If you blank during the live assessment, StealthCoder runs invisibly as a safety net and hands you the clean recursion.

The problem

Given a binary tree, return the sum of every node whose grandparent exists and has an even value.
A grandparent is the parent of a node's parent. Nodes at depth zero or one never contribute.

Function
sumEvenGrandparent(root: TreeNode) → int

Examples
Example 1
root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
return = 18
The eligible descendants sum to 18.
Example 2
root = [1]
return = 0
The root has no grandparent.
Example 3
root = [2,1,3,4,5,6,7]
return = 22
All four grandchildren have the even root as grandparent.

Constraints
The tree has 1 to 10000 nodes.
0 <= node.val <= 100.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to pass state down, not look up. Write a DFS that takes the node, its parent value, and its grandparent value. At each node, if the grandparent is even, add node.val to the total. Then recurse into both children with (node.val, parent) as the new (parent, grandparent). Use a sentinel like 1 for missing ancestors, since 1 is odd and never triggers. The pitfall is a sentinel of 0, because 0 is even and the constraints allow node values of 0. That silently inflates your sum on shallow nodes. Another miss is summing the grandparent's value instead of the descendant's. Example 3 checks this: the answer is 22, which is 4+5+6+7. Complexity is O(n) time and O(h) space. If the live OA freezes you, StealthCoder is the hedge that gives you the working version fast.

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 Sum Nodes with an Even-Valued Grandparent 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

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as sum of nodes with even valued grandparent. If you have time before the OA, drill that.

⏵ The honest play

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

SambaNova Systems 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.

Sum Nodes with an Even-Valued Grandparent FAQ

What's the trick to Sum Nodes with an Even-Valued Grandparent?+

Carry the parent and grandparent values down through DFS. At each node, check if the grandparent is even and add the current node's value. Then call the children with the current value as parent and the old parent as grandparent. No lookups, no parent pointers needed.

How hard is this one really?+

Easy to medium. The traversal is basic, but the details trip people up. You need the right sentinel for missing ancestors and you must add the correct node's value. If you've written a tree DFS before, you can finish it in about 10 minutes.

Should I use DFS or BFS?+

Either works. DFS with two extra parameters is the shortest code. BFS needs you to store parent info in the queue or a map, which is clunkier. For a tree of up to 10000 nodes, recursion depth could get large if it's skewed, so consider an iterative stack.

What edge cases should I test?+

A single node tree returns 0, as in Example 1's sibling case where the root has no grandparent. Test a node value of 0, since 0 is even and counts as an even grandparent. Also test a skewed tree and a tree where the root is odd.

How do I prepare for this in 48 hours?+

Write the DFS with parent and grandparent parameters from scratch twice. Then trace Example 3 by hand to confirm 22. Practice similar ancestor-tracking tree problems so passing state down feels automatic. Don't memorize code, memorize the shape of the recursion.

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

OA at SambaNova Systems?
Invisible during screen share
Get it