Trim a Binary Search Tree
A medium-tier problem at 66% community acceptance, tagged with Tree, Depth-First Search, Binary Search Tree. Reported in interviews at josh technology and 0 others.
Trim a Binary Search Tree hits medium difficulty but feels harder because the naive approach wastes time on recursion that doesn't pay off. You're given a BST and two boundaries, and you need to return a subtree that contains only nodes within the range. Josh Technology reportedly asks this. The trick isn't complex once you see it, but missing the pattern during a live assessment means rebuilding logic under pressure. If you blank on the recursive contract, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Trim a Binary Search Tree"
Trim a Binary Search 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. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE.
Get StealthCoderThe key insight: a BST's structure lets you prune entire branches without traversing them. If a node's value is below the low boundary, everything in its left subtree is definitely too small, so you only need to trim its right side. Conversely, if the node is above the high boundary, skip the right and trim the left. The pitfall is thinking you need to visit every node. Candidates often write DFS that explores both subtrees even when one is provably out of range, burning time and complicating the logic. The actual solution is a recursive trim function that handles three cases at each node, returning the trimmed subtree. During the live assessment, if the tree traversal logic tangles, StealthCoder executes a tested solution, letting you move on.
Pattern tags
You know the problem.
Make sure you actually pass it.
Trim a Binary Search Tree recycles across companies for a reason. It's medium-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. Made by a working Amazon engineer who got tired of watching qualified friends bomb OAs they'd solve cold in an IDE. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Trim a Binary Search Tree interview FAQ
Is this really a medium, or does it feel harder?+
The 66% acceptance rate suggests most who see the pruning insight solve it cleanly. If you think you must visit every node to decide what stays, it gets messy fast. Once you accept that BST ordering lets you skip branches entirely, it's straightforward recursion.
What's the common mistake?+
Recursing on both left and right children regardless of the node's value relative to the boundaries. This creates unnecessary work and often leads to off-by-one errors when deciding inclusion. The fix: prune the branch that's guaranteed out of range before recursing.
How does this relate to binary search tree properties?+
The entire solution leans on the BST invariant: left subtree values are smaller, right subtree values are larger. Without this guarantee, you'd have to check every node. The boundaries let you confidently discard entire subtrees, making the algorithm efficient.
Is this still asked at companies like Josh Technology?+
Yes. It's a medium-tier interview question that tests whether you understand BST structure well enough to exploit it for optimization, not just traverse it blindly. Companies use it to separate candidates who think through constraints from those who code from muscle memory.
What should I practice alongside this?+
Practice other BST problems involving Depth-First Search and binary tree traversal, especially ones where you delete or filter nodes. This problem is a natural step after basic BST insertion and search, and a precursor to harder tree reconstruction challenges.
Want the actual problem statement? View "Trim a Binary Search Tree" on LeetCode →