Construct a Tree from Level-Order and Inorder Traversals
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole problem hinges on a binary tree and a hash map. Salesforce reported this one in September 2026: rebuild a tree from its level-order and inorder traversals, with distinct values and exactly one valid answer. If your OA invite is sitting there, this is a tree reconstruction problem, not a trick question. Level order hands you roots. Inorder tells you which side everything falls on. You combine them and recurse. If you blank mid-assessment, StealthCoder runs invisibly as a safety net and reads the problem on screen, but the idea is short enough to hold in your head.
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: the first element of level order is always the root. Find that root in inorder, and everything left of it is the left subtree, everything right is the right subtree. Build a hash map from value to inorder index so lookups are O(1). Then for each subtree, filter the level-order array to keep only values in that subtree's inorder set, preserving order. The first kept value is the subtree root. Recurse on both halves. The common pitfall is scanning for the root in inorder every call, which gives O(n^2) or worse. Another is forgetting to keep level-order relative order when you split. With n up to 1200, a filtered recursion is fine. A queue-based version with index ranges also works. If the recursion tangles on the live OA, StealthCoder is the hedge that gets you a clean working solution while you keep your head straight.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Salesforce's OA.
Salesforce reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Construct a Tree from Level-Order and Inorder Traversals FAQ
How hard is this Salesforce tree reconstruction problem really?+
Medium. It's the cousin of the classic preorder plus inorder build, with one twist: level order doesn't split into contiguous chunks. Once you see that you must filter by subtree membership, the rest is standard recursion and a hash map.
What's the core trick to solve it?+
Level order gives you the root first. Locate it in inorder via a hash map, split inorder into left and right, then filter the level-order array into two lists using the left and right value sets. Recurse on each pair and attach the results.
What time complexity should I aim for?+
The filtering approach is O(n^2) worst case on a skewed tree, which is fine for n up to 1200. You can tighten it with index ranges and sets, but don't over-engineer. A correct, clean solution beats a clever broken one.
What edge cases break most solutions?+
A single-node tree, fully skewed trees (all left or all right), and empty subtrees returning null. Also negative values and large magnitudes up to 10^9, so use the value as a map key, not an array index.
How do I prepare for this in 48 hours?+
Hand-trace Example 1 on paper until the split-and-filter step feels automatic. Then code it once from scratch with a hash map for inorder positions. Also skim the preorder plus inorder version, since the recursion shape is nearly identical.