Why is self-attention O(n^2) and what are the implications?
Tests the attention matrix bottleneck. Strong answers note QK^T yields an N×N matrix, creating quadratic compute and memory that blocks long documents and high-res images. Red flag: confusing model size with activation memory.
What's really being asked
Whether you understand the self-attention computational graph at a systems level. Interviewers care if you can trace why sequence length creates a quadratic bottleneck, distinguish activation memory from parameter memory, and reason about hardware limits for long documents or high resolution images.
The full answer
First, the math behind the bottleneck. For a sequence of length n, the query and key matrices each have shape n by d. Multiplying Q by K transpose produces an n by n attention score matrix. Computing softmax over rows and multiplying by the n by d value matrix keeps the operation count proportional to n squared. Second, the memory story. Standard implementations materialize that full n by n matrix in high bandwidth memory to apply softmax, dropout, and masking. For n of 100000 tokens, a float32 attention matrix alone consumes roughly 40 GB, exceeding most single GPU capacities. Third, the practical implication. Very long documents, high resolution images flattened into long patch sequences, or long audio waveforms quickly hit the memory wall. Training becomes impossible without model parallelism or gradient checkpointing, and inference latency grows quadratically. Fourth, the nuance that memory is not fundamentally quadratic. Techniques like memory efficient attention or the approach in Self-attention Does Not Need O(n^2) Memory show that by recomputing attention rather than storing the full matrix, activation memory can be reduced to O(n), though the number of FLOPs remains O(n^2).
The mistakes people make
Confusing the O(n^2) activation cost with the model parameter count, which is independent of sequence length. Claiming feed forward layers dominate the cost, they scale linearly with n and are not the bottleneck. Asserting that standard attention can be linear time without approximation, which mixes up the exact softmax attention with linear approximations like Performer or Linformer. Ignoring memory bandwidth and only discussing FLOPs, which misses why GPUs actually run out of resources.
Likely follow ups
How does FlashAttention exploit tiling to reduce high bandwidth memory usage without changing asymptotic compute? What is the complexity of sparse patterns like local sliding window attention? How would you partition a 100k token sequence across multiple GPUs? Can you calculate the exact activation memory for a given batch size, heads, and sequence length?
A concrete example
Take a vision transformer on a 1024 by 1024 image with 16 by 16 patches. That yields n of 4096 patches. The attention matrix has 4096 squared or about 16.8 million entries. At float16 that is roughly 32 MB per head. With 16 heads and batch size 32, a single layer needs over 16 GB of activation memory just for the attention scores. A 24 layer model would require hundreds of gigabytes without checkpointing or memory efficient kernels, which is why high resolution ViTs quickly become impractical under standard attention.
Interview question
Which statement accurately captures the computational and memory complexity of exact self-attention as sequence length grows?
- a.FLOPs grow as O(n^2) from the QK^T attention score matrix, while activation memory can be reduced to O(n) by recomputing rather than storing the full matrix.Correct
- b.For long sequences, feed-forward layers dominate cost because their activation memory scales quadratically with sequence length.
- c.Both FLOPs and activation memory are fundamentally O(n^2), and neither can be reduced without approximation.
- d.FLOPs scale as O(n^2) but activation memory is always O(n) because only the Q, K, and V matrices must be stored.
Why? this is the answer
Exact self-attention must perform O(n^2) FLOPs to compute the attention score matrix, but activation memory is not fundamentally quadratic: by recomputing scores instead of materializing the full n×n matrix in HBM, memory can be reduced to O(n). Option C is tempting because standard implementations often do store the full matrix, leading many to assume that O(n^2) memory is unavoidable even without approximation.
Just read this? Test yourself on what you have been reading.
Read the original → huggingface.co
- #llms
- #transformers
- #attention
- #complexity
- #systems
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on llms — each one lists the topics its interview covers.
See open roles