Interview conceptCoding & Algorithms

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 in O(total_bytes) time.

  • Binary integer packing — use int.to_bytes, int.from_bytes, or struct.pack; declare endianness like little-endian consistently.

  • Cursor-based parsing — maintain index i, check i + 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

Related concepts

Serialization, Binary Encoding, And Persistent KV Stores — Tech Interview Concept | PracHub