Reported September 2026
Googletree

Trim a Binary Search Tree to a Range

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

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

Google reported this one in September 2026, and the detail that matters is in the statement: a retained descendant may become the new root. That's the whole question. You get a BST and an inclusive range [low, high], and you have to cut out everything outside it without breaking the structure. It's a tree recursion problem wearing a BST costume, with up to 10^5 nodes. If the OA invite is sitting in your inbox, learn the three-case recursion below. StealthCoder is the safety net on the live OA if your mind goes blank, but this one is short enough to own.

The problem

Given the root of a binary search tree and an inclusive range [low, high], remove every node whose value lies outside the range.
The relative structure of retained nodes must remain unchanged. Return the root of the trimmed tree; a retained descendant may become the new root.

Function
trimBST(root: TreeNode, low: int, high: int) → TreeNode

Examples
Example 1
root = [1,0,2]
low = 1
high = 2
return = [1,null,2]
Node 0 is below the interval and is removed.
Example 2
root = [3,0,4,null,2,null,null,1]
low = 1
high = 3
return = [3,2,null,1]
The branch rooted at 4 is above the interval. The valid descendants from the left branch remain attached in BST order.
Example 3
root = [0,null,1]
low = 1
high = 2
return = [1]
The original root is too small, so its retained right child becomes the new root.

Constraints
0 <= number of nodes <= 10^5.
-10^9 <= node.val, low, high <= 10^9.
low <= high, and all node values are distinct.
The input is a valid binary search tree.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is using the BST ordering to skip work. At each node, there are three cases. If node.val < low, the node and its entire left subtree are out, so return trim(node.right). If node.val > high, the node and its right subtree are out, so return trim(node.left). Otherwise the node stays, and you set left = trim(left) and right = trim(right). Return the node. The common pitfall is trimming both children no matter what, or forgetting that the returned subtree replaces the node, which is how Example 3 gets a new root. Another pitfall is recursion depth. With 10^5 nodes and a skewed tree, recursion can overflow in some languages, so know the iterative version. Time is O(n), space is O(h). If you blank during the live OA, StealthCoder can supply the recursion while you read the cases.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Trim a Binary Search Tree to a Range 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as trim a binary search tree. If you have time before the OA, drill that.

⏵ The honest play

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

Google reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Trim a Binary Search Tree to a Range FAQ

What's the trick to Trim a Binary Search Tree?+

Use the BST property to discard whole subtrees. If a node is below low, everything in its left subtree is also below low, so return the trimmed right child. If it's above high, return the trimmed left child. Otherwise keep the node and recurse both sides.

How hard is this problem really?+

It's a medium on paper but short once you see the three cases. The code is about ten lines. Most failures come from not reassigning children to the recursive result, or from mishandling the case where the root itself gets removed.

Will deep trees break the recursive solution?+

They can. The constraints allow 10^5 nodes, and a fully skewed BST makes recursion depth 10^5. In languages with a small call stack that risks overflow. Know an iterative approach: first find a valid root, then fix left and right chains with a loop.

What's the time and space complexity?+

Time is O(n) worst case, since you may visit every node. Space is O(h) for the recursion stack, where h is the tree height. It's O(log n) for a balanced tree and O(n) for a skewed one. Say this out loud if asked.

How do I prepare for this in 48 hours?+

Write the recursive version from memory twice, then trace Example 2 by hand to see how the branch rooted at 4 vanishes. Then write the iterative version once. Also test empty trees and ranges that exclude every node, since the constraints allow zero nodes.

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

OA at Google?
Invisible during screen share
Get it