Reported September 2026
Amazontree

Lowest Common Ancestor in an Inserted BST

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

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

Amazon reported this one in September 2026, and it looks like a tree problem until you read it twice. The thing it really reduces to is a walk down a BST that you build from the insertion order, splitting where p and q diverge. If you've got an Amazon OA coming, this is the LCA-in-a-BST pattern with one twist: you don't get the tree, you get an array. Build it, then descend. It's a short problem with one real trap, and that's input size. StealthCoder is the safety net if you blank mid-assessment, but the logic below is small enough to hold in your head.

The problem

Build a binary search tree by inserting the distinct values of insertionOrder from left to right. For each insertion, values smaller than the current node go left and larger values go right.
Return the value of the lowest common ancestor of the nodes containing p and q. A node may be an ancestor of itself.

Function
bstLowestCommonAncestor(insertionOrder: int[], p: int, q: int) → int

Examples
Example 1
insertionOrder = [6,2,8,0,4,7,9,3,5]
p = 2
q = 8
return = 6
The two nodes lie on opposite sides of the root, so 6 is their lowest common ancestor.
Example 2
insertionOrder = [6,2,8,0,4,7,9,3,5]
p = 2
q = 4
return = 2
The node containing 2 is an ancestor of the node containing 4.
Example 3
insertionOrder = [5]
p = 5
q = 5
return = 5
A node is its own lowest common ancestor.

Constraints
1 <= insertionOrder.length <= 100000.
All values in insertionOrder are distinct integers.
p and q both appear in insertionOrder.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that you don't need a general LCA algorithm. In a BST, start at the root. If both p and q are smaller than the current value, go left. If both are larger, go right. The first node where they split, or where the node equals p or q, is the answer. You can even skip building the tree: simulate insertion and track the path. The pitfall is constraints. With up to 100000 distinct values, a sorted or near-sorted insertionOrder makes a skewed tree, so recursion can blow the stack. Use iterative insertion and an iterative descent. Time is O(n) worst case on a skewed tree, O(n log n) on a balanced one. Also handle p equals q, which returns that value. If you freeze during the live Amazon OA, StealthCoder can run invisibly and hand you the iterative version, but you should be able to write it from this paragraph.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Lowest Common Ancestor in an Inserted BST 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as lowest common ancestor of a 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 Amazon's OA.

Amazon reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Lowest Common Ancestor in an Inserted BST FAQ

What's the trick to this Amazon BST LCA problem?+

Build the BST from insertionOrder, then walk down from the root. If p and q are both smaller than the current node, go left. If both are larger, go right. The first node that splits them, or equals one of them, is the LCA. No general tree LCA needed.

Do I need to actually build the tree?+

Yes, or simulate it. The shape depends on insertion order, so you can't just use the sorted array. Insert iteratively with left and right child arrays or node objects, then run the descent. Building costs O(n) on average and O(n^2) in the worst skewed case if done naively.

Why does the input size of 100000 matter here?+

Sorted or nearly sorted insertion order creates a chain, depth up to 100000. Recursive insert or recursive LCA can overflow the stack. Write both the insertion and the descent as loops. That's the one pitfall that fails otherwise correct solutions.

What edge cases should I test before submitting?+

Test p equals q, which returns that value. Test p as an ancestor of q, like the 2 and 4 example. Test a single-element array. Test a fully skewed ascending array with p and q near the bottom. Those four catch most wrong answers.

How do I prepare for this in 48 hours?+

Write the iterative BST insert and the iterative LCA descent from memory twice. Then run the three given examples by hand. That covers the whole problem. Spend leftover time on other tree traversals, since Amazon OAs often mix tree and array questions.

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

OA at Amazon?
Invisible during screen share
Get it