Reported May 2022
Bloombergtree

Maximum Sum BST in a Binary Tree

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

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

The edge case that kills naive solutions on Bloomberg's Maximum Sum BST question is the one hiding in plain sight: a subtree can look like a BST at its root while a deeper node breaks the rule. Bloomberg candidates reported this one in May 2022, and it's a tree problem that punishes anyone who checks BST validity from the top down. You need one clean postorder pass. Know it cold before your invite window opens. And if you blank mid-assessment, StealthCoder runs invisibly as a safety net and gives you the approach in real time.

The problem

Given the root of a binary tree, find the maximum sum of node values among all subtrees that are valid binary search trees.
A valid BST has every left-subtree value strictly smaller than its root and every right-subtree value strictly larger. Return 0 when every valid non-empty BST subtree has a negative sum.

Function
maxSumBST(root: TreeNode) → int

Examples
Example 1
root = [1,4,3,2,4,2,5,null,null,null,null,null,null,4,6]
return = 20
The subtree rooted at 3 is a BST with sum 20.
Example 2
root = [4,3,null,1,2]
return = 2
The leaf with value 2 is the best valid BST subtree.
Example 3
root = [-4,-2,-5]
return = 0
All valid BST sums are negative, so zero is returned.

Constraints
The tree contains between 0 and 5000 nodes.
-10000 ≤ Node.val ≤ 10000.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is postorder DFS that returns four things per node: is it a BST, the min value, the max value, and the sum. At each node, check that left max is less than the node value and right min is greater. If so, the sum is left plus right plus the node, and you update a global best. If not, mark it invalid and pass up sentinel bounds so every ancestor fails too. The pitfall is calling an isValidBST helper on every node, which turns it into O(n^2). Another trap is the negative case. Example 3 returns 0, so initialize the best at 0 rather than negative infinity. Empty children need min as infinity and max as negative infinity so they pass the checks. If you freeze on the return tuple during the live OA, StealthCoder is the hedge that reads the problem and hands you the structure.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Maximum Sum BST in a Binary Tree 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as maximum sum bst in binary tree. If you have time before the OA, drill that.

⏵ The honest play

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

Bloomberg reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Sum BST in a Binary Tree FAQ

What's the trick to Maximum Sum BST in a Binary Tree?+

Do a postorder traversal and return min, max, sum, and a validity flag from each node. Children get computed first, so you check the BST rule in constant time at the parent. One pass, O(n) total, no repeated validation.

Why does a naive solution fail on this problem?+

Checking only a node against its immediate children misses deeper violations. A left child can be smaller than the root while its right grandchild is larger than the root. You need the min and max of whole subtrees, not just child values.

Why does Example 3 return 0 instead of a negative number?+

The problem says to return 0 when every valid BST subtree has a negative sum. So seed your global max at 0 and only update when a valid subtree sum beats it. Don't seed with negative infinity.

How do I handle null children?+

Treat a null child as a valid BST with sum 0, min set to a huge value, and max set to a very small value. That makes the comparisons pass automatically for leaves, so you don't need special-case branching.

How should I prepare for this in 48 hours?+

Write the postorder tuple solution from scratch twice, then test it on the three given examples, especially the all-negative one. Also practice the same pattern on tree diameter or balanced tree checks, since they share the return-multiple-values structure.

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

OA at Bloomberg?
Invisible during screen share
Get it