Incremental Server Sent Events Parser For Streaming Completions

Last updated: November 29, 2026

Quick Overview

Write a parser that receives arbitrary byte-boundary chunks of an OpenAI-compatible server-sent events stream and returns the text deltas as soon as each event is complete. It must handle comments, keep-alives, multi-line data fields and the terminating [DONE] marker.

Fireworks AI
Coding & Algorithms
Software Engineer
Fireworks AI
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

0

0

0 solved


Write a parser that receives arbitrary byte-boundary chunks of an OpenAI-compatible server-sent events stream and returns the text deltas as soon as each event is complete. It must handle comments, keep-alives, multi-line data fields and the terminating [DONE] marker.

Fireworks exposes OpenAI-compatible chat and completion endpoints with streaming, and SDK and gateway engineers deal with chunk boundaries that never line up with events. The interviewer looks for correct buffering and a clear sense of the SSE framing rules.

What the Interviewer Expects
  • Buffer partial input until a blank line ends the event
  • Join multiple data lines with newlines per the SSE spec
  • Ignore comment lines and keep-alive events
  • Stop cleanly on the [DONE] sentinel
  • Normalize CRLF line endings
Key Topics to Cover
Server-sent events
Streaming
Buffering
Parsing
OpenAI-compatible API
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you handle a chunk that splits a multi-byte UTF-8 character?
  • How would you surface an error event that arrives mid-stream?
  • What changes if the server sends usage statistics in the final chunk?
  • How would you make this parser resumable after a dropped connection?
Sharpen Your Skills on Codemia

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

Practice DSA Problems
Sample Answer
Requirements

Input arrives as text chunks of any size. An SSE event is a group of lines ending with a blank line. Lines starting with data: carry payload; lines starting with : are comments (servers often send...

High-Level Design

Keep a string buffer. Append each chunk, normalize \r\n to \n, and repeatedly split off everything before the first \n\n. For each complete event collect its data lines, join them with \n, the...


Submit Your Answer
Markdown supported

Related Questions