Validate Binary Search Tree

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

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

The classic Validate Binary Search Tree question showed up in a SambaNova Systems OA reported in June 2022, and it punishes anyone who only checks a node against its direct children. That's the trap. The tree looks fine locally and still fails globally. If your invite is in the inbox, this is a tree problem with one clean idea behind it: carry bounds down the recursion. Get that right and it's ten lines. Miss it and you pass the sample, then fail hidden tests. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the idea below is small enough to memorize tonight.

The problem

Return whether the binary tree is a valid binary search tree. Every value in a left subtree must be strictly smaller than its ancestor, and every value in a right subtree must be strictly larger.

Function
isValidBST(root: TreeNode) → boolean

Examples
Example 1
root = [2,1,3]
return = true
Both children satisfy the strict bounds imposed by the root.

Constraints
The tree contains between 1 and 10000 nodes.
Node values fit in a signed 32-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is passing a valid range (low, high) down each call. Root has no bounds. Going left, the node's value becomes the new upper bound. Going right, it becomes the new lower bound. Every node must satisfy low < val < high, strictly. The naive pitfall is comparing only node.left.val < node.val < node.right.val, which accepts a tree where a right-subtree descendant is smaller than the root. The second pitfall is strictness: duplicates are invalid here. The third is bounds sentinels. Node values fit in a signed 32-bit integer, so using INT_MIN or INT_MAX as sentinels can collide with real values. Use null/None or Long/infinity instead. An in-order traversal that checks strictly increasing values also works. Complexity is O(n) time and O(h) space. If you freeze during the live OA, StealthCoder can surface this bounds approach in real time, so you just type it out cleanly.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Validate Binary Search Tree 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as validate 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 SambaNova Systems's OA.

SambaNova Systems reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Validate Binary Search Tree FAQ

What's the trick in Validate Binary Search Tree?+

Don't compare a node only to its children. Pass a (low, high) range down the recursion. Left child gets the parent's value as the new high, right child gets it as the new low. Every node must fall strictly between its bounds.

Why does the naive child-only check fail?+

It misses violations deeper in a subtree. A node in the right subtree can be smaller than the root while still being larger than its own parent. Only inherited bounds from all ancestors catch that case.

How do I handle duplicates and extreme values?+

The problem says strictly smaller and strictly larger, so equal values make the tree invalid. Since values span the full signed 32-bit range, don't use INT_MIN or INT_MAX as sentinels. Use null bounds or a wider type like long.

Should I use recursion or in-order traversal?+

Either works in O(n) time. Bounds recursion is the most direct. In-order traversal is also clean: track the previous value and fail if the current one isn't strictly greater. With up to 10000 nodes, a skewed tree makes recursion depth worth a thought.

How do I prepare for this in 48 hours?+

Write the bounds solution from scratch twice without looking. Then test it on a tree where a deep right descendant is smaller than the root, on duplicates, and on a single node. That covers the edge cases hidden tests usually target.

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

OA at SambaNova Systems?
Invisible during screen share
Get it