Stop Sequences That Span Streamed Token Boundaries
Last updated: November 29, 2026
Quick Overview
While streaming generated text to a client, enforce a list of stop strings so that no character of a matched stop string is ever sent, even when a stop string is split across several decoded deltas. Emit everything else as early as possible.
Fireworks AI
November 29, 20260
0
0 solved
While streaming generated text to a client, enforce a list of stop strings so that no character of a matched stop string is ever sent, even when a stop string is split across several decoded deltas. Emit everything else as early as possible.
Inference APIs such as Fireworks accept a `stop` parameter and stream tokens, and tokenizers rarely align stop strings with token boundaries. The interviewer is checking whether the candidate holds back exactly the ambiguous suffix and no more, and handles the end of generation.
What the Interviewer Expects
- Hold back only the longest suffix that could begin a stop string
- Detect the earliest match across all stop strings
- Never emit text after a stop
- Flush held text when generation ends without a stop
- Discuss complexity in terms of stop string lengths
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 an Aho-Corasick automaton change the per-delta cost with many stop strings?
- Should the stop check run on detokenized text or token ids, and why?
- How do you report finish_reason and usage when a stop hits mid-token?
- What happens with stop strings that contain multi-byte characters split across tokens?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
push(delta) returns the text that is safe to send now. A string is safe if no stop string occurs in it and no suffix of it could be the start of a stop string completed by later deltas. Once any sto...
High-Level Design
Concatenate held text with the new delta. Search every stop string; if any occurs, cut at the smallest index. Otherwise compute the longest suffix that equals a proper prefix of some stop string, hold...