Build a Tree from Preorder and Inorder Traversals
Reported by candidates from StackAdapt's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The StackAdapt OA reported in September 2026 looks like a tree problem, but it really reduces to one lookup trick plus a careful output format. You get preorder and inorder traversals of a binary tree with distinct values, rebuild the tree, then print it in level order with # for missing children. If you've seen the classic construct-from-traversals problem, you're halfway there. The other half is trimming trailing # markers without breaking the middle ones. StealthCoder sits as a safety net on the live OA if you blank on the recursion or the serialization step, but the logic below is short enough to own tonight.
The problem
Reconstruct the unique binary tree described by the preorder and inorder traversals. All values are distinct. Return the constructed tree in level order as strings. Use # for a missing child and remove all trailing # markers. Return an empty array for an empty tree. Function buildTreeLevelOrder(preorder: int[], inorder: int[]) → String[] Examples Example 1 preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] return = ["3","9","20","#","#","15","7"] The root is 3, with leaf 9 and a right subtree rooted at 20. Example 2 preorder = [] inorder = [] return = [] Empty traversals construct an empty tree. Constraints 0 <= preorder.length <= 10000. Both arrays have equal length, contain the same distinct values, and describe a valid tree.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: preorder's first element is always the root. Find that value in inorder. Everything left of it is the left subtree, everything right is the right subtree. Build a hash map from value to inorder index up front so each lookup is O(1), giving O(n) total instead of O(n^2). Recurse with index bounds, not sliced arrays, or you'll copy lists and burn time at n = 10000. Keep a moving preorder pointer: build left first, then right. The common pitfall is the output. Do a BFS with a queue, push # for null children, then strip trailing # entries only at the end. Don't strip as you go. Also handle empty input by returning an empty array. Deep recursion on a skewed tree of 10000 nodes can overflow in some languages, so watch that. If the live OA rattles you, StealthCoder can supply the working solution.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Build a Tree from Preorder 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
This OA pattern shows up on LeetCode as construct binary tree from preorder and inorder traversal. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass StackAdapt's OA.
StackAdapt 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.
Build a Tree from Preorder and Inorder Traversals FAQ
What's the trick to the StackAdapt build-tree problem?+
Preorder gives you the root first, inorder tells you how many nodes sit in the left subtree. Map each value to its inorder index once, then recurse using index bounds. Build left before right so the preorder pointer advances correctly. That's the whole algorithm.
How hard is this one really?+
Medium. The reconstruction is a well-known pattern. The part that trips people is the level-order output with # markers and trailing trimming. Write the BFS carefully and test the skewed and single-node cases before submitting.
How do I handle the trailing # markers?+
Run BFS from the root, appending the value string for real nodes and # for null children. Don't enqueue children of null entries. After the traversal finishes, pop # entries off the end of the list until the last item is a real value. Interior # markers stay.
Will recursion blow the stack with 10000 nodes?+
It can on a fully skewed tree in languages with small default stack limits. Python is the usual victim. Either raise the recursion limit, or build iteratively with a stack. In Java or C++ the depth is typically fine, but know your environment.
How do I prepare in 48 hours?+
Write the reconstruction from memory twice, once with slicing and once with index bounds plus a hash map. Then write the BFS serializer separately. Test empty input, one node, left-skewed, and right-skewed trees. That covers nearly every failure mode for this question.