Questions
19 of 26
1What is a Tree? Define Node, Root, Leaf, Edge, Height, Depth.
2What is a Binary Tree?
3What is a Binary Search Tree (BST)? What are its properties?
4What is the difference between a Binary Tree and a Binary Search Tree?
5Explain Tree Traversals: Inorder, Preorder, Postorder, Level Order.
6How do you implement Inorder traversal iteratively? (Without recursion)
7How do you find the Lowest Common Ancestor (LCA) of two nodes in a BST?
8How do you validate if a given Tree is a valid BST?
9What is a Balanced Tree? Why do we need balance?
10What is a Complete Binary Tree? A Full Binary Tree? A Perfect Binary Tree?
11What is a Heap? (Min-Heap and Max-Heap)
12How is a Heap implemented using an Array? (Parent/Child index math)
13What is the time complexity of insertion and deletion in a Heap? Why?
14What is Heapify? Explain the process.
15What is a Trie (Prefix Tree)? When is it used?
16How does a Trie compare to a Hash Table for string storage?
17What is an AVL Tree? What is a Red-Black Tree? What is the difference?
18Why do databases prefer B-Trees/B+ Trees over Binary Search Trees?
19How do you serialize and deserialize a Binary Tree?
20What is a Segment Tree? What problems does it solve?
21What is a Fenwick Tree (Binary Indexed Tree)?
22What is the difference between a B-Tree and a B+ Tree? Why are B+ Trees better for disk access?
23Explain the concept of Tree Rotation.
24What is a Splay Tree? When would you use it?
25How do you find the diameter of a Binary Tree?
26How do you check if a tree is symmetric (Mirror image)?
19 / 26

How do you serialize and deserialize a Binary Tree?

Difficulty: 5/10
preorder traversal, null markers, binary vs text format

Binary Tree Serialization

Serialization converts a tree into a sequence that can be stored or transmitted, while deserialization reconstructs the original structure. The critical requirement is preserving null-child information; otherwise different tree shapes can produce the same value sequence. Preorder traversal with explicit null markers is a common approach.

javascript
  1. 1

    Preorder plus null markers uniquely captures tree structure.

  2. 2

    Serialization time: O(n).

  3. 3

    Deserialization time: O(n).

  4. 4

    Serialized representation can be string, byte stream, or structured format.

  5. 5

    For production systems, format versioning and validation should also be considered.

Scenario Questions

0-2 years experience

  1. 1Write a function to serialize a binary tree to a string using preorder traversal with '#' for nulls. How would you then deserialize it back?
  2. 2If you have a tree where some nodes have only left children, what will your serialization output look like, and how does your deserialization handle it?
  3. 3What would happen if you forget to include a delimiter between node values in your serialized string?

2-5 years experience

  1. 1We need to store user session trees in a Redis cache. Which serialization format would you pick for a binary tree and why?
  2. 2During a recent release, deserialization started throwing an IndexOutOfBoundsException for certain trees. Walk me through how you'd debug the issue.
  3. 3If the tree can be very deep (depth > 10,000), how would you modify your serialization/deserialization to avoid stack overflow?

5-8 years experience

  1. 1Our service exchanges binary trees over the network at high QPS. Discuss the trade‑offs between a compact binary format vs a human‑readable text format for serialization.
  2. 2Design a version‑tolerant serialization scheme that allows adding new fields to tree nodes without breaking older services.
  3. 3How would you benchmark and optimise the throughput of your tree serialization pipeline under 1 GB/s traffic?

8+ years experience

  1. 1We are migrating a legacy monolith that uses a custom pointer‑based tree representation to a microservice architecture. How would you plan the migration of tree persistence while ensuring backward compatibility?
  2. 2Across multiple teams, different languages need to share the same tree data. What architecture would you propose for a language‑agnostic serialization contract, and how would you govern its evolution?
  3. 3If we need to support incremental updates (diffs) to large trees rather than full serialization each time, what design would you recommend?

Follow-up Questions

  • What edge cases would you test for this implementation?
  • How would you handle very large or unbalanced trees?
  • Can you compare the space overhead of a text‑based format versus a binary one?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.