Reported May 2026
Amazonstack

Lowest Common Ancestor Implemented with Stack

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

The edge case that sinks the naive answer is when p is itself an ancestor of q, and Amazon's May 2026 report has it baked into the second example. This is Lowest Common Ancestor with a twist: the interviewer wants it iterative, with a stack and a parent map. The tree comes in as a level-order array of strings with "null" gaps, so you build it first, then climb. If you've got an Amazon OA coming and recursion is your reflex, you need a different script. StealthCoder sits invisible on your screen as a safety net if you blank mid-assessment.

The problem

This question was reported for Amazon Fall Intern onsite.
Given the root of a binary tree and two distinct nodes p and q in that tree, return their lowest common ancestor.
In this FastPrep version, the binary tree is provided as root, a level-order representation of the tree. Each non-null entry is an integer stored as a string, and "null" marks a missing child. The values p and q identify the two target nodes.
The lowest common ancestor of two nodes is the deepest node in the tree that has both p and q as descendants. A node may be considered a descendant of itself.
You should solve this problem iteratively. In particular, use a stack to traverse the tree and build a parent mapping for each visited node. Then use that parent information to find the first common ancestor of p and q.
A Note From OP
The original OP mentioned that the interviewer specifically required a stack-based solution. The approach they described was to use a stack to traverse the tree and build a parent map for every node. Then, starting from p, move upward to the root and store all ancestors in a set. Finally, starting from q, move upward toward the root; the first node that already appears in p's ancestor set is the lowest common ancestor.
Reference Choice
There are 3 "find lowest common ancestor" questions on LeetCode. 236 was selected as the reference because this source describes the LCA of exactly two existing nodes, p and q, in a normal binary tree.
It does not match 1644, because that variant changes the contract by allowing one or both target nodes to be missing from the tree. It also does not match 1676, because that variant asks for the LCA of multiple nodes, not just p and q.
Additional Practice Note
A super tiny interviewer-pattern prediction:::) if you are interested, you can also try solving the other two Lowest Common Ancestor variants with a stack-based approach. Given that this interviewer seems to be a fan of stacks, they may ask one LCA problem as the main interview question and use the other two LCA variants as follow-ups if time allows 😉

Function
lowestCommonAncestor(root: String[], p: int, q: int) → int

Examples
Example 1
root = ["10","5","15","3","7","12","18","null","4","6","8"]
p = 3
q = 8
return = 5
Node 5 is the lowest node that has both 3 and 8 in its subtree.
Example 2
root = ["10","5","15","3","7","12","18","null","4","6","8"]
p = 5
q = 8
return = 5
A node can be an ancestor of itself. Since 5 is an ancestor of 8, the lowest common ancestor is 5.

Constraints
2 <= root.length <= 10^5
Each non-null entry in root is an integer in the range [-10^9, 10^9].
All non-null node values are unique.
p and q are distinct.
Both p and q exist in the tree.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is the parent map. Traverse with an explicit stack, record parent[child] = node for every visited node, and stop once both p and q are seen if you want an early exit. Then walk from p to the root, dropping every ancestor into a set, and include p itself. Walk up from q and return the first node already in the set. Including the starting node is the pitfall. Skip it and example 2 returns the wrong answer, because 5 is p and also the LCA. The second pitfall is parsing: the level-order array with "null" markers needs a queue-based build, and values are strings, so convert them to integers before comparing with p and q. With up to 10^5 nodes, recursion risks depth trouble on skewed trees, which is probably why the stack is required. Everything runs in O(n) time and space. If the parsing step trips you during the live OA, StealthCoder is the hedge that gets you unstuck fast.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Lowest Common Ancestor Implemented with Stack 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 passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as lowest common ancestor of a binary 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 passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Lowest Common Ancestor Implemented with Stack FAQ

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

Build a parent map with an iterative stack traversal. Put p and all its ancestors, including p itself, into a set. Then climb from q and return the first node found in that set. Treating a node as its own ancestor is what handles the second example.

Why does the interviewer insist on a stack?+

It forces an iterative solution, which avoids recursion depth problems on skewed trees of up to 10^5 nodes. It also tests whether you can build parent pointers yourself instead of leaning on the call stack to track ancestry.

How do I parse the level-order input?+

Use a queue. Create the root from the first entry, then for each dequeued node assign the next two entries as left and right children, skipping "null". Convert the strings to integers. Then run your stack traversal on the built tree.

Is this the same as LeetCode 236?+

Yes, it matches 236: two distinct nodes that both exist in a normal binary tree. Variants 1644 (nodes may be missing) and 1676 (multiple nodes) change the contract, so they aren't the same problem, though they could come up as follow-ups.

How do I prepare in 48 hours?+

Write the parent-map solution from memory twice, once with a stack and once with BFS. Test it on p being an ancestor of q. Then practice building a tree from a level-order array with nulls. Those two pieces are where candidates lose time.

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