Simulate Continuous Batching With A KV Token Budget
Last updated: November 29, 2026
Quick Overview
Given LLM requests with arrival step, prompt length and output length, simulate an iteration-level scheduler with a maximum batch size and a KV cache token budget, and return each request's first-token and finish steps.
Fireworks AI
November 29, 20260
0
0 solved
Given LLM requests with arrival step, prompt length and output length, simulate an iteration-level scheduler with a maximum batch size and a KV cache token budget, and return each request's first-token and finish steps.
Continuous batching is the core of an inference server like the one behind Fireworks, where requests join and leave the batch every decode step. The interviewer checks that the candidate models admission, memory reservation and completion in the right order and can reason about head-of-line blocking.
What the Interviewer Expects
- Admit requests per step rather than per batch
- Reserve KV memory for prompt plus maximum output on admission
- Free slots and memory as soon as a request finishes
- Handle idle gaps by jumping to the next arrival
- Explain the fairness choice (strict FIFO versus skipping ahead)
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would preemption and recomputation let you reserve less memory up front?
- What changes if prefill of long prompts takes several steps and you chunk it?
- How would you prioritize latency-sensitive tenants without starving batch jobs?
- How would you measure TTFT and inter-token latency from this simulation?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Time is discrete. At each step, first admit arrived requests in FIFO order while the batch has fewer than max_batch sequences and the reserved KV tokens (prompt plus output for each running request)...
High-Level Design
A deque of pending requests sorted by arrival, a dict of running requests with remaining tokens and reserved memory, and a running total of used KV. Strict FIFO: if the head does not fit, stop admitti...