Reported May 2023
Airbnbgraph

Validate BST Node Descriptions

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

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

The edge case that kills most solutions on this Airbnb OA from May 2023 is a tree that passes the local parent-child check but breaks the BST rule three levels down. You're given rows of [value, left, right] and must return the root of one valid BST, or -1. It's a graph-building problem wearing a tree costume. Check structure first, then ordering. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you a working solution in real time, so one bad minute doesn't sink the attempt.

The problem

You are given an array nodes. Each row [value, left, right] describes one proposed binary-tree node:
value is the node's unique integer value.
left is the value of its left child, or -1 when it has no left child.
right is the value of its right child, or -1 when it has no right child.
Determine whether all rows together describe exactly one valid binary search tree. A valid description must have exactly one root, every non-root node must have exactly one parent, every referenced child must have its own row, every node must be reachable from the root, and the strict binary-search-tree ordering rule must hold for every descendant.
Return the root value when the description is valid. Otherwise, return -1.

Function
findValidBstRoot(nodes: int[][]) → int

Examples
Example 1
nodes = [[17,-1,-1],[15,13,17],[7,-1,-1],[13,-1,-1],[5,3,7],[3,-1,-1],[10,5,15]]
return = 10
The rows form one connected tree rooted at 10. Every value in its left subtree is smaller, and every value in its right subtree is larger.
Example 2
nodes = [[2,3,-1],[3,-1,-1]]
return = -1
Node 3 is described as the left child of 2, which violates the strict binary-search-tree ordering rule.

Constraints
1 <= nodes.length <= 10^5.
nodes[i].length == 3.
0 <= value <= 10^9, and all value entries are distinct.
Each child entry is -1 or an integer from 0 through 10^9.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is two passes. First, build a map from value to row and count parents. The root is the only node with zero parents. If there are zero or multiple roots, return -1. A child with two parents, or a child referenced but missing its own row, also means -1. Second, traverse from the root with min and max bounds, strictly exclusive, and count visited nodes. If the count doesn't equal nodes.length, something's disconnected or cyclic, so return -1. The classic pitfall is only comparing a node to its direct children. Example 2 hides that kind of failure. Another pitfall is recursion depth: with 10^5 nodes a skewed tree will overflow the stack in many languages, so go iterative with an explicit stack. StealthCoder is your hedge on the live OA if the bounds logic slips under pressure.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Validate BST Node Descriptions 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Airbnb's OA.

Airbnb reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Validate BST Node Descriptions FAQ

What's the actual trick in Validate BST Node Descriptions?+

Split it into structure and ordering. Find the single node with no parent, reject duplicate parents and missing child rows, then walk from the root carrying exclusive min and max bounds. Finally confirm the visited count equals the number of rows so nothing is left disconnected.

How do I find the root without being told?+

Collect every value that appears as a child. The root is the one row whose value never shows up as a child. If none qualifies, there's a cycle. If more than one qualifies, the description is a forest. Either way, return -1.

Why does checking only direct children fail?+

A node deeper in the left subtree can be larger than an ancestor while still being smaller than its own parent. Local checks pass, but the BST rule is violated. Passing down min and max bounds catches it because every descendant inherits the ancestor limits.

Should I use recursion or iteration here?+

Iteration. Constraints allow 10^5 nodes, and a chain-shaped tree makes recursion depth that large. Use a stack holding the value plus its lower and upper bounds. It's the same logic without a stack overflow risk in languages with small default recursion limits.

How do I prepare for this in 48 hours?+

Practice validating a BST with bounds until it's automatic. Then practice building a parent-count map and checking a visited count. Write the full solution once from scratch, including the edge cases: single node, missing child row, cycle, duplicate parent, and equal values on the wrong side.

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

OA at Airbnb?
Invisible during screen share
Get it