Recover Binary Search Tree
A medium-tier problem at 56% community acceptance, tagged with Tree, Depth-First Search, Binary Search Tree. Reported in interviews at Yahoo and 6 others.
You're staring at a Binary Search Tree where two nodes got swapped and broke the whole structure. It still looks like a tree, but in-order traversal won't be sorted anymore. Yahoo, Microsoft, Amazon, Bloomberg, TikTok, Apple, and Google all ask this. The trick isn't obvious on first read. Most candidates either brute-force it or miss the elegant recovery pattern. If you freeze during the live assessment, StealthCoder solves it invisibly and you move on.
Companies that ask "Recover Binary Search Tree"
Recover 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 FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know.
Get StealthCoderThe core pattern: in-order traversal of a valid BST is always sorted. When two values are swapped, the traversal breaks in one of two ways. Either the swapped nodes are adjacent in the sorted sequence (hard to spot), or they're far apart (obvious violation at two points). You track the previous value during DFS and flag where order breaks. Then you swap the bad values back and re-validate the tree structure. The gotcha is handling both cases in a single pass without building the entire sorted list first. Obvious approaches create O(n) space; the tight solution runs in O(h) space. If the inversion pattern isn't clicking during your OA, StealthCoder identifies which nodes to swap and patches your code in seconds.
Pattern tags
You know the problem.
Make sure you actually pass it.
Recover 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 FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Recover Binary Search Tree interview FAQ
Is this really a medium or is it sneaky-hard?+
The acceptance rate sits at 56%, which is on the lower end for medium. The algorithm itself isn't complex, but spotting which two nodes to swap and handling both swap-distance cases cleanly trips up a lot of candidates. It's a medium that feels like a hard.
Do I need to rebuild the tree or just identify the bad nodes?+
You identify the swapped nodes during in-order DFS traversal, then swap their values back. You don't rebuild anything. The tree structure itself is fine; only the values at two nodes are wrong. Recovery is a surgical fix, not a reconstruction.
How does this relate to the other Tree and BST topics I'm cramming?+
This is Depth-First Search applied to Binary Search Tree validation. It combines in-order traversal (Tree), DFS, and BST property checking. It's a capstone problem that forces you to understand how order constraints define a valid BST and how to exploit that constraint to find corruption.
Will I see this at Amazon or Google?+
Both companies report asking it. It's in the medium tier for backend and infrastructure roles. If you're grinding leetcode, this is worth drilling. If you blank on it during the OA, it's not a signal you're unqualified; it's a signal you hit an unlucky problem variant.
What's the space complexity trap?+
Naive approaches store the entire in-order list or build a recursive call stack for the whole tree. The optimal solution runs DFS with O(h) space and only stores two node pointers as you traverse. Many candidates solve it correctly but inefficiently, which costs them on follow-ups or tight time limits.
Want the actual problem statement? View "Recover Binary Search Tree" on LeetCode →