Sliding Window Attention over a KV Cache
Updated
Problem statement
Given the query, key and value rows (each of length d) of one attention head for n decode steps and a window size, return the output of sliding window attention at every step, where each step attends only to the window most recent positions, including its own.
The steps run in order t = 0, 1, ..., n - 1. At step t, first add keys[t] and values[t] to the KV cache. Then queries[t] attends to positions max(0, t - window + 1) through t. If fewer than window steps have run, it attends to all of them.
The score of position j is the dot product of queries[t] and keys[j]. The queries are already scaled, so do not divide by the square root of d. The weights are the softmax of these scores over the attended positions only, and the output of step t is the weighted sum of their rows in values. A score can be as large as 80000 in absolute value.
Return one string per step: the d output values in order, each written with exactly six digits after the decimal point (rounded to nearest) and joined by commas with no spaces, for example "7.310586,2.689414". The tests avoid values that sit exactly halfway between two six-digit results, so any correct solution that uses double precision prints the same text.
The shape of the KV cache is part of the task: it should hold at most window key rows and window value rows, not every row seen so far. Only the returned strings are checked.
Function
slidingWindowAttention(queries: int[][], keys: int[][], values: int[][], window: int) → String[]Examples
Example 1
queries = [[1],[1],[1]]keys = [[0],[1],[0]]values = [[0],[10],[20]]window = 2return = ["0.000000","7.310586","12.689414"]Step 0 sees only its own key, so its output is its own value, 0. At step 1 the window holds positions 0 and 1 with scores 0 and 1, so position 1 gets weight e/(1+e) and the output is 10e/(1+e) = 7.310586. At step 2 the window size is 2, so position 0 drops out: the scores are 1 (position 1) and 0 (position 2), and the output is (10e + 20)/(1+e) = 12.689414.
Example 2
queries = [[1,0],[0,1],[1,1],[0,0]]keys = [[1,0],[0,1],[1,0],[2,2]]values = [[4,0],[0,4],[2,2],[8,8]]window = 3return = ["4.000000,0.000000","1.075766,2.924234","2.000000,2.000000","3.333333,4.666667"]Each score is the plain dot product of the query with a key, with no extra scaling. At step 3 the window holds positions 1, 2 and 3, and the query is all zeros, so all three scores are 0 and the weights are equal. The output is the average of those three value rows: (0 + 2 + 8)/3 = 3.333333 and (4 + 2 + 8)/3 = 4.666667. Position 0 is outside the window.
Example 3
queries = [[100,1],[100,1]]keys = [[100,0],[100,1]]values = [[0,10],[10,0]]window = 2return = ["0.000000,10.000000","7.310586,2.689414"]At step 1 the scores are 10000 and 10001. Taking exp of these numbers directly overflows a double. After subtracting the largest score they become -1 and 0, so the weights are 1/(1+e) and e/(1+e), and the output is 10e/(1+e) = 7.310586 and 10/(1+e) = 2.689414.
Constraints
1 <= n <= 500, wheren == queries.length == keys.length == values.length1 <= d <= 8, where every row ofqueries,keysandvalueshas lengthd-100 <= queries[i][j], keys[i][j], values[i][j] <= 1001 <= window <= 1000
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
- Merge Multiple Sorted StreamsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Evenly Schedule Vehicle InspectionsAmazon · PHONE SCREEN
- Implement Cross-Entropy LossOpenAI · PHONE SCREENOpenAI interview questions
- Cumulative No-Intercept Regression BetasCitadel · PHONE SCREENCitadel interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Automation Pipeline, Part 2: Stage WorkersPlaid · PHONE SCREENPlaid interview questions
- Reservoir Sampling with Recorded DrawsGoogle · PHONE SCREENGoogle interview questions
- Rolling Five-Second Flight-Distance AlertsSkydio · PHONE SCREENSkydio interview questions
- K-Means Delivery ZonesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Token Bucket Request DecisionsDatadog · PHONE SCREENDatadog interview questions