Implement Sliding Window Attention in C++ with a Self-Designed KV Cache Layout

Quick Overview

An AI-assisted C++ coding exercise from a software engineering internship interview: implement sliding window attention inside an inference codebase, design the shape of the KV cache yourself, and explain the implementation's logic and optimizations. It tests attention mechanics, memory layout and verifying AI-written code.

Implement Sliding Window Attention in C++ with a Self-Designed KV Cache Layout

Company: NVIDIA

Role: Software Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Technical Screen

In a 30-minute AI-assisted coding exercise, you are given an existing C++ codebase and asked to implement sliding window attention, in which each token attends only to a fixed number `W` of the most recent positions instead of the whole prefix. You may use an AI coding assistant, and the interviewer reads your prompts: use the assistant to understand the codebase, but the shape of the key/value (KV) cache is yours to design. Afterwards you must explain the implementation the assistant helped produce, both its logic and its optimizations. The codebase itself is not part of this write-up. To practice, assume a decoder-only transformer inference engine in C++ whose attention currently attends over the full prefix. For every new token, each layer produces a query for each of `num_heads` heads and a key and a value for each of `num_kv_heads` heads, each a vector of `head_dim` floats. Generation is a prefill over the prompt followed by one decoding step per generated token. ### Clarifying Questions - Does the window of `W` positions include the current token, and does the same window apply to every layer? - Does the cache serve a batch of sequences, possibly at different positions, or a single sequence? - Is `num_kv_heads` equal to `num_heads`, or do several query heads share each key/value head? - Where is positional encoding applied: are keys already rotated or offset when they would be written to the cache? - Is prefill in scope or only the decoding step, and can a prompt be longer than `W`? - Which data type should the cache use, and is there a memory budget? - What does the codebase already provide: tensor types, threading, SIMD helpers, a reference attention to compare against? ### Part 1: Design the KV cache shape Propose the shape and memory layout of the KV cache for sliding window attention: its dimensions and their order, where a new token's key and value are written, which entries are read at each step, and how much memory it needs compared with a cache sized for the full sequence length. ```hint How much history is needed Ask how many past keys any query can ever need, and what should happen to the entry that falls out of the window when a new token arrives. ``` #### What This Part Should Cover - The dimensions and their order, justified by how the attention loop reads them - How the write position and the set of valid entries follow from the token position - Memory as a function of the window size versus the sequence length, including the effect of shared key/value heads - How the layout interacts with positional encoding and with sequences at different positions ### Part 2: Implement sliding window attention in C++ Implement attention over your cache: the decoding step that adds one token per sequence, and the handling of the prompt during prefill, so that every query attends to exactly the keys inside its window. ```hint Probe the boundary Plan the comparison that would expose an off-by-one in the window: prompts shorter than, equal to and longer than `W`, checked against a straightforward masked attention. ``` ```hint Keep the softmax finite Think about what the softmax needs so that large dot products cannot overflow the exponential. ``` #### What This Part Should Cover - Correct window bounds in both decoding and prefill - Scaled dot-product attention with a numerically stable softmax - Correct cache indexing, including after the write position wraps around - Tests against a reference implementation ### Part 3: Explain the logic and the optimizations Walk the interviewer through the implementation the assistant helped you produce: why it is correct, what you asked the assistant for and what you verified yourself, and which optimizations it uses or could use. ```hint Own every line Be ready to point to the loop that dominates the cost and the memory layout it depends on, and to defend each index expression the assistant wrote. ``` #### What This Part Should Cover - A line-level account of correctness, especially the window bounds and cache indexing - How the prompts were used to understand the codebase and how the generated code was checked - Optimizations and their effect: memory layout, vectorization, threading, a single-pass softmax, the cache data type ### What a Strong Answer Covers - A cache whose size depends on the window rather than the sequence length, with a justified layout - Exact window semantics in both prefill and decoding - Numerically stable attention code that compiles and is tested against a reference - Purposeful use of the AI assistant: targeted prompts and verification rather than blind acceptance - Clear complexity and memory reasoning, and concrete optimizations ### Follow-up Questions - How would the layout and indexing change to serve many sequences at different positions in one batch? - How would you map the decoding step onto a GPU kernel: what does each thread block own, and where are the partial softmax results combined? - Some models keep the first few tokens of the sequence permanently in addition to the window. How would you extend the cache? - If memory were tight, how would storing the cache in 16-bit or 8-bit types affect your code and the model's accuracy?

Overview: An AI-assisted C++ coding exercise from a software engineering internship interview: implement sliding window attention inside an inference codebase, design the shape of the KV cache yourself, and explain the implementation's logic and optimizations. It tests attention mechanics, memory layout and verifying AI-written code.

NVIDIA logo
NVIDIA
Nov 23, 2026
mediumSoftware EngineerTechnical ScreenMachine Learning
1
0

In a 30-minute AI-assisted coding exercise, you are given an existing C++ codebase and asked to implement sliding window attention, in which each token attends only to a fixed number W of the most recent positions instead of the whole prefix. You may use an AI coding assistant, and the interviewer reads your prompts: use the assistant to understand the codebase, but the shape of the key/value (KV) cache is yours to design. Afterwards you must explain the implementation the assistant helped produce, both its logic and its optimizations.

The codebase itself is not part of this write-up. To practice, assume a decoder-only transformer inference engine in C++ whose attention currently attends over the full prefix. For every new token, each layer produces a query for each of num_heads heads and a key and a value for each of num_kv_heads heads, each a vector of head_dim floats. Generation is a prefill over the prompt followed by one decoding step per generated token.

Clarifying Questions Guidance

  • Does the window of W positions include the current token, and does the same window apply to every layer?
  • Does the cache serve a batch of sequences, possibly at different positions, or a single sequence?
  • Is num_kv_heads equal to num_heads , or do several query heads share each key/value head?
  • Where is positional encoding applied: are keys already rotated or offset when they would be written to the cache?
  • Is prefill in scope or only the decoding step, and can a prompt be longer than W ?
  • Which data type should the cache use, and is there a memory budget?
  • What does the codebase already provide: tensor types, threading, SIMD helpers, a reference attention to compare against?

Part 1: Design the KV cache shape

Propose the shape and memory layout of the KV cache for sliding window attention: its dimensions and their order, where a new token's key and value are written, which entries are read at each step, and how much memory it needs compared with a cache sized for the full sequence length.

What This Part Should Cover Guidance

  • The dimensions and their order, justified by how the attention loop reads them
  • How the write position and the set of valid entries follow from the token position
  • Memory as a function of the window size versus the sequence length, including the effect of shared key/value heads
  • How the layout interacts with positional encoding and with sequences at different positions

Part 2: Implement sliding window attention in C++

Implement attention over your cache: the decoding step that adds one token per sequence, and the handling of the prompt during prefill, so that every query attends to exactly the keys inside its window.

What This Part Should Cover Guidance

  • Correct window bounds in both decoding and prefill
  • Scaled dot-product attention with a numerically stable softmax
  • Correct cache indexing, including after the write position wraps around
  • Tests against a reference implementation

Part 3: Explain the logic and the optimizations

Walk the interviewer through the implementation the assistant helped you produce: why it is correct, what you asked the assistant for and what you verified yourself, and which optimizations it uses or could use.

What This Part Should Cover Guidance

  • A line-level account of correctness, especially the window bounds and cache indexing
  • How the prompts were used to understand the codebase and how the generated code was checked
  • Optimizations and their effect: memory layout, vectorization, threading, a single-pass softmax, the cache data type

What a Strong Answer Covers Guidance

  • A cache whose size depends on the window rather than the sequence length, with a justified layout
  • Exact window semantics in both prefill and decoding
  • Numerically stable attention code that compiles and is tested against a reference
  • Purposeful use of the AI assistant: targeted prompts and verification rather than blind acceptance
  • Clear complexity and memory reasoning, and concrete optimizations

Follow-up Questions Guidance

  • How would the layout and indexing change to serve many sequences at different positions in one batch?
  • How would you map the decoding step onto a GPU kernel: what does each thread block own, and where are the partial softmax results combined?
  • Some models keep the first few tokens of the sequence permanently in addition to the window. How would you extend the cache?
  • If memory were tight, how would storing the cache in 16-bit or 8-bit types affect your code and the model's accuracy?