Serialization, Binary Encoding, And Persistent KV Stores
Asked of: Software Engineer
Last updated
What's being tested
These problems test reversible serialization: designing encodings where arbitrary strings, bytes, maps, or KV entries can be serialized and deserialized without ambiguity. Interviewers are probing careful handling of binary layouts, length prefixes, endianness, Unicode/bytes boundaries, and malformed input.
Patterns & templates
-
Length-prefix encoding — write
len(payload)then raw bytes; avoids delimiter escaping and supports arbitrary characters inO(total_bytes)time. -
Binary integer packing — use
int.to_bytes,int.from_bytes, orstruct.pack; declare endianness like little-endian consistently. -
Cursor-based parsing — maintain index
i, checki + n <= len(buf)before reads; return parsed object plus final offset. -
Map/KV serialization — encode count, then repeated key/value length-prefixed blobs; reject duplicate keys or define overwrite semantics.
-
Type/version headers — include magic bytes, format version, and optional type tags when future compatibility or mixed value types matter.
-
Robust error handling — detect short reads, negative/overflowed lengths, invalid UTF-8, trailing bytes, and unreasonable allocation sizes.
-
Round-trip testing — assert
deserialize(serialize(x)) == x; test empty strings, null bytes, Unicode, huge payloads, and randomized inputs.
Common pitfalls
Pitfall: Using delimiters like
","or"#"without escaping fails for inputs that contain the delimiter.
Pitfall: Confusing characters with bytes breaks Unicode; serialize encoded bytes, not Python string lengths.
Pitfall: Trusting lengths from input can cause out-of-bounds reads or massive allocations; validate before slicing.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Implement map serialization and deserializationOpenAI · Software Engineer · Onsite · medium
- Implement a persistent sharded key-value storeOpenAI · Software Engineer · Technical Screen · hard
- Implement delimiter-free string codecOpenAI · Software Engineer · Technical Screen · medium
- Design a persistent key-value storeOpenAI · 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
- 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
- Binary Serialization And CodecsCoding & Algorithms
- Persistent Key-Value StoresCoding & Algorithms
- Durable Key-Value Stores And CachesSystem Design
- Distributed Key-Value Storage And TransactionsSystem Design
- Caching And Stateful Data Structure DesignCoding & Algorithms
- Storage, Indexing, APIs, And Secure ExecutionSystem Design