HARDasked at 1 company

Maximum Sum BST in Binary Tree

A hard-tier problem at 44% community acceptance, tagged with Dynamic Programming, Tree, Depth-First Search. Reported in interviews at Zepto and 0 others.

Founder's read

Maximum Sum BST in Binary Tree is a hard problem that hits you with a two-layer constraint: find the largest sum of any valid binary search tree (BST) that exists as a subtree in the given binary tree. Zepto and other companies ask this when they want to see if you can track multiple properties simultaneously during tree traversal. The twist is that you can't just find the max sum, you have to verify the BST property holds for every candidate subtree. Most candidates get stuck trying to validate and compute in separate passes. The acceptance rate sits at 44%, which means half the room will blank on the pattern.

Companies asking
1
Difficulty
HARD
Acceptance
44%

Companies that ask "Maximum Sum BST in Binary Tree"

If this hits your live OA

Maximum Sum BST in Binary Tree is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.

Get StealthCoder
What this means

The trick is to compute four values for every node in a single DFS pass: whether the subtree is a valid BST, its min value, its max value, and its sum. You return all four, then at each node decide whether the current subtree qualifies as a valid BST. Only if both children are valid BSTs and the current node's value falls in the right range do you include its sum in the running maximum. The gotcha is handling invalid subtrees correctly, you can't just skip them, you have to carry a flag so parent nodes know the whole subtree is poisoned. If you try to validate BSTs by checking left less than root and right greater, you'll fail on cases where a left grandchild sneaks above the parent. StealthCoder solves this in seconds during a live assessment if you hit a wall on the state machine logic.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Maximum Sum BST in Binary Tree recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Sum BST in Binary Tree interview FAQ

What makes this harder than a normal 'find max sum subtree' problem?+

You have to validate the BST property while computing sums. A subtree can have a large sum but be invalid, so you need to track validity separately. You can't compute the answer until you've verified the constraint, which means four return values instead of one.

Why do most solutions fail this?+

They try to validate BSTs by only checking direct children (left < root < right) and miss constraints from grandchildren. Or they compute sums before confirming validity. The trick is a single post-order pass that builds up min, max, validity, and sum together.

Is this still asked at companies that hire for this role?+

Zepto reports it. The acceptance rate of 44% suggests it's not trivial, but it's stable. If you know the pattern of tracking multiple node properties in one DFS, you'll recognize it immediately.

How does this relate to the other tree topics listed?+

It uses DFS for traversal, Dynamic Programming because you build the answer bottom-up from subtree results, and Binary Search Tree knowledge for validation. They're not separate concerns, they're woven into one algorithm.

What's the time and space complexity?+

Time is O(n) because you visit each node once. Space is O(h) for the recursion stack, where h is the height. You don't need any external data structures if you manage the return values right.

Want the actual problem statement? View "Maximum Sum BST in Binary Tree" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.