Property-Test a Protobuf-Style Varint Encoder for Round-Trip Invariants
Last updated: November 29, 2026
Quick Overview
Implement base-128 varint encoding and decoding as used in protocol buffers, then test it with properties (round trip, encoded length, continuation bits, stream concatenation, truncation) over random and boundary inputs rather than a few hand-picked examples.
0
0
0 solved
Implement base-128 varint encoding and decoding as used in protocol buffers, then test it with properties (round trip, encoded length, continuation bits, stream concatenation, truncation) over random and boundary inputs rather than a few hand-picked examples.
Protocol buffers are Google's lingua franca, and varints are a compact, bug-prone piece of them. In a technical screen the interviewer checks bit manipulation and, more importantly, whether the candidate can state invariants and generate tests that probe boundaries like 127 and 128.
What the Interviewer Expects
- Implements 7-bit groups with a continuation bit, least significant group first
- States round-trip and length properties precisely
- Mixes deterministic boundary values with seeded random inputs
- Tests streams of concatenated values and truncated input
- Rejects malformed input instead of looping or overflowing
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How do zigzag encodings make negative numbers cheap, and how would you test them?
- How would shrinking help when a random case fails?
- How would you fuzz the decoder with arbitrary bytes?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
encode_varint(n)for unsigned integers up to 64 bits.decode_varint(buf, pos)returns(value, next_pos)and raises on truncated or over-long input.- A test harness that checks properties ov...
High-Level Design
Encoding emits the low 7 bits of n each step, setting the high bit if more bytes follow. 300 is 0b100101100: low group 0101100 with continuation gives 0xAC, then 0b10 gives 0x02. Decoding ...