FastPrepSerialize and Deserialize a Binary Tree
Problems/Serialize and Deserialize a Binary Tree

Serialize and Deserialize a Binary Tree

Bloomberg LP logoBloomberg LP● HardFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A mid-level software engineer's report from a Bloomberg interview names two tasks for a coding interview: a well-known island question, then a binary tree that must be turned into text and back, and that the whole process was a phone screen followed by an on-site day. It gives no statement, text format, limits or example, and it does not say which round asked the tree question.

Everything else here is our practice choice. The candidate picks the text format, so one function writes the tree to a string in the candidate's own format, rebuilds a tree from that string alone and returns the rebuilt tree. The judge compares the returned tree with the input tree: same shape and same values. The tree has 0 to 1000 nodes, no downward path has more than 500 nodes, and the values are whole numbers from -1000 to 1000 that may repeat.

The judged task matches the report at about 75%.

Design your own way to turn a binary tree into a string, and to rebuild the tree from that string.

In one function, do both steps. First write an encoder that turns root into a string in any text format you like. Then write a decoder that builds a new tree from that string alone: it must not look at root. Return the tree the decoder builds.

The returned tree must have exactly the same shape and the same node values as root. The empty tree must also come back as the empty tree. The checker sees only the returned tree, not your string, so the format is up to you.

Function

serializeAndDeserialize(root: TreeNode) → TreeNode

Examples

Example 1

root = [5,2,9,null,4,7]return = [5,2,9,null,4,7]
root
52497
→
return
52497

The tree has root 5. Its left child 2 has only a right child 4. Its right child 9 has only a left child 7. One possible encoding writes the nodes in preorder (node, left subtree, right subtree) with # for an empty child: 5,2,#,4,#,#,9,7,#,#,#. Reading that string back rebuilds the same tree, so the returned tree is the same as the input.

Example 2

root = []return = []
root
empty
return
empty

The empty tree can be encoded as #, and decoding gives the empty tree back. The result is the empty tree.

Example 3

root = [7,7,null,7]return = [7,7,null,7]
root
777
→
return
777

Three nodes all hold 7: the root, its left child, and that child's left child, a chain going left. An encoding that kept only the values would be 7,7,7 and could not tell this chain from other shapes. With empty-child markers it is 7,7,7,#,#,#,#, which rebuilds exactly this tree.

Example 4

root = [-1,null,-2,null,-3]return = [-1,null,-2,null,-3]
root
-1-2-3
→
return
-1-2-3

A chain going right with negative values. One possible encoding is -1,#,-2,#,-3,#,#. The minus signs and the comma separators must both survive the round trip, so the result is the same chain.

Constraints

  • The tree has between 0 and 1000 nodes.
  • -1000 <= Node.val <= 1000, and values can repeat.
  • No path from the root down to a leaf has more than 500 nodes.

More Bloomberg LP Phone Screen questions

  • N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
  • Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
  • Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
  • Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
  • Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • ClayWorkspace Resource TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
  • In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
  • Hierarchical In-Memory File SystemClay · PHONE SCREEN
  • Serialize and Deserialize a Binary TreeAmazon · ONSITE INTERVIEW
  • N-Ary Tree BFS CodecGoogle · ONSITE INTERVIEWGoogle interview questions
  • N-Ary Tree Codec OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Serialize and Deserialize a Binary TreeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Serialize and Deserialize an N-Ary TreeUber · ONSITE INTERVIEWUber interview questions
  • Recover a Tree From Preorder TraversalGrammarly · PHONE SCREEN
  • Design Search Autocomplete SystemRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions

Interview reports that mention this question

  • Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)

Bloomberg LP interview experiences

  • Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
  • Bloomberg SWE Intern Interview Questions: 68 Questions
  • Bloomberg Interview Questions, Level Not Reported: 65 Questions
  • Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
  • Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)
All Bloomberg LP interview questionsSee Bloomberg LP 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 TreeNode serializeAndDeserialize(TreeNode root) {
        // Write your code here.
    }
}
Checking account…