Interview conceptCoding & Algorithms

Binary Serialization And Codecs

Asked of: Software Engineer

Last updated

Top-to-bottom flowchart for designing binary-safe codecs: choose TLV vs length-prefix, add magic/version header, parser bounds-checks, streaming offset, round-trip tests, optional checksums.

What's being tested

These problems test reversible serialization: converting maps, strings, or KV-store state into bytes and reconstructing the exact original data. Interviewers probe whether you can design binary-safe codecs that handle arbitrary bytes, Unicode, empty values, large inputs, malformed payloads, and format evolution without relying on fragile delimiters.

Patterns & templates

  • Length-prefix encoding — write len(key) | key | len(value) | value; O(total_bytes) time, delimiter-free, handles arbitrary characters.

  • Fixed-width integer headers — use uint32 or uint64 lengths with explicit endianness; reject negative, overflowed, or truncated lengths.

  • Type-length-value (TLV) layout — encode type | length | payload for extensible records; useful for versioned KV entries and metadata.

  • Round-trip invariant — always test deserialize(serialize(x)) == x; include empty map, empty string, Unicode, null bytes, duplicates, and huge values.

  • Streaming parser — maintain an offset, bounds-check before every read, and fail fast on trailing bytes or incomplete records.

  • Versioned format header — prefix with magic | version | flags; enables backward-compatible changes and early rejection of invalid data.

  • Persistent mutation log — append PUT / DELETE records and replay into a map; optionally add checksums and compaction for robustness.

Common pitfalls

Pitfall: Using : or , delimiters breaks as soon as keys or values contain the delimiter; prefer length-prefixing or escaping with strict decoding.

Pitfall: Forgetting bounds checks lets malformed input panic, over-read, or allocate massive buffers; validate every declared length before slicing.

Pitfall: Treating strings as characters instead of bytes causes Unicode bugs; serialize UTF-8 bytes and define whether keys/values are str or raw bytes.

Practice these

The practice cards below cover the canonical variants — solve all of them and time yourself.

Featured in interview prep guides

Practice questions

Related concepts

Binary Serialization And Codecs — Tech Interview Concept | PracHub