Closest Binary Search Tree Value II
A hard-tier problem at 60% community acceptance, tagged with Two Pointers, Stack, Tree. Reported in interviews at LinkedIn and 1 others.
Closest Binary Search Tree Value II hits hard because it forces you to find the k closest values in a BST without the luxury of a sorted array sitting in memory. You're juggling tree traversal, pointer management, and heap logic simultaneously. LinkedIn and Google ask this regularly, and it's the kind of problem where the naive approach (collect all, sort, pick k) works but gets rejected in real interviews. Your first instinct will be to do an in-order walk and dump everything into a list. That works for passing the test suite but signals you don't understand the constraint. If this problem blindsides you during a live assessment, StealthCoder runs invisibly and surfaces the optimal solution in seconds.
Companies that ask "Closest Binary Search Tree Value II"
Closest Binary Search Tree Value II 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 an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too.
Get StealthCoderThe trick is realizing you don't need to materialize the entire tree. Two Pointers on in-order sequences and a Heap (Priority Queue) are your tools. The standard flow: in-order traversal gives you values in sorted order, so you can use two pointers inching from the smallest and largest values toward your target, picking the closer one each step. If you can't avoid collecting all values, a max-heap of size k keeps memory flat. The catch is that tree structure actually helps here: DFS with controlled pruning beats brute force collection. Stack-based iteration (not recursion) lets you backtrack efficiently. The acceptance rate sits above 60%, but that includes solutions that work on test data but collapse under real interview scrutiny. The tie-breaker is understanding when to stop traversing branches. StealthCoder is your insurance if the heap logic or two-pointer coordination breaks down mid-interview.
Pattern tags
You know the problem.
Make sure you actually pass it.
Closest Binary Search Tree Value II 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. Made by an Amazon engineer who watched the leaked-problem repo become an industry secret. He decided you should have it too. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Closest Binary Search Tree Value II interview FAQ
Should I collect all BST values first, then pick the k closest?+
It passes small test cases but fails real interviews. You're not using the BST structure at all. Two Pointers on in-order values or a controlled Heap approach shows you understand the constraint. The problem exists precisely because naive collection is wrong.
Why is this rated HARD and not MEDIUM?+
The algorithm is straightforward, but the implementation is layered. You're combining DFS or iterative traversal, Heap logic or two-pointer coordination, and pruning decisions. Mistakes in any layer tank the solution. Most candidates get partway there.
Do I need recursion or can I use a Stack?+
Both work, but Stack-based iteration is safer under time pressure. You control exactly when you halt traversal, which is critical for efficiency. Recursion risks exploring branches you don't need.
How does this relate to the original 'Closest BST Value' problem?+
The single-value version lets you binary search the tree directly. Adding k values forces you to handle sorting or ranking. You lose the binary search shortcut and must commit to a traversal strategy instead.
Is this still asked at Google and LinkedIn?+
Yes. It appears in both companies' interview reports. The problem hasn't rotated out, likely because it separates candidates who understand tree optimization from those who brute-force everything.
Want the actual problem statement? View "Closest Binary Search Tree Value II" on LeetCode →