Serialize and deserialize a trie
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Snowflake's coding round, last asked Sep 2025. It tests tries, serialization and strings.
Design and implement serialization and deserialization for a trie (prefix tree) that holds a large dictionary of words. Each node has an end-of-word flag and child edges labelled by one character. The serialized form must be compact, must rebuild exactly the same trie (including which nodes end words), and must be safe to read when the input is broken.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
serialize(root: TrieNode) -> str(or bytes) -
deserialize(data: str) -> TrieNode, raising a clear error on malformed or truncated input.
To check your code, build a trie from a word list, round-trip it, and list the words of the rebuilt trie: they must equal the distinct input words.
Example
One possible format writes each node in pre-order as its end-of-word flag, its child count followed by ., then each child's character and subtree:
words ["car", "cat"] -> "01.c01.a02.r10.t10."
words ["a", ""] -> "11.a10." # the root itself ends the empty word
words [] -> "00."
round trip of ["cat", "car", "card", "dog"] -> words ["car", "card", "cat", "dog"]
Constraints
- Up to 10^4 words of up to 100 characters; start with lowercase
a–z. - Duplicate words appear once in the trie.
Clarifying questions
- What alphabet: lowercase only, or arbitrary Unicode?
- Is the output text or binary, and does size or speed matter more?
- Must the reader be able to stream the input without holding it all in memory?
- Will nodes later carry extra fields such as word frequency?
What a strong answer covers
- A format that captures structure explicitly: end-of-word markers and either child counts (length prefixes) or delimiters, and why counts avoid escaping.
- Pre-order DFS versus level-order BFS encodings: compactness, streaming, and how the reader rebuilds each.
- Escaping rules if a delimiter scheme is used and edge characters can collide with delimiters.
- Robustness: bounds-checked reads, rejecting truncated input, bad flags and trailing bytes, rather than returning a half-built trie.
- Forward and backward compatibility through a magic number and version header, or tagged optional fields.
-
O(N)time and output size, whereNis the number of trie nodes, and recursion depth bounded by the longest word.
Follow-ups
- Support Unicode edge labels (UTF-8, multi-byte characters).
- Add optional compression, and say where it pays off.
- Deserialize under a tight memory limit, or answer prefix lookups directly from the serialized bytes.
- Add a per-word frequency field without breaking old readers.
Where it's asked
- Snowflake · Coding · Last asked Sep 202560 min · Live coding; senior loops may agree a small storage component together, then build it
What it tests
tries · serialization · strings
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
