BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / LLM inference and serving28 of 36 in topic

454. Stream text safely when stop sequences can span chunks

HardLLM inference and serving Pro
Asked atxAI

Why it matters Every streaming chat API implements stop-sequence handling, and leaking half a stop token like "<|e" to users is a common production bug.

When streaming, the server must never send part of a stop sequence to the client. If the text so far ends with "<|e" and the stop string is "<|end|>", those characters have to be held back until the next chunk shows whether the stop really arrived.

Write stream_with_stops(chunks, stops). chunks is a list of text pieces in arrival order; stops is a list of non-empty stop strings. Keep a buffer of held text. For each chunk, set buf = held + chunk.
- If any stop string occurs in buf, cut at the earliest occurrence (smallest start index), emit buf before it, and stop processing: return (emits, True).
- Otherwise find the largest k such that the last k characters of buf are a proper prefix (shorter than the full string) of some stop. Emit buf without those k characters and hold them.
emits gets one string per processed chunk (possibly empty). If the stream ends with no stop, append one final string with the held text and return (emits, False).

Functiondef stream_with_stops(chunks, stops)

Example 1

Input: chunks ["Hi <", "|e", "nd|> bye"], stops ["<|end|>"]

Output: (["Hi ", "", ""], True)

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Search the joined buffer, not just the new chunk, then hold the longest suffix that could still grow into a stop.

Hint 2

Keep held as a string between chunks.

Hint 3

Use str.find on held + chunk for each stop and take the smallest index.

Hint 4

For holdback, test buf.endswith(stop[:m]) for m from len(stop) - 1 down to 1.

Common mistakes
  • Only searching inside the newest chunk.
  • Emitting text that might be the start of a stop.
  • Forgetting to flush held text when the stream ends without a stop.
Understand the idea

Holdback is like a news anchor reading a live feed with a few seconds of delay. If a sentence might turn into something that must not go on air, the delay gives time to cut it. For streaming, the risky part is the tail of the text that matches the beginning of a stop string.

Worked example

stop is "<|end|>". After chunk "Hi <", the tail "<" could start the stop, so only "Hi " is sent and "<" is held. After "|e", the buffer "<|e" still could, so nothing is sent. The next chunk completes "<|end|>", so streaming ends and the held text is dropped.

if a stop occurs, emit text before its earliest start and end; else hold the longest suffix that is a proper prefix of a stop

Chat servers do this for chat template end markers and user-provided stop words.

Topics
LLM inference and serving
Companies1
  • xAI a candidate reported this question xAI Machine Learning Engineer Interview Questions | Screna AI

From public interview reports and guides. Not affiliated with or endorsed by these companies.

Similar questions
  • Simulate continuous batchingHard
  • Simulate a chunked prefill scheduler with a per-step token budgetHard
  • Accept or reject draft tokens in speculative samplingHard
  • Simulate KV memory preemption with recomputeHard
All LLM inference and serving problems
Report
454 / 903
Python 3solution.pyPython idle
1
2
3
4
def stream_with_stops(chunks, stops):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS