FastPrepReconstruct a BST from Preorder
Problems/Reconstruct a BST from Preorder

Reconstruct a BST from Preorder

Lyft logoLyft● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Lyft phone screen. The report asks to rebuild a binary search tree from its preorder walk, and says node values are unique. It does not give the function signature, the output format, examples or limits. Our version is about 94% the same as the reported question.

You are given an integer array preorder. It is the preorder traversal of a valid binary search tree (BST) whose values are all unique. Rebuild the tree, then return it as a string in the format below.

Write the tree in preorder: first the node's value in decimal, then its left subtree, then its right subtree. Write a missing child as #. Join all the pieces with commas and no spaces. Because every missing child is written out, the string shows the exact shape of the rebuilt tree.

Function

reconstructBST(preorder: int[]) → String

Examples

Example 1

preorder = [8,5,1,7,10,12]return = "8,5,1,#,#,7,#,#,10,#,12,#,#"

The root is 8. The values 5, 1, 7 are smaller, so they form its left subtree. The values 10, 12 are larger, so they form its right subtree. Writing # for every missing child gives the string shown.

Example 2

preorder = [1]return = "1,#,#"

The tree has one node with two missing children, so the answer is its value followed by # for the left child and # for the right child.

Example 3

preorder = [5,4,3,2,1]return = "5,4,3,2,1,#,#,#,#,#,#"

Each value is smaller than the one before it, so each node is the left child of the node before it. The tree is one chain going left, and the string lists the values in order followed by # for every missing child.

Constraints

  • 1 <= preorder.length <= 1000.
  • -10^9 <= preorder[i] <= 10^9.
  • All values are unique.
  • preorder is guaranteed to be the preorder traversal of a valid BST.

More Lyft interview questions

  • Minimum Window SubstringPHONE SCREEN · Seen Oct 2026
  • Deterministic K-Means AssignmentsPHONE SCREEN · Seen Oct 2026
  • Stateful Paginated Fetch NPHONE SCREEN · Seen Sep 2026
  • Validate Binary Search TreePHONE SCREEN · Seen Aug 2026
  • Keyboard Autocomplete SuggestionsONSITE INTERVIEW · Seen Nov 2026
  • Nested Transaction Key-Value Store with Value CountsONSITE INTERVIEW · Seen Sep 2026
  • Globally Versioned Key-Value StoreONSITE INTERVIEW · Seen Sep 2026
  • Rotten Oranges / Grid Infection BFSOA · Seen Aug 2026
  • Minimum Workers with Job AssignmentsONSITE INTERVIEW · Seen Jul 2026

Similar questions at other companies

  • Convert a Sorted Linked List to a Balanced BSTMeta · PHONE SCREENMeta interview questions
  • Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
  • Construct a Binary Tree from Preorder and InordereBay · PHONE SCREENeBay interview questions
  • Filter Data with Composable PredicatesCoreWeave · PHONE SCREEN
  • Trim a Binary Search Tree to a RangeGoogle · PHONE SCREENGoogle interview questions
  • Count Uni-Valued SubtreesAmazon · PHONE SCREEN
  • Rebuild a Binary Tree From Two WalksAdobe · PHONE SCREENAdobe interview questions
  • Delete a Node from a Binary Search TreeNVIDIA · PHONE SCREENNVIDIA interview questions
  • AVL Insertion and RotationsSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
  • Delete Node in a BSTBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions

Lyft interview experiences

  • Lyft MLE Intern Statistics Phone Screen: 1 Experience
All Lyft interview questionsSee Lyft hiring insights
Saved
class Solution {
    public String reconstructBST(int[] preorder) {
        // Write your code here.
    }
}
Checking account…