FastPrepBinary Tree Maximum Path Sum
Problems/Binary Tree Maximum Path Sum

Binary Tree Maximum Path Sum

ByteDance logoByteDance● MediumFULLTIMEONSITE INTERVIEW

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report names a maximum-path-sum tree exercise and says it was a variant, without preserving the variation. This practice uses the conventional nonempty path between arbitrary tree nodes, including negative values, and an indexed finite encoding with explicit bounds. These choices are practice assumptions rather than reported variant details. Approximate source match: 75%.

Find a nonempty path in a binary tree with the largest sum of node values. Return that sum. A path uses parent-child edges, visits each node at most once, and may begin and end anywhere; it need not include the root.

For this exercise, use the conventional maximum-path-sum interpretation of the named tree exercise. The arrays values, left and right describe the nodes. Node 0 is the root. A child index of -1 means there is no child; any other child index identifies that child node. Inputs describe one valid connected binary tree. Return the sum as a signed 64-bit integer.

Function

maximumPathSum(values: int[], left: int[], right: int[]) → long

Examples

Example 1

values = [-10,9,20,15,7]left = [1,-1,3,-1,-1]right = [2,-1,4,-1,-1]return = 42

The best path is node 3 to node 2 to node 4, totaling 15+20+7.

Example 2

values = [-3]left = [-1]right = [-1]return = -3

A nonempty path is required even when every value is negative.

Example 3

values = [1,2,3]left = [1,-1,-1]right = [2,-1,-1]return = 6

The path through both children and the root has sum 6.

Constraints

  • The three arrays have the same length 1 <= n <= 200.
  • -10^6 <= values[i] <= 10^6.
  • Child indices are -1 or valid node indices. Node 0 is the root; every other node has exactly one parent.
  • The input is connected and acyclic. The path contains at least one node.

More ByteDance Onsite Interview questions

  • Search a Row- and Column-Sorted MatrixONSITE INTERVIEW · Seen Nov 2026
  • Alien DictionaryPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lexicographically Smallest Topological OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Implement Multi-Head AttentionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Dynamic Median with Additions and DeletionsONSITE INTERVIEW · Seen Oct 2026
  • Serialize and Deserialize a String Key-Value StoreONSITE INTERVIEW · Seen Oct 2026
  • Merge Overlapping IntervalsONSITE INTERVIEW · Seen Oct 2026
  • Tic-Tac-Toe Move WinnersONSITE INTERVIEW · Seen Oct 2026
  • Kth Largest Element without a HeapONSITE INTERVIEW · Seen Oct 2026
  • Least Recently Used CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Boolean Expression Results After Leaf FlipsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Transitive Employee Referral CountsRobinhood · ONSITE INTERVIEWRobinhood interview questions
  • Maximum Nonadjacent Sum in a General TreeZip · PHONE SCREEN · ONSITE INTERVIEWZip interview questions
  • Maximum Sum of Non-Adjacent Tree NodesInMobi · ONSITE INTERVIEWInMobi interview questions
  • Distance Sums to Special Nodes in a Weighted TreeNutanix · ONSITE INTERVIEWNutanix interview questions
  • File Encryption Tree OptimizationDatabricks · PHONE SCREEN · ONSITE INTERVIEWDatabricks interview questions
  • Binary Tree Maximum Path SumGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Binary Tree Maximum Path SumBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Binary Tree Maximum Path SumGlobalization Partners · ONSITE INTERVIEW
  • Binary Tree Maximum Path SumJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions

ByteDance interview experiences

  • ByteDance TikTok USDS SWE Intern Interview: 1 Experience
  • ByteDance MLE New Grad Interviews: 1 Experience
  • ByteDance Security SRE Intern Phone Screens: 1 Experience
  • ByteDance USDS New Grad SWE Phone Screen: 1 Experience
  • ByteDance New Grad Data Engineer Video Interviews: 1 Experience
  • ByteDance Trust & Safety Research Scientist Intern: 1 Experience
All ByteDance interview questionsSee ByteDance hiring insights
Saved
class Solution {
    public long maximumPathSum(int[] values, int[] left, int[] right) {
        // Write your code here.
    }
}
Checking account…