Binary Serialization And Persistent Key-Value Stores
Asked of: Software Engineer
Last updated

What's being tested
Demonstrates designing reversible, delimiter-free binary serialization and a crash-consistent persistent key-value store: encoding arbitrary bytes/Unicode, managing offsets/indexes, and safe disk writes. Interviewers probe correctness across edge cases, complexity reasoning, and simple durability guarantees.
Patterns & templates
-
Length-prefix encoding: store a fixed-width (e.g., 4- or 8-byte) big-endian length before bytes; decode by reading length then exact payload, O(n) serialize/deserialize.
-
Varint / zigzag for compact integer lengths: saves space for small values; implement decode loop carefully to avoid infinite loops.
-
Append-only log + in-memory index: append records to file, keep
HashMap<key, offset>for O(1) get; rebuild index by scanning on startup. -
Per-record checksum (e.g.,
CRC32) after payload to detect partial writes or corruption before using a record. -
Atomic replace pattern: write to temp file,
fsyncdata and metadata, thenrenameto swap files atomically on POSIX. -
Compaction/GC: background pass copies live entries to new file, then atomic swap; avoid holding long-lived locks during compaction.
-
Edge-size handling: support empty keys/values and very large blobs by streaming IO and limiting in-memory buffers.
Common pitfalls
Pitfall: Using a byte delimiter (e.g.,
\0) fails for arbitrary binary data — always prefer length-prefix or escape-free encodings.
Pitfall: Forgetting to
fsyncmetadata (fsyncon directory afterrenameon some platforms) breaks durability guarantees on crash.
Pitfall: Rebuilding index by naive memory structures without bounds can OOM on millions of keys — consider sharding, sparse indexes, or on-disk B-tree.
Practice these
the practice cards below cover the canonical variants — solve all of them and time yourself
Practice questions
- Implement Persistent KV Store SerializationOpenAI · Software Engineer · Technical Screen · hard
- Implement map serialization and deserializationOpenAI · Software Engineer · Onsite · medium
- Implement delimiter-free string codecOpenAI · Software Engineer · Technical Screen · medium
- Parse and build binary data in PythonOpenAI · Software Engineer · Technical Screen · medium
- Implement KV store serializationOpenAI · Software Engineer · Technical Screen · medium
- Design a persistent key-value storeOpenAI · Software Engineer · Technical Screen · medium
- Implement KV store and plan type conversionsOpenAI · Software Engineer · Technical Screen · medium
- Implement in-memory KV store with serializationOpenAI · Software Engineer · Technical Screen · medium
- Implement persistent key-value storeOpenAI · Software Engineer · Technical Screen · medium
- Implement a serializable key-value storeOpenAI · Software Engineer · Technical Screen · medium
Related concepts
- Serialization, Binary Encoding, And Persistent KV StoresCoding & Algorithms
- Binary Serialization And CodecsCoding & Algorithms
- Persistent Key-Value StoresCoding & Algorithms
- Durable Key-Value Stores And CachesSystem Design
- Distributed Key-Value Storage And TransactionsSystem Design
- Distributed Key-Value StorageSystem Design