Decode an HTTP Chunked Transfer-Encoded Body With Trailers
Last updated: November 29, 2026
Quick Overview
Given the raw bytes of an HTTP/1.1 response body sent with chunked transfer encoding, reconstruct the original payload and return any trailer headers, ignoring chunk extensions and rejecting truncated or malformed input.
0
0
0 solved
Given the raw bytes of an HTTP/1.1 response body sent with chunked transfer encoding, reconstruct the original payload and return any trailer headers, ignoring chunk extensions and rejecting truncated or malformed input.
A systems-flavored Google onsite question in the style of what a front-end proxy (GFE-like) has to do. The interviewer checks careful byte-level parsing with a cursor, hex size parsing, exact CRLF validation, and robustness to truncated data from the network.
What the Interviewer Expects
- Walks the input with a single cursor rather than splitting on CRLF
- Parses hex chunk sizes and strips chunk extensions after a semicolon
- Copies exactly size bytes, so payloads containing CRLF survive
- Parses trailers after the zero-size chunk until an empty line
- Rejects truncated chunks and missing CRLFs with clear errors
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Make the decoder incremental: bytes arrive in arbitrary network reads.
- How would you cap memory if a malicious peer sends a huge chunk size?
- Why did HTTP/2 drop chunked encoding, and what replaced it?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
bytesin the formsize[;ext]\r\n data \r\n ... 0\r\n trailers \r\n. - Sizes are hexadecimal; extensions after
;are ignored. - Output: the concatenated payload and a dict of lowercase t...
High-Level Design
Splitting the whole body on \r\n is wrong because chunk data may itself contain CRLF. Instead keep an index pos. A helper reads one line up to the next CRLF for size lines and trailer lines only. ...