438. Generate one token at a time with a KV cache
When a model writes token number 500, it needs the keys and values of all 499 earlier tokens. Recomputing them every step is wasteful, so servers store them in a KV cache and only compute the newest token's key and value.
Write decode_step(q, k, v, cache) where q, k, v are 1D arrays (length d) for the newest token and cache is a dict {"K": list, "V": list} holding earlier keys and values (possibly empty lists).
1. Append k to cache["K"] and v to cache["V"].
2. Attend: scores = K @ q / sqrt(d) over every cached key (including the new one), softmax, weighted sum of cached values.
3. Return the output vector (length d). The cache must be updated in place.
No mask is needed: the cache only ever holds the past and the present.
def decode_step(q, k, v, cache)Example 1
Input: decode_step([1.0, 0.0], [1.0, 0.0], [2.0, 3.0], {'K': [], 'V': []})
Output: [2.0, 3.0]
Constraints
- Python 3 with NumPy. Code runs in your browser.
- Your code must pass all 1 tests to be accepted.
Hint 1
Only the newest token's key and value are new: add them to the cache, then let the single query attend over everything cached.
Hint 2
Append k and v to the lists, turn the lists into matrices with np.stack, compute K @ q / sqrt(d), softmax the scores, and take the weighted sum of the rows of V.
Hint 3
Append before stacking so the new token attends to itself, and subtract s.max() before np.exp for a stable softmax; the output is w @ V with shape (d,).
Understand the idea
When a model generates text one token at a time, the keys and values of old tokens never change. So instead of recomputing them, you keep them in a cache and only add the newest token's key and value. Like keeping your notes from earlier chapters instead of rereading the book. Each step: append the new k and v, then let the single query q attend over every cached key, including its own. Worked example, d = 2: cache holds k0 = [1, 0], v0 = [2, 0]. New token: q = [0, 1.414], k = [0, 1], v = [0, 4]. Scores divided by sqrt(2): k0.q = 0 gives 0, k.q = 1.414 gives 1. Softmax of [0, 1] is [0.269, 0.731]. Output = 0.269*[2, 0] + 0.731*[0, 4] = [0.538, 2.924].
append, then scores = K q / sqrt(d), softmax, weighted sum of V
No mask is needed. Every LLM serving engine uses a KV cache to make generation fast.
Companies6
- Anthropic a candidate reported this question LLM from scratch interview walkthrough (system-design-labs)
- ByteDance the topic appears in interview reports ByteDance Machine Learning Engineer Interview Process
- DeepMind a candidate reported this question Google DeepMind interview guide (landedjobs/ai-interview-guides)
- Mistral the topic appears in interview reports Mistral AI Interview Guide 2026: European Frontier Lab, Open-Weight Strategy, MoE, and La Plateforme
- Netflix the topic appears in interview reports Netflix Machine Learning Engineer Interview Process
- OpenAI a candidate reported this question OpenAI Research Engineer Interview Guide (Dataford)
From public interview reports and guides. Not affiliated with or endorsed by these companies.