Implement a Rolling Buffer KV Cache for Sliding-Window Attention

Last updated: November 29, 2026

Quick Overview

Implement a fixed-size key/value cache for a model that uses sliding-window attention with window W: appends overwrite the oldest slot, and you must return the visible entries in chronological order, including during chunked prompt prefill.

Mistral AI
Coding & Algorithms
Machine Learning Engineer
Mistral AI
November 29, 2026
Machine Learning Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Implement a fixed-size key/value cache for a model that uses sliding-window attention with window W: appends overwrite the oldest slot, and you must return the visible entries in chronological order, including during chunked prompt prefill.

Mistral 7B popularized sliding-window attention with a rolling buffer cache, so an inference-focused screen at Mistral AI may ask you to build one from scratch. The interviewer checks modular index arithmetic, correct ordering after wrap-around, and whether you understand how chunked prefill interacts with the window.

What the Interviewer Expects
  • Use a circular buffer indexed by absolute position modulo W
  • Return entries oldest to newest even after wrap-around
  • Explain why memory stays O(W) regardless of sequence length
  • Handle chunked prefill where a chunk's queries see the cache plus earlier tokens in the chunk
  • Mention the effective receptive field grows with depth (layers times W)
Key Topics to Cover
Sliding-window attention
Circular buffer
KV cache
Chunked prefill
Inference memory
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 does the attention mask look for a query in the middle of a prefill chunk?
  • How would you batch sequences of different lengths with one buffer tensor?
  • What changes if positions use RoPE: do you store rotated or unrotated keys?
  • How does this interact with prefix caching across requests?
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

Window W: each new token attends to at most the previous W tokens (including itself, depending on convention). The cache stores per-token KV entries, uses O(W) memory, and must expose them in time o...

High-Level Design

Keep pos, the absolute count of tokens written. Token at position p lives in slot p % W. The visible window is positions [max(0, pos - W), pos), read back through the same modulo mapping, which ...


Submit Your Answer
Markdown supported

Related Questions