Reported July 2026
Goldman Sachsbinary tree

Validate Binary Search Tree

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

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

The mistake that sinks a first attempt on this Goldman Sachs OA, reported in July 2026, is checking each node only against its direct parent. Example 2 is built to punish exactly that. The tree is stored as aligned arrays with child indices and a root index, but it's still the classic Validate Binary Search Tree. Strict inequality means duplicates fail. If you've got the OA invite and 48 hours, learn the bounds trick below. StealthCoder sits invisible on your screen as a safety net if you blank mid-assessment, but this one is very learnable.

The problem

A binary tree is represented by aligned arrays. Node i has value values[i]; left[i] and right[i] contain child indices, or -1 when that child is absent. The index root identifies the root node.
Return true if the tree is a valid binary search tree and false otherwise.
For every node, every value in its left subtree must be strictly smaller and every value in its right subtree must be strictly greater. Therefore duplicate values make the tree invalid.

Function
isValidBST(values: int[], left: int[], right: int[], root: int) → boolean

Examples
Example 1
values = [2,1,3]
left = [1,-1,-1]
right = [2,-1,-1]
root = 0
return = true
The left value 1 is smaller than 2, and the right value 3 is greater.
Example 2
values = [5,1,4,3,6]
left = [1,-1,3,-1,-1]
right = [2,-1,4,-1,-1]
root = 0
return = false
Node value 4 lies in the right subtree of 5 but is not greater than 5.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to pass a valid range down the tree. Start at root with low = -infinity and high = +infinity. For node i, require low < values[i] < high. Go left with high = values[i]. Go right with low = values[i]. Strict comparisons on both sides handle the duplicate rule for free. The pitfall is comparing a node only to its parent. In Example 2, node 4 is greater than nothing it's compared to locally, since its children are fine, but it sits in the right subtree of 5 and is less than 5. A parent-only check passes it. Use long or null bounds so values at the int limits don't break the sentinel. An iterative stack or an in-order traversal that checks strictly increasing order also works. Recursion depth can hurt on a skewed tree, so mention the iterative option. If you freeze live, StealthCoder is the hedge, but the range-passing idea fits in five lines.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

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 for the candidate who got the OA invite this morning and has 72 hours, not six months.

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 Goldman Sachs's OA.

Goldman Sachs reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Validate Binary Search Tree FAQ

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

Carry a lower and upper bound down the recursion. Each node must fall strictly between them. Going left tightens the upper bound to the current value, going right tightens the lower bound. Checking only the parent fails cases like Example 2, where 4 sits under 5's right side.

How do duplicates change the answer here?+

They make the tree invalid. The problem says left must be strictly smaller and right strictly greater, so use strict inequalities on both bounds. If you use less-than-or-equal anywhere, a tree with equal values will wrongly return true.

Can I solve it with in-order traversal instead?+

Yes. An in-order walk of a valid BST yields strictly increasing values. Track the previous value and return false if the current one isn't greater. Equal values fail automatically. It's the same O(n) time, and easy to do iteratively with a stack.

How do the aligned arrays affect the code?+

You don't build node objects. Recurse on an index. For index i, read values[i], then go to left[i] and right[i]. A -1 means no child, so return true there. Start the call at the given root index, which may not be 0.

How should I prepare for this in 48 hours?+

Write the bounds solution from memory twice, once recursive and once with an explicit stack. Test Example 2, a duplicate-value tree, a single node, and a skewed chain. Use long bounds or nullable bounds to avoid integer limit bugs.

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

OA at Goldman Sachs?
Invisible during screen share
Get it