← All Problems

13. Reassemble Out-of-Order Streamed Text Chunks into One Context

Confirmed Medium Buffering / Hash Map
Grounding: Confirmed: Cartesia's realtime text-to-speech input streaming API accepts text via push() with a continue flag, finalized by no_more_inputs(), and "context carries history" so each chunk is appended to the prior context rather than reprocessed from scratch (docs.cartesia.ai realtime text-to-speech quickstart). Inference: reassembling chunks that can arrive out of order at a buffering layer before being appended to the context is a plausible scenario for a client or proxy sitting in front of that API; it is not a description of Cartesia's own internal buffering.

Problem

Cartesia's realtime text-to-speech input streaming API lets a client push() text chunks into a context with continue=True, finalized by no_more_inputs() (continue=False); the context carries history forward, so each chunk logically appends to the prior chunks of that same context. A buffering layer in front of that context needs to reassemble chunks into the final ordered text even if network retries deliver them out of send order.

Given a list of (sequence_index, text) pairs (0-indexed, possibly out of order, possibly containing duplicate indices from retries where the latest delivery for an index should win), return the fully concatenated text once every index 0..expect_count-1 is present, or None if any index is still missing.

Source: src/13_streaming_context_reassembly.py

def reassemble_context(chunks: list[tuple[int, str]], expect_count: int) -> str | None:

>>> reassemble_context([(1, "world"), (0, "hello "), (2, "!")], expect_count=3)
'hello world!'

>>> reassemble_context([(0, "hi"), (2, "!")], expect_count=3)
# None -- index 1 is missing

Step-by-Step Approach

  1. Ingest all chunks into a dict[int, str] keyed by sequence index, in a single pass over the input list.
  2. For a duplicate index, simply overwrite — "last write wins" naturally falls out of dict assignment, which matches retry semantics where the most recent delivery for an index should replace an earlier one.
  3. Check completeness with a single generator expression: any(i not in buf for i in range(expect_count)) — this short-circuits at the first missing index instead of scanning everything.
  4. If any index is missing, return None immediately without doing any string work.
  5. Otherwise, join the text in sequence order: "".join(buf[i] for i in range(expect_count)), which is safe now that completeness has already been confirmed.

The key insight is separating "ingest" from "assemble": a hash map absorbs out-of-order and duplicate deliveries for free via plain key assignment, and the sequence constraint (0..expect_count-1) is only ever checked once, up front, rather than being threaded through the ingestion loop.

Reference solution

def reassemble_context(chunks: list[tuple[int, str]], expect_count: int):
    # dict keyed by sequence index: O(n) to ingest, last write wins on duplicates
    buf: dict[int, str] = {}
    for idx, text in chunks:
        buf[idx] = text
    # a single membership check per required index, short-circuits on first gap
    if any(i not in buf for i in range(expect_count)):
        return None
    # join in sequence order, O(expect_count) once completeness is confirmed
    return "".join(buf[i] for i in range(expect_count))

Key Functions & Tricks

  • dict[int, str] keyed by sequence index — absorbs out-of-order arrivals and duplicate retries with plain assignment.
  • any(i not in buf for i in range(n)) — short-circuiting completeness check — stops at the first gap instead of scanning to the end.
  • "".join(buf[i] for i in range(n)) — assembles the final string in sequence order only once completeness is known.
  • last-write-wins on duplicate keys — matches real retry semantics: the newest delivery for an index should be the one that's kept.

How to Recognize This Pattern

Reach for a hash-map buffer whenever a problem describes reassembling a sequence from parts that can arrive out of order, possibly with duplicates — chunked uploads, retried network messages, or out-of-order packets that carry an explicit index. The signal is "parts identified by index/sequence number, order of arrival not guaranteed." A common variation adds a streaming/incremental flavor: instead of receiving the whole list at once and checking completeness at the end, the buffer must flush the longest available contiguous prefix as soon as it exists (see the jitter-buffer problem in this set for that variant). A common pitfall is checking completeness by comparing len(buf) == expect_count instead of checking that specific indices 0..expect_count-1 are present — that's wrong whenever duplicates are possible, since a full-length buffer built from N chunks with some repeated indices can still be missing a real gap.