449. Simulate continuous batching
Static batching waits for the slowest request in a batch. Continuous batching (used by vLLM and TGI) refills a free slot the moment a request finishes.
Simulate it in discrete steps. requests is a list of (arrival_step, tokens_to_generate). There are max_batch slots. At each step t = 0, 1, 2, ...:
1. Admit waiting requests whose arrival_step <= t into free slots, in list order.
2. Every request in a slot generates one token.
3. Any request that has now generated all its tokens finishes at step t and frees its slot.
Write continuous_batching(requests, max_batch) returning a list with the finishing step of each request, in input order.
def continuous_batching(requests, max_batch)Example 1
Input: requests = [(0, 2), (0, 6), (0, 3)], max_batch = 2
Output: [1, 5, 4]
Example 2
Input: requests = [(0, 4), (3, 2)], max_batch = 4
Output: [3, 4]
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
Keep a queue of indices not yet admitted, a list of active indices and a remaining-token count per request.
Hint 2
Loop t until everything finishes.
Common mistakes
- The buggy version only refills when the whole batch is empty, which is static batching.
Understand the idea
Static batching waits until every request in a batch finishes before starting new ones, so short requests sit idle behind long ones. Continuous batching refills a slot the moment it frees up. Like a bus that lets new passengers on at every stop instead of only when it is empty. Each step: first admit arrived requests into free slots in list order, then every active request makes one token, then requests that are done finish at this step and free their slots.
max_batch 2, requests [(0, 3), (0, 1), (1, 2)]. Step 0: admit r0 and r1; each makes a token; r1 is done, finishes at 0. Step 1: r2 has arrived, takes the free slot; r0 makes token 2, r2 makes token 1. Step 2: r0 makes token 3 and finishes at 2; r2 makes token 2 and finishes at 2. Answer [2, 0, 2]. Static batching would start r2 only at step 3.
admit, generate, then free. vLLM and similar servers use continuous batching
Companies2
- ByteDance the topic appears in interview reports ByteDance Machine Learning Engineer Interview Process
- 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.