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

785. Group incoming requests into batches by size or wait time

MediumLLM inference and serving Pro

Embedding and rerank servers run faster on batches, but nobody wants to wait forever for a batch to fill. A micro-batcher flushes a batch when it is full or when its oldest request has waited long enough.

Write batch_requests(arrivals, max_batch, max_wait). arrivals is a list of (time, request_id) pairs sorted by time. Return a list of [flush_time, [request_ids]] in flush order.

Rules: the first request in an empty batch opens it at time t0 and sets the deadline t0 + max_wait. A request arriving at or before the deadline joins the batch. When the batch reaches max_batch requests it flushes at that request's arrival time. When a request arrives after the deadline, the open batch flushes at the deadline first and the new request opens a new batch. At the end, any open batch flushes at its deadline.

Functiondef batch_requests(arrivals, max_batch, max_wait)

Example 1

Input: arrivals = [(0, 'a'), (1, 'b'), (2, 'c'), (10, 'd')], max_batch = 2, max_wait = 5

Output: [[1, ['a', 'b']], [7, ['c']], [15, ['d']]]

Example 2

Input: arrivals = [(0, 1), (5, 2), (6, 3)], max_batch = 4, max_wait = 5

Output: [[5, [1, 2]], [11, [3]]]

Constraints

  • At most 10,000 arrivals.
  • max_batch >= 1, max_wait >= 0.
Hint 1

The deadline belongs to the oldest request in the batch.

Hint 2

Check the deadline before you add the new request.

Hint 3

Flush leftovers at their deadline, not at the last arrival.

Common mistakes
  • Resetting the deadline on every arrival, so a steady stream never flushes.
  • Flushing a timed-out batch at the new request's time instead of the deadline.
Understand the idea

Batching trades a little latency for a lot of throughput: a GPU does 8 inputs in about the time of 1 when it is underused. A micro-batcher caps the latency cost with a timer. Rule of thumb: flush on size or on time, whichever comes first, and start the timer when the first request arrives. With max_batch 2 and max_wait 5, requests at 0 and 1 flush together at 1; a lone request at 2 waits until 7. Inference servers use the same idea, often called dynamic batching, in front of embedding, rerank and classification models.

Topics
LLM inference and serving
Similar questions
  • Generate one token at a time with a KV cacheMedium
  • Allocate KV cache memory in pages, like vLLMMedium
  • Verify a draft model's guesses in speculative decodingMedium
  • Is decoding memory-bound? A roofline estimateMedium
All LLM inference and serving problems
Report
785 / 903
Python 3solution.pyPython idle
1
2
3
4
def batch_requests(arrivals, max_batch, max_wait):
# return [[flush_time, [ids]], ...]
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