Computer science concepts
Serialization, Binary Encoding, And Persistent KV Stores
These problems test reversible serialization: designing encodings where arbitrary strings, bytes, maps, or KV entries can be serialized and deserialized…
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.