Skip to content
← Implement-from-Scratch Coding / 22
hardNewMistral AIMetaTogether AI

Implement a sliding-window KV cache as a ring buffer that stays correct after wraparound and for multi-token chunks.

Slot = position mod W is the easy line. The answer is in the two that follow it: rotate keys by absolute position, never by slot, and attend before you write, because a prefill chunk that writes first overwrites keys its own earlier queries still need.

Updated Oct 2026 · Worked answers on inference mechanisms, with calculations, implementation details, and follow-up questions.

Slot = position mod W is the easy line. The answer is in the two that follow it: rotate keys by absolute position, never by slot, and attend before you write, because a prefill chunk that writes first overwrites keys its own earlier queries still need.

10 answers per topic instead of 5, plus saved progress and bookmarks · costs nothingor unlock all 213 remaining answers · $25

Every worked answer and derivation in all 12 topics, every concept page, the full practice-test bank, and every code drill’s edge-case tests and reference solution.

6 months · One payment · No auto-renewal

UP NEXT ON YOUR JOURNEY
DISCUSSION · 0

Nobody has commented on this answer. Add a correction or another way to work it.