Medium6 min readUpdated 2026-08-12

How does attention work in a transformer?

Attention lets each token look up relevant information from every other token using query and key similarity, then return a weighted mix of value vectors. Here is the intuition, a worked numeric example, and what interviewers probe for.

A hand-drawn knowledge diagram showing tokens flowing into query, key and value, then attention weights, then a weighted sum output.
TL;DR
  • Attention lets each token look at other tokens using queries QQ, keys KK, and values VV to compute a weighted sum of information.
  • We score similarity with dot products qikjq_i \cdot k_j, scale by dk\sqrt{d_k}, apply softmax, and multiply by VV to get outputs.
  • Scaling by dk\sqrt{d_k} keeps the softmax from becoming too peaky as dkd_k grows; this stabilizes gradients.
  • Multi head attention runs several of these in parallel with different linear projections, letting the model focus on different relationships.

Key tradeoffs: simpler attention is flexible and parallelizable but costs O(n2)O(n^2) memory/time in sequence length and can struggle with very long contexts.

In this question, we will learn how the attention mechanism in a transformer turns token embeddings into context-aware representations. We will walk from a simple intuition to the formal formula, then run a small worked example on the sentence "The cat sat on the mat".

We will cover the following:

  • The intuition (an analogy)
  • How it actually works
  • Why we scale by dk\sqrt{d_k}
  • Multi head attention
  • Tradeoffs and failure modes
  • Questions the interviewer might ask
  • What the interviewer is really testing

Direct answer: Attention lets each token form a query qiq_i that compares to keys kjk_j of all tokens, producing softmax weights that mix the values vjv_j into a context vector. The core computation is a scaled dot-product followed by softmax and a weighted sum. This is computed efficiently with matrix operations on QQ, KK, and VV.

The intuition (an analogy)

Think of a classroom where each student holds a sticky note with facts. When one student wants to answer a question, they look at other students' notes. Their question is a query qiq_i. Each other student presents a key kjk_j that expresses what their note contains. The similarity between the question and a note determines how much the asking student reads that note. That reading is the weight, and the actual content copied is the value vjv_j. The final answer is a weighted combination of those notes.

This lets every token selectively copy information from any other token, whether that information is nearby or far away in the sequence.

How it actually works

The formal, compact formula for scaled dot-product attention is:

Attention(Q,K,V)=softmax ⁣(QKTdk)V\text{Attention}(Q,K,V) = \text{softmax}\!\left(\frac{QK^T}{\sqrt{d_k}}\right)V

Here QQ, KK, and VV are matrices of queries, keys, and values; dkd_k is the dimensionality of the keys. The inner product QKTQK^T gives all pairwise scores.

Worked example on "The cat sat on the mat". For clarity we use dk=2d_k=2 and set embeddings so that Q=K=VQ=K=V (a common simplification in a single self-attention layer).

Token order: The | cat | sat | on | the | mat We will compute attention for the query of the token "sat", which we label qsatq_{\text{sat}}.

Token vectors (rows are components):

tokenThecatsatonthe2mat
component 1110011
component 2011000.5

So qsat=[0,1]q_{\text{sat}} = [0, 1]. The dot-product scores sj=qsatkjs_j = q_{\text{sat}}\cdot k_j are:

tokenThecatsatonthe2mat
sjs_j (unscaled)011000.5

We scale by dk\sqrt{d_k} with dk=2d_k=2, so dk1.414\sqrt{d_k} \approx 1.414. The scaled scores are:

tokenThecatsatonthe2mat
sj/dks_j/\sqrt{d_k}00.7070.707000.354

Apply softmax to the scaled scores to get weights aja_j:

Exp of scaled scores (approx):

tokenThecatsatonthe2mat
esj/dke^{s_j/\sqrt{d_k}}1.0002.0282.0281.0001.0001.424

Softmax weights (normalized):

tokenThecatsatonthe2mat
aja_j0.1180.2390.2390.1180.1180.168

Finally, the attention output for the query qsatq_{\text{sat}} is the weighted sum of the values vjv_j:

outputsat=jajvj[0.643,  0.562].\text{output}_{\text{sat}} = \sum_j a_j v_j \approx [0.643,\;0.562].

This output is a context-aware vector for "sat" that mixes nearby lexical items like "cat" and "mat" according to the learned similarities.

Why we scale by dk\sqrt{d_k}

When dkd_k grows, typical dot products qikjq_i \cdot k_j have variance proportional to dkd_k. Without scaling, as dkd_k increases the raw scores become larger in magnitude and softmax turns into a near one-hot distribution, which hurts gradient flow. Dividing by dk\sqrt{d_k} normalizes the scale so the softmax operates in a stable range.

A small table showing how exe^x grows and why large xx dominate softmax:

xxexe^x
01.00
12.72
27.39
320.09

Because exe^x increases rapidly, a modest increase in raw dot-product magnitude can make one token dominate the weights. The dk\sqrt{d_k} factor keeps the xx values small enough that multiple tokens contribute.

Multi head attention

Multi head attention runs several parallel attention computations with different learned linear projections of QQ, KK, and VV. Each head has its own subspace (often smaller, e.g., dk=dmodel/hd_k = d_{\text{model}}/h). The separate heads can capture different kinds of relationships, such as syntactic connections in one head and semantic co-reference in another. The per-head outputs are concatenated and projected back to the model dimension.

Tradeoffs and failure modes

Attention is powerful but not perfect. It costs O(n2)O(n^2) time and memory in sequence length nn, which becomes prohibitive for very long contexts. Attention can also focus on spurious tokens if training data is biased, and it does not by itself encode ordering unless positional information is added. Finally, if queries, keys, and values are poorly scaled or initialized, training can be unstable.

Questions the interviewer might ask

Some follow-up questions you might get:

  • Why use dot product and not cosine similarity? Dot product is efficient to compute in matrix form and, with learned projections, the model can scale and shift vectors to represent useful comparisons; cosine would require extra normalization steps that complicate gradients.
  • What is the role of positional encoding? Attention is permutation invariant, so positional encodings add order information so the model can distinguish "cat sat" from "sat cat".
  • How does attention handle variable-length sequences? Attention naturally handles variable length because QKTQK^T just computes pairwise scores over whatever tokens are present; padding and masks are used to ignore positions.
  • Why have multiple heads instead of one wide head? Multiple heads let the model attend to different subspaces and relationships in parallel; splitting often helps learning more diverse patterns than a single head of equal total size.
  • How do masks work in attention? A mask adds large negative values (e.g., 109-10^9) to QKTQK^T before softmax to zero out unwanted positions, used for padding or causal/auto-regressive attention.
  • What happens if dk\sqrt{d_k} is omitted? Softmax can become extremely peaky for large dkd_k, causing vanishing gradients and learning difficulties.

Some things to note:

  • Attention parameters are learned via the linear projections that produce QQ, KK, and VV.
  • In practice we use residual connections and layer norm around attention for stable training.

What the interviewer is really testing

They want to see that you understand both the math and the intuition: how queries, keys, and values interact, why scaling matters, and what practical consequences attention has for computation and model behavior. Showing that you can walk through a small numeric example and mention implementation details like masking and complexity signals depth of understanding.

Further reading in the curriculum

Go deeper on the fundamentals behind this question.

Related questions

#attention#transformers#self-attention#query-key-value

How would you rate the quality of this article?

Prepare for your AI engineering interview

This is one of many detailed questions and explainers on StudyAIDesign. Browse the full set, work through the curriculum, and walk into your interview ready.

Follow along for new questions and explainers:Instagram