Reported September 2026
Amazontree

Construct a Tree from Level-Order and Inorder Traversals

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 whole problem hinges on a binary tree and a hash map, and Amazon reportedly served it in September 2026. You get a level-order array and an inorder array, and you have to rebuild the tree. If you've only memorized preorder plus inorder, this one will feel off for about ten minutes. The level-order array hands you the root first, but the subtrees are interleaved, so you can't just slice it. The pattern is tree construction with an inorder index map. If you blank mid-OA, StealthCoder runs invisibly as a safety net and gives you the working solution.

The problem

Given the levelOrder and inorder traversals of the same binary tree, reconstruct and return its root.
All node values are distinct. Both arrays contain the same values, and together they describe exactly one valid binary tree.

Function
buildTree(levelOrder: int[], inorder: int[]) → TreeNode

Examples
Example 1
levelOrder = [3,9,20,15,7]
inorder = [9,3,15,20,7]
return = [3,9,20,null,null,15,7]
The root 3 appears first in level order; the inorder split places 9 left and the remaining nodes right.
Example 2
levelOrder = [1]
inorder = [1]
return = [1]
Both traversals describe a one-node tree.
Example 3
levelOrder = [1,2,3,4,5]
inorder = [4,2,5,1,3]
return = [1,2,3,4,5]
The traversals reconstruct the shown complete upper levels.

Constraints
1 <= levelOrder.length == inorder.length <= 1200.
-10^9 <= levelOrder[i], inorder[i] <= 10^9.
Each traversal contains distinct values, and both contain the same set of values.
The arrays are valid traversals of one binary tree.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: inorder tells you which side of the root every value lives on, and level order tells you the order in which to pick roots. Build a hash map from value to inorder index. Recurse on an inorder range. At each call, you need the node from that range that appears earliest in level order. Simple approach: for each subtree, filter the level-order array to values in the inorder range, preserving order. The first element is the root. Split by its inorder index, then recurse left and right with the filtered lists. With n up to 1200, O(n^2) is fine. The pitfall is slicing level order by position like you would for preorder. That breaks, because left and right nodes are interleaved. Another pitfall is forgetting the empty range base case. If you freeze on the filtering step during the live OA, StealthCoder is the hedge that gets you unstuck.

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 Construct a Tree from Level-Order and Inorder Traversals 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 Amazon's OA.

Amazon 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.

Construct a Tree from Level-Order and Inorder Traversals FAQ

How hard is this Amazon OA question really?+

Medium. If you know the preorder plus inorder build, the idea transfers fast. The new part is that level order can't be sliced by position. Once you see that, the code is short. Expect most of your time to go to the filtering step and the edge cases.

What's the trick to solving it?+

Use inorder to decide left versus right, and use level order only to find each subtree's root. The root of any subtree is the first level-order value that falls inside that subtree's inorder range. A value-to-index hash map makes the split lookup constant time.

Is O(n^2) acceptable here?+

With length up to 1200, yes. Filtering level order per recursive call costs O(n) and you make n calls, so roughly 1.4 million operations in the worst case. You can do better with a set-based or queue-based build, but the simple version is safe and easier to get right.

Which edge cases should I test?+

Test a single node, a fully left-skewed chain, a fully right-skewed chain, and a node with only one child. Skewed trees stress your empty-range base case. Also confirm negative values and large magnitudes work, since you're keying a map on values.

How do I prepare in 48 hours?+

Rebuild the preorder plus inorder tree from scratch until it's automatic. Then write this variant once, using the filter-by-range idea. Practice tracing Example 1 by hand. Don't chase new topics. Tree construction with a hash map is the one pattern to lock in.

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