Batching interview problems

Batching questions ask you to group many small operations into fewer large ones. The simplest version chunks an iterable, which itertools.batched(iterable, n) does since Python 3.12. The interesting version is a micro-batcher. Callers await batcher.submit(item) and get their own result back, while the batcher flushes when it reaches max_size items or when max_wait seconds pass since the first item, whichever comes first. That needs a buffer, one asyncio.Future per caller, and a timer task or asyncio.wait_for on the next item.

The common mistakes are flushing only on size so a lone request waits forever, starting the timer on every item instead of the first in a batch, and failing every caller in a batch when the backend reports a per-item error. Others include forgetting to flush on shutdown and resolving futures that were already cancelled, which raises InvalidStateError. A strong answer maps results back to the right future, checks future.done() before setting a result, and discusses the latency versus throughput trade of the two knobs.

1 open problem

  1. 20Dynamic Request Batcherhard

    Group concurrent inference requests into batches that flush when full or when the oldest request has waited long enough, with cancellation and drain.

4 locked

These open in order of demand. Open one and press I want this one to ask for it.

  1. 11Dynamic Request Batching Lockedhard

    Group arriving inference requests into batches that close when full or when a wait deadline passes, using an asyncio producer and consumer.

  2. 27Async Inference Batcher Lockedhard

    Group single inference requests into batches by size or wait time, with per-request timeouts, cancellation and a draining shutdown.

  3. 37Stream Rechunker Lockedeasy

    Regroup a stream of arbitrarily sized byte chunks into fixed-size chunks, lazily and with memory bounded by the chunk size.

  4. 47Token Budget Batching Lockedeasy

    Greedily group inference requests into ordered batches capped by both a maximum request count and a maximum total token count.