FastPrepMaximum Subtree Sum
Problems/Maximum Subtree Sum

Maximum Subtree Sum

Meta logoMeta● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Meta onsite interview. The report asks for the largest sum when each node is treated as the root of a subtree. It says this is different from a maximum-path problem. We chose that the root counts, the input is not empty, and the number limits. Our version is about 95% the same as the reported question.

Given a nonempty binary tree, consider each node as the root of a subtree. A subtree includes that node and all of its descendants in the original tree.

Return the largest sum among these subtree sums. You cannot omit a negative descendant, select only a path, or choose an empty subtree. Node values may be positive, negative or zero; the tree does not have to be a binary search tree.

Function

maximumSubtreeSum(root: TreeNode) → long

Examples

Example 1

root = [1,-2,3]return = 3
root
1-23

The root subtree sums to 2, the left subtree to -2, and the right subtree to 3. Return 3.

Example 2

root = [-5,-2,-7]return = -2
root
-5-2-7

Every subtree sum is negative. The left leaf gives the maximum, -2; an empty sum of 0 is not allowed.

Example 3

root = [10,5,5,-100,20,20,-100]return = 20
root
105-10020520-100

Each subtree rooted at a 5 sums to -75, and the whole tree sums to -140. A leaf with value 20 is best. Paths that omit negative descendants are not eligible.

Example 4

root = [1000000000,1000000000,1000000000]return = 3000000000

The whole tree sums to 3000000000, which exceeds a signed 32-bit integer.

Constraints

  • For this exercise, assume the tree contains between 1 and 2000 nodes.
  • -10^9 <= node.val <= 10^9.
  • Return a signed 64-bit integer. The total magnitude is at most 2 * 10^12.
  • The input is a valid binary tree, displayed in level order with null for missing children.

More Meta Onsite Interview questions

  • Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
  • Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
  • Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
  • Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
  • Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
  • Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
  • Maximum Stock Profit with Transaction LimitsONSITE INTERVIEW · Seen Aug 2026

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

Meta interview experiences

  • Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
  • Meta Business Support Engineer Phone Screen: 1 Experience
  • Meta Software Engineer Onsite Questions: 3 Experiences
  • Meta AI Native Engineer OA and Onsite: 1 Experience
  • Meta AI-Native Coding Interview: 3 Experiences
  • Meta Production Engineer Intern Questions: 6 Experiences
All Meta interview questionsSee Meta 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 long maximumSubtreeSum(TreeNode root) {
        // Write your code here.
    }
}
Checking account…