FastPrepSum Nodes with an Even-Valued Grandparent
Problems/Sum Nodes with an Even-Valued Grandparent

Sum Nodes with an Even-Valued Grandparent

SambaNova Systems logoSambaNova Systems● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand SambaNova onsite report dated June 30, 2022 clearly describes the well-known problem of summing the nodes that have an even-valued grandparent. Our version is about 100% the same as the reported question.

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
root
67297148135

The eligible descendants sum to 18.

Example 2

root = [1]return = 0
root
1

The root has no grandparent.

Example 3

root = [2,1,3,4,5,6,7]return = 22
root
2145367

All four grandchildren have the even root as grandparent.

Constraints

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

More SambaNova Systems Onsite Interview questions

  • Vectorized K-Means AssignmentsONSITE INTERVIEW · Seen Apr 2024
  • 4Sum II Tuple CountONSITE INTERVIEW · Seen Jun 2022
  • Bomb Enemy Best CoordinatesONSITE INTERVIEW · Seen Jun 2022
  • Validate Binary Search TreeONSITE INTERVIEW · Seen Jun 2022
  • Deterministic Zigzag SortPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
  • Maximum Area of IslandONSITE INTERVIEW · Seen Jun 2022
  • Task Scheduler with CooldownPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
  • Point Inside a TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
  • Parse Variable, Expression, and Application TreesPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
  • Flip Equivalent Binary TreesONSITE INTERVIEW · Seen Oct 2021

Similar questions at other companies

  • Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
  • Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
  • Find Leaves of a Binary TreeLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
  • Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
  • JSON Path Lookup with Implicit Array WildcardsMongoDB · ONSITE INTERVIEWMongoDB interview questions
  • Delete and Compact a Forest SubtreePinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
  • Onion-Peel Layers of a Binary TreeeBay · ONSITE INTERVIEWeBay interview questions
All SambaNova Systems interview questionsSee SambaNova Systems hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int sumEvenGrandparent(TreeNode root) {
        // write your code here
    }
}
Checking account…