Continuous Batching Scheduler Under a KV Cache Budget

Last updated: November 29, 2026

Quick Overview

Simulate an LLM server that runs one decode step per time unit, adds new requests to the running batch between steps, and admits a request only if its prompt plus output tokens fit in the remaining KV cache budget and the batch has room. Return each request's finish time, or -1 if it can never fit.

Baseten
Coding & Algorithms
Machine Learning Engineer
Baseten
November 29, 2026
Machine Learning Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Simulate an LLM server that runs one decode step per time unit, adds new requests to the running batch between steps, and admits a request only if its prompt plus output tokens fit in the remaining KV cache budget and the batch has room. Return each request's finish time, or -1 if it can never fit.

Throughput on Baseten's LLM deployments comes from continuous (in-flight) batching, where finished sequences leave the batch and waiting ones join without draining it. A model performance interviewer uses this to see whether you can model the scheduler precisely, including memory accounting and fairness.

What the Interviewer Expects
  • Models iteration-level scheduling: requests join and leave between steps, not per batch
  • Reserves KV memory at admission and releases it exactly when a request finishes
  • Keeps FIFO order with head-of-line blocking and can explain the trade-off versus skipping ahead
  • Rejects requests that exceed the whole budget instead of looping forever
  • Jumps time forward when the server is idle instead of ticking empty steps
  • Explains how paged attention changes the reservation model
Key Topics to Cover
continuous batching
KV cache
discrete event simulation
LLM serving
admission control
head-of-line blocking
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Switch from reserving prompt plus max output to allocating KV blocks on demand. What happens when memory runs out mid-generation, and how would you preempt?
  • How would you model prefill cost so that long prompts slow down the step they join?
  • Would you let a small request skip a large blocked one? How do you prevent starvation?
  • How do you pick max_batch to balance throughput against per-token latency?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Each request is (arrival, prompt_tokens, output_tokens). At time t the scheduler adds arrivals to a FIFO queue, admits from the head while used + prompt + output <= kv_budget and the batch is be...

High-Level Design

Three collections: pending (sorted by arrival), waiting (FIFO, arrived but not admitted) and running (index to tokens left). A loop admits, then steps. If nothing is running and nothing can be a...


Submit Your Answer
Markdown supported

Related Questions