Construct Binary Tree from Preorder and Inorder Traversal
A medium-tier problem at 67% community acceptance, tagged with Array, Hash Table, Divide and Conquer. Reported in interviews at TikTok and 11 others.
TikTok, Tesla, Meta, and Amazon all ask this problem. You get two arrays: preorder and inorder traversals of the same binary tree. Your job: rebuild the original tree. It looks straightforward until you realize the obvious recursive approach tanks on large inputs if you don't optimize. The trick is understanding what each traversal tells you and using a hash table to eliminate the bottleneck. At 67% acceptance, most candidates either nail it or spend 20 minutes debugging. If this hits your live assessment and you blank on the pattern, StealthCoder solves it in seconds, invisible to the proctor.
Companies that ask "Construct Binary Tree from Preorder and Inorder Traversal"
Construct Binary Tree from Preorder and Inorder Traversal is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.
Get StealthCoderPreorder visits root first, then left, then right. Inorder visits left, then root, then right. The first element of preorder is always the root. Find that root in inorder: everything left of it is the left subtree, everything right is the right subtree. Recursively apply this to each subtree. The naive approach searches for the root in inorder on every call, which is O(n^2). The fix: hash all inorder values to their indices before recursing. Then each lookup is O(1), and the whole algorithm is O(n). Common mistake: forgetting to track array bounds correctly, or re-slicing arrays on every recursion. The divide-and-conquer framing is clean once you see it. If the recursive pattern doesn't click during your OA, StealthCoder handles the construction logic without you having to re-derive the bounds.
Pattern tags
You know the problem.
Make sure you actually pass it.
Construct Binary Tree from Preorder and Inorder Traversal recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Construct Binary Tree from Preorder and Inorder Traversal interview FAQ
Why does the naive O(n^2) solution fail at scale?+
Each recursive call searches for the preorder root inside the inorder array. Without a hash table, that's linear search per call. With n levels of recursion and n elements, you get O(n^2) time. On large trees, it hits time limits. The hash table fix reduces lookup to O(1), dropping total time to O(n).
Is this still being asked at big tech?+
Yes. TikTok, Meta, Amazon, Apple, and others report it frequently. It's not trendy, it's canonical. Companies like this problem because it tests whether you understand tree structure and can optimize beyond the first solution.
What's the core insight I need to remember?+
Preorder gives you the root. Inorder tells you which elements belong in left vs. right subtree. The root location in inorder is your partitioning point. Once you see that, recursion and a hash table do the rest.
How does this relate to the other tree topics on the input?+
It's pure binary tree construction. Hash Table is the optimization layer. Divide and Conquer describes the recursive strategy. Array is just the input format. If you're weak on any tree problem, this one surfaces gaps fast.
What's the most common mistake during the interview?+
Off-by-one errors when tracking array indices, or forgetting to hash inorder before recursing. Some candidates also try to reconstruct in-place and lose track of boundaries. Write out a small example (3 nodes) before coding to anchor your index logic.
Want the actual problem statement? View "Construct Binary Tree from Preorder and Inorder Traversal" on LeetCode →