Build an Incremental Decoder for HTTP Chunked Transfer Encoding

Last updated: November 29, 2026

Quick Overview

Implement a streaming decoder for HTTP/1.1 chunked bodies that accepts bytes split at arbitrary boundaries, as they come off a socket, and rejects malformed input.

Apple
Software Engineering Fundamentals
Software Engineer
Apple
November 29, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Medium

0

0

0 solved


Implement a streaming decoder for HTTP/1.1 chunked bodies that accepts bytes split at arbitrary boundaries, as they come off a socket, and rejects malformed input.

Networking engineers at Apple work on stacks like CFNetwork and URLSession where parsers must be incremental and robust against hostile input. The interviewer checks protocol knowledge, a clean state machine, and defensive limits.

What the Interviewer Expects
  • Knows the format: hex size line with optional extensions, CRLF, data, CRLF, terminating zero chunk, trailers
  • Uses an explicit state machine that resumes when more bytes arrive
  • Handles splits anywhere, including inside the size line and the CRLF
  • Validates input and caps line length and chunk size to resist resource exhaustion
  • Tests by feeding one byte at a time
Key Topics to Cover
HTTP/1.1
state machines
streaming parsers
input validation
networking
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How would you avoid copying with memoryview or a ring buffer?
  • What security issues arise when a proxy and a server disagree on chunked parsing (request smuggling)?
  • How does HTTP/2 framing remove the need for chunked encoding?
  • How would you expose trailers to the caller?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements
  • feed(bytes) returns decoded body bytes available so far.
  • done becomes true after the zero-size chunk and the blank line ending the trailers.
  • Raise on non-hex sizes, missing CRLF after data, ...
High-Level Design

States: SIZE (read a line), DATA (copy up to remaining bytes), DATA_CRLF (expect exactly CRLF), TRAILER (read header lines until an empty one), DONE. The buffer keeps unconsumed bytes, and each stat...


Submit Your Answer
Markdown supported