Technical Interview Questions

Transformer Attention: Why Self-Attention Works

"Explain the self-attention mechanism in a Transformer. Why is it better than RNN for language modeling?"

Why Interviewers Ask This

Self-attention is the central idea behind modern LLMs — GPT, Claude, Llama all use it. Any AI/ML engineering interview in 2026 will probe whether you understand Q/K/V mechanics, not just "attention is a weighted sum".

How to Answer

  1. 1

    State the core operation: for each token, compute how much it should pay attention to every other token, then form its output as a weighted average of all token values.

  2. 2

    Q, K, V are three linear projections of the input embedding: query (what am I looking for?), key (what do I offer?), value (my actual content). Attention weight from token i to token j = softmax(Q_i · K_j / sqrt(d_k)).

  3. 3

    The sqrt(d_k) scaling matters: without it, dot products grow large with dimension and push softmax into regions with vanishing gradient. Scaling keeps variance roughly 1.

  4. 4

    Multi-head: instead of one attention, run h parallel attentions with smaller dimensions and concatenate. Each head learns to attend to different patterns — syntax, coreference, position — giving richer representations.

  5. 5

    Why better than RNN: parallelism (all tokens computed at once vs. RNN's sequential dependency), constant-length path between any two positions (helps long-range dependencies), and global context at every layer. Cost: O(n²) in sequence length, which is the scaling bottleneck for long contexts.

Sample Answer

Self-attention lets each token in a sequence look at every other token and decide how much to weigh them when computing its own output. For each token, you learn three linear projections of its embedding: Q (query, "what am I looking for?"), K (key, "what do I offer?"), and V (value, "my content to be mixed in"). Attention weights are computed as softmax of Q · K^T divided by sqrt(d_k), applied to V. Intuitively, token i's query vector is dot-producted against every other token's key vector to produce a similarity score; softmax turns these into weights summing to 1; then the output for token i is the weighted sum of all tokens' value vectors. The sqrt(d_k) scaling is easy to miss — without it, as dimension grows, the magnitude of dot products grows too, pushing softmax into saturation where gradients vanish and training stalls. Scaling keeps the distribution well-behaved. Multi-head attention runs h independent attentions in parallel, each with d/h-dimensional Q/K/V, then concatenates and projects. Different heads empirically learn different patterns — some heads attend to syntactic dependents, some to previous mention for coreference, some to positional neighbors. This gives the model richer representational capacity than one big attention. The crucial comparison to RNN: RNN processes tokens sequentially, so the path from token 1 to token 1000 is 999 recurrent steps — gradients vanish, long-range dependencies are hard, and training is inherently sequential. Self-attention makes every token see every other token in one layer, so the effective path length is 1, and all token computations can be parallelized on GPU. This is the reason Transformers train faster and learn longer dependencies better. The cost is O(n²) in sequence length for compute and memory, which is why long-context models use tricks like sparse attention, sliding windows, or linear approximations.

Pro Tips

  • Always mention scaled dot-product specifically, not just "attention" — the sqrt(d_k) is a frequent follow-up.

  • The "O(n²) sequence length bottleneck" is what every long-context paper tries to fix — mentioning it signals awareness of current research.

  • Multi-head intuition — different heads learn different patterns — is the right level of abstraction.

  • If you blank on Q/K/V terminology during a live interview, HireMe AI can surface them.

Frequently Asked Questions

Why three separate projections Q, K, V instead of one?

Different roles — query vs. key vs. content — need different learned representations. Sharing would force the same vector to do multiple jobs poorly.

What is positional encoding for?

Attention is permutation-invariant — it doesn't know token order. Positional encodings (sinusoidal or learned) add position information so order matters.

What's Flash Attention?

An I/O-aware attention kernel that computes attention without materializing the full N×N matrix, dramatically reducing memory and speeding up long sequences.

Worried About Going Blank in Your Interview?

HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.

Try HireMe AI Free