Entry

What is self-attention and why does it matter?

I explain self-attention as a way for the model to decide which other tokens in the sequence matter for the token it is processing. Each token is projected into a query, a key and a value.

How to answer it in an AI engineer interview: what is being tested, a step-by-step framework, a sample answer and the mistakes to avoid.

$19.99 on Gumroad, instant download

Smaller start: Score your answers against 50 rubrics, $9. 50 AI engineer questions, one rubric each, PDF on Gumroad.

Practiced one out loud? Paste your answer into the free self-check and get a score in 30 seconds

What the interviewer is testing

Whether you can explain the query-key-value computation, why self-attention improves long-range token interactions, the quadratic cost, and the reason for scaling before softmax.

Answer framework

  1. Represent each token with three learned projections: a query, a key and a value.
  2. Compute compatibility scores with the query-key dot product, scale them by 1/sqrt(d_k), and apply softmax to obtain attention weights.
  3. Produce each token's output as a weighted sum of the value vectors using those attention weights.
  4. Allow each token to retrieve information from any position permitted by the attention mask in one layer, avoiding a recurrent bottleneck while supporting parallel sequence processing during training.
  5. Recognize the trade-off: full self-attention has O(n^2) compute and an attention matrix that also scales quadratically with sequence length.

Sample answer

I explain self-attention as a way for the model to decide which other tokens in the sequence matter for the token it is processing. Each token is projected into a query, a key and a value. The query represents what the token is looking for, the key represents what it offers for matching, and the value carries information that can be passed along. We compute scores by taking the query-key dot product, divide by the square root of the key dimension, and apply softmax to get normalized attention weights.

The output for each position is a weighted sum of the value vectors using those weights. The important benefit is direct information flow: each token can pull information from positions permitted by the attention mask through one layer instead of passing it through recurrent steps. Unmasked attention can access the entire sequence; causal attention can access only current and earlier positions. During training, positions can still be processed in parallel.

The trade-off is quadratic computation. For sequence length n and fixed head dimensions, full attention uses O(n^2) compute. Explicitly storing its attention matrix also takes O(n^2) memory, but implementations such as FlashAttention avoid materializing that matrix. Dividing scores by the square root of the key dimension also matters: under standard assumptions, dot-product variance grows with dimension, which can saturate softmax and produce small gradients. Scaling helps keep scores in a useful range.

Likely follow-ups

Why do we divide the attention scores by sqrt(d_k)? Without the scaling, dot products grow as the key dimension grows. The softmax can then saturate, producing very small gradients and making optimization harder.
What is the main trade-off in self-attention? It gives tokens direct access to positions permitted by the attention mask, but full attention requires O(n^2) computation at fixed head dimensions. Materializing the attention matrix also requires O(n^2) memory; memory-efficient implementations avoid storing it.

Common mistakes

The PlaybookWant the full method?

The 0 to 1 AI Engineer Interview Playbook covers the answer frameworks, system-design walkthroughs and an 8-week plan.

Get the playbook

Smaller start: Score your answers against 50 rubrics, $9. 50 AI engineer questions, one rubric each, PDF on Gumroad.

Interviewing for PM or TPM? See the full PM package, $59 on Gumroad: a resume system, a PM, TPM and PGM interview handbook, a 1:1 cheat sheet and a PM playbook. PM and TPM content, not AI engineer prep.

Related questions

Based on question #2 in the full question bank.

AI Engineer Interview PlaybookStructured practice for AI engineer interviews

A structured interview practice pack with prompts and scoring notes for AI engineer rounds. Price is $19.99.

Get the AI Engineer Interview Playbook, $19.99

Also: Grok Bot Team Setup Pack ($49)