FastPrepBuild a Tree of Subtree Sums
Problems/Build a Tree of Subtree Sums

Build a Tree of Subtree Sums

Snowflake logoSnowflake● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Turning a tree into a tree of subtree sums is about 90% the same as the reported question. This exercise comes from a collection of interview-prep questions, not from one dated interview. We chose these. The n-ary tree is a parent array. The output has the same shape. Each node includes itself. Values can be negative. We also set the limits and the examples.

You are given one rooted tree in which each node may have any number of children. Its node IDs run from 0 through n - 1. The array parent describes its edges: parent[v] is the parent of node v, and exactly one root has parent -1. Each node stores values[v].

Build a separate tree with the same node IDs and parent relationships. Its value at each node must be the sum of the original values of that node and all its descendants. Return the new values as an array in node-ID order. Do not change either input.

In this practice exercise, the original parent array and your returned values together describe the new tree. A parent ID may be larger or smaller than its child ID. Include the node itself when finding its subtree sum, and add negative values as usual.

Function

subtreeSums(parent: int[], values: int[]) → int[]

Examples

Example 1

parent = [-1,0,0,1,1]values = [5,2,-3,4,1]return = [9,7,-3,4,1]

The subtree at node 1 contains values 2, 4, and 1, summing to 7. The root sum is 9.

Example 2

parent = [2,2,-1,0]values = [1,-2,3,4]return = [5,-2,6,4]

Node 2 is the root even though it is not first. The new values retain the original node positions.

Example 3

parent = [-1]values = [-8]return = [-8]

A one-node subtree contains the node itself.

Constraints

  • 1 <= n = parent.length = values.length <= 1000.
  • -10^6 <= values[v] <= 10^6.
  • The parent array describes one valid rooted tree, with one -1 and no cycles.
  • Every other parent is an existing node ID different from its child. All subtree sums fit a signed 32-bit integer under these bounds.

More Snowflake Phone Screen questions

  • Minimum StackPHONE SCREEN · Seen Nov 2026
  • Design In-Memory File SystemPHONE SCREEN · Seen Nov 2026
  • Merge Two Binary TreesPHONE SCREEN · Seen Nov 2026
  • Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
  • Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
  • Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
  • Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
  • Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
  • Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026

Similar questions at other companies

  • Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
  • Binary Tree Spiral Level OrderAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
  • Tree DiameterQualcomm · PHONE SCREENQualcomm interview questions
  • Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions
  • Employee Importance from a DictionaryPatreon · PHONE SCREENPatreon interview questions
  • Distance Between Two Nodes in a Binary TreeBloomberg LP · PHONE SCREENBloomberg LP interview questions
  • Transform and Prune a Mode-Valued Binary TreeGoogle · PHONE SCREENGoogle interview questions
  • Time to Burn a Binary Tree From One NodeAdobe · PHONE SCREENAdobe interview questions
  • Team Cost Under a ManagerRippling · PHONE SCREENRippling interview questions

Snowflake interview experiences

  • Snowflake AI Coding Interview, Experienced Hires: 3 Experiences
All Snowflake interview questionsSee Snowflake hiring insights
Saved
class Solution {
    public int[] subtreeSums(int[] parent, int[] values) {
        // Write your solution here.
    }

}
Checking account…