Parse Server-Sent Events From a Chunked Byte Stream
Last updated: November 29, 2026
Quick Overview
Implement an incremental parser that receives arbitrary byte chunks from a streaming HTTP response and returns each complete Server-Sent Event (event name plus data) as soon as its terminating blank line arrives.
Anthropic
November 29, 20260
0
0 solved
Implement an incremental parser that receives arbitrary byte chunks from a streaming HTTP response and returns each complete Server-Sent Event (event name plus data) as soon as its terminating blank line arrives.
The Claude Messages API streams responses as Server-Sent Events, so client SDK and API engineers handle exactly this framing problem. Anthropic coding rounds favor practical, incrementally specified problems, and the interviewer watches whether you handle chunk boundaries that split lines, CRLF pairs and multi-byte UTF-8 characters rather than assuming each chunk is a whole event.
What the Interviewer Expects
- Keeps a buffer across feed calls instead of assuming chunks align with lines or events
- Handles \n, \r\n and bare \r line endings, including a \r\n pair split across two chunks
- Decodes UTF-8 incrementally so a character split across chunks is not corrupted
- Joins multiple data lines with newlines and ignores comment lines used as keep-alive pings
- Tests by feeding the same stream one byte at a time and comparing to a single-chunk feed
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
- How would you support the id and retry fields so a client can resume with Last-Event-ID after a dropped connection?
- The server sends an error event mid-stream after some content deltas. How should the SDK surface that to the caller?
- How do you bound memory if a malicious server never sends a newline?
- How would you expose this as an async iterator over typed events?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
feed(chunk: bytes) -> list[event] may be called with any split of the stream. A line ends with \n, \r\n or \r. A blank line dispatches the pending event if it has at least one data line. `ev...
High-Level Design
Two layers. An incremental UTF-8 decoder turns bytes into text and holds back an incomplete multi-byte sequence. A line splitter pulls complete lines out of a string buffer and hands each to a tiny st...