FastPrepSerialize a Binary Search Tree
Problems/Serialize a Binary Search Tree

Serialize a Binary Search Tree

Confluent logoConfluent● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand Confluent onsite report asks for binary-search-tree serialization, with preorder as the expected form. It asks for a recursive solution and an iterative follow-up. Our version checks the serialization, which always gives the same output. Rebuilding the tree from limits is the codec follow-up that the report mentions. Our version is about 94% the same as the reported question.

You are given the root of a binary search tree with unique integer values. Serialize the tree as its preorder traversal, joining decimal values with commas and using no null markers.

Return the empty string for an empty tree.

As an interview follow-up, explain how to deserialize this representation in linear time by consuming preorder values under valid lower and upper bounds, and how to produce the preorder iteratively for a very deep tree.

Function

serializeBST(root: TreeNode) → String

Examples

Example 1

root = [2,1,3]return = "2,1,3"
root
213

Preorder visits the root, then the left subtree, then the right subtree.

Example 2

root = [8,3,10,1,6,null,14,null,null,4,7,13]return = "8,3,1,6,4,7,10,14,13"
root
831647101413

The output lists exactly the preorder traversal and does not emit null markers.

Example 3

root = []return = ""
root
empty

An empty tree serializes to the empty string.

Constraints

  • The tree contains at most 100000 nodes.
  • -1000000000 <= node.val <= 1000000000.
  • All values are unique and satisfy the binary-search-tree invariant.

More Confluent Onsite Interview questions

  • Implement tail -nPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Sudoku Board ValidationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Sudoku Puzzle SolverPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Sequential Phrase Search Across DocumentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Bounded Combination Sum with Signed CandidatesONSITE INTERVIEW · Seen Oct 2026
  • Detect a Silent SensorONSITE INTERVIEW · Seen Sep 2026
  • Build an Inverted Document IndexONSITE INTERVIEW · Seen Sep 2026
  • Combination Sum with Unlimited ReuseONSITE INTERVIEW · Seen Sep 2026
  • Word Search Across DocumentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Windowed Key-Value MapPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025

Similar questions at other companies

  • Filesystem Entity Total SizeGoogle · ONSITE INTERVIEWGoogle interview questions
  • Render a Comment HierarchyAmazon · ONSITE INTERVIEW
  • Kth Smallest Element in a Binary Search TreeSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Delete a Filesystem TreeDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
  • Flatten and Reconstruct an Ordered TreeThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
  • Inorder, Preorder and Postorder TraversalsAMD · ONSITE INTERVIEWAMD interview questions
  • Compare N-Ary Tree Leaf SequencesNavan · ONSITE INTERVIEWNavan interview questions
  • Flatten JSON into Repeated Key-Value PairsKlaviyo · ONSITE INTERVIEWKlaviyo interview questions
  • Kth Smallest Value in a BSTExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
  • Validate Binary Search TreeInMobi · ONSITE INTERVIEWInMobi interview questions

Interview reports that mention this question

  • Confluent Coding and System Design Questions: 33 Questions

Confluent interview experiences

  • Confluent Older Concurrency Rounds: 12 Experiences (Part 2 of 2)
  • Confluent Concurrency Interviews: 28 Experiences (Part 1 of 2)
  • Confluent SWE OA, Coding and Design Interviews: 19 Experiences
  • Confluent File Manipulation Round (tail -n): 19 Experiences
All Confluent interview questionsSee Confluent hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public String serializeBST(TreeNode root) {
        // Write your code here.
    }
}
Checking account…