Incremental Parser for Streamed Chat Completion Events

Last updated: November 29, 2026

Quick Overview

Write a parser that receives arbitrary byte chunks from a server-sent events stream of chat completion deltas and returns the assembled text so far. Chunks can split lines, JSON objects and even multi-byte UTF-8 characters anywhere.

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

0

0

0 solved


Write a parser that receives arbitrary byte chunks from a server-sent events stream of chat completion deltas and returns the assembled text so far. Chunks can split lines, JSON objects and even multi-byte UTF-8 characters anywhere.

Every Baseten LLM deployment can stream tokens over SSE, and both the SDK and the gateway have to reassemble those streams correctly. The interviewer checks careful buffering and protocol handling rather than any clever algorithm.

What the Interviewer Expects
  • Buffers bytes until a full line is available instead of parsing each chunk directly
  • Decodes UTF-8 only on complete lines so split characters survive
  • Treats a blank line as the end of an event and joins multi-line data fields
  • Ignores comment lines used as keep-alives and stops at the [DONE] sentinel
  • Skips deltas without content, such as role-only or finish events
Key Topics to Cover
server-sent events
streaming
byte buffering
UTF-8
incremental parsing
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 surface a mid-stream error event from the server?
  • What changes if the server sends tool-call deltas whose JSON arguments arrive in fragments?
  • How do you measure time to first token and inter-token latency from this parser?
  • How would you resume a stream 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 is a sequence of bytes chunks. Events are separated by blank lines, each data: line carries JSON with choices[].delta.content, lines starting with : are comments, and data: [DONE] ends...

High-Level Design

Keep a byte buffer. After each chunk, peel off complete lines (split on \n, strip a trailing \r). Accumulate data values until a blank line, then dispatch the event. Decoding happens per complet...


Submit Your Answer
Markdown supported

Related Questions