Machine Learning Engineer Interview Question

Explain how self attention works in a transformer, out loud, without drawing anything.

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

Each token is projected into a query, a key, and a value vector. You score every query against every key with a dot product, scale by the square root of the head dimension, softmax the scores into weights, and take the weighted sum of the values. Each token ends up holding a representation mixed from whichever other tokens it found relevant. Multiple heads do this in parallel with different projections.

Why interviewers ask this

Transformers underpin almost everything shipping now, so interviewers expect a mechanical explanation rather than an analogy about attention being like focus. The scaling factor and the softmax are the details people forget, and the quadratic cost in sequence length is the follow up they are steering toward, because that is what constrains context length and serving cost in practice.

How to structure your answer

  • Describe the query, key, and value projections in one sentence.
  • Walk the dot product, scaling, softmax, weighted sum in order.
  • Say what multiple heads add.
  • Note the quadratic cost in sequence length and why it matters.

Example answer

Spoken example, first person

Every token embedding gets multiplied by three learned matrices to produce a query, a key, and a value. The query is what this token is looking for, the key is what it advertises, the value is what it passes along if selected. You take the dot product of each query with every key, which gives a compatibility score for every pair of positions. Divide by the square root of the head dimension, because otherwise the dot products grow with dimensionality and push the softmax into a region where it is effectively a hard argmax with vanishing gradients. Softmax across the row turns those scores into weights that sum to one, then you take the weighted sum of the value vectors. So each token walks away holding a blend of the tokens it cared about. Multiple heads run that whole operation in parallel with separate projections, so one head can track syntax while another tracks coreference, and the outputs get concatenated and projected. The cost worth naming is that the score matrix is sequence length squared, which is exactly why long context serving is expensive and why flash attention and KV caching matter so much.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • What does the KV cache store during autoregressive decoding, and why?
  • How does causal masking change the computation?
  • What do the sub quadratic attention variants give up?

Related machine learning engineer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot