Simulate Continuous Batching Under a KV Memory Budget

Last updated: November 29, 2026

Quick Overview

Write a step-by-step simulator for an LLM server that admits requests FCFS into a running batch whenever KV memory and batch slots allow, emits one token per running request per step, and returns each request's completion step.

Together AI
Coding & Algorithms
Machine Learning Engineer
Together AI
November 29, 2026
Machine Learning Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Write a step-by-step simulator for an LLM server that admits requests FCFS into a running batch whenever KV memory and batch slots allow, emits one token per running request per step, and returns each request's completion step.

Throughput on Together AI's inference stack depends on iteration-level (continuous) batching: finished requests leave the batch immediately and waiting ones join. This round checks that you can model the scheduler precisely, keep memory accounting consistent, and discuss the policy choices that affect latency and starvation.

What the Interviewer Expects
  • Models iteration-level scheduling rather than static batches
  • Reserves and frees KV memory consistently on admit and finish
  • Keeps FCFS order and explains why it does not skip ahead
  • Jumps time forward when the server is idle
  • Rejects requests that can never fit
Key Topics to Cover
continuous batching
scheduling
KV cache budget
simulation
FCFS fairness
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
  • How would results change if admission reserved only the prompt and grew memory per token, with preemption on overflow?
  • How would you model prefill taking longer than one decode step for long prompts?
  • What policy would you use to cut p99 time to first token without hurting throughput much?
  • How would chunked prefill change the simulation?
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 (id, arrival_step, prompt_len, output_len). Admission reserves prompt_len + output_len KV slots (worst case, so no preemption is needed). At most max_batch requests run. The admi...

High-Level Design

Three collections: pending sorted by arrival, waiting (arrived, not admitted), running. Each loop: move arrivals into waiting, admit from the head of waiting while memory and slots allow, run on...


Submit Your Answer
Markdown supported

Related Questions