Skip to content

Design an algorithm to serialise and deserialise a binary tree.

Whether you can design a format, not just an algorithm, and whether you make the null markers explicit.

CodingHard

Who reports being asked this

DoorDash · LinkedIn · Microsoft · NVIDIA

Attested by 2 independent sources, including 3 first-hand reports, most recently around 2025-03.

What a strong answer does

The trap in this problem is assuming that a traversal of the real node values is enough. A preorder list like 1,2,3 only tells you the order in which nodes were visited, not which of those nodes were left children, right children, or leaves. Many different trees can produce the same value traversal. To make the representation reversible, the serialized data must include both the values and the shape of the tree. The usual way to encode the shape is to write an explicit marker whenever a child pointer is null.

All interview questions