Rebuild a BST of User IDs From Its Preorder Dump Alone
Last updated: November 29, 2026
Quick Overview
A BST of distinct user IDs was serialized in preorder. Reconstruct the tree from that list alone in O(n) time.
Meta
November 29, 20260
0
0 solved
A BST of distinct user IDs was serialized in preorder. Reconstruct the tree from that list alone in O(n) time.
A Meta technical-screen question about compact serialization. With a BST, preorder alone is enough because in-order is just the sorted order. Interviewers look for the O(n) upper-bound recursion rather than the O(n^2) split-search approach.
What the Interviewer Expects
- Explain why preorder suffices for a BST
- Give the naive O(n^2) split approach
- Improve to O(n) by passing an upper bound
- Consume the preorder with a single shared index
- Discuss recursion depth on skewed input
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Serialize a BST in the most compact form you can.
- Do it iteratively with a stack.
- What if the input preorder is not a valid BST preorder: how do you detect it?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list of distinct integers that is the preorder of some BST.
- Output: root of that BST.
- Target O(n) time....
High-Level Design
The first element is the root; the following elements smaller than it form the left subtree, then the rest form the right subtree. Finding the split by scanning costs O(n) per node, O(n^2) worst case....