Why did transformers replace RNNs and LSTMs for language modeling?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
Two reasons: parallelism and gradient paths. RNNs process tokens sequentially - hidden state t depends on t−1 - so training can't parallelize across the sequence dimension, and information from early tokens must survive hundreds of recurrent steps, where it degrades even with LSTM gating. Transformers compute attention over all positions simultaneously: one matrix multiply processes the whole sequence, so training saturates GPUs/TPUs, and any token can attend to any other token through a path of length one, so long-range dependencies don't decay through recurrence.
Full worked answer with diagram
Two reasons: parallelism and gradient paths. RNNs process tokens sequentially - hidden state t depends on t−1 - so training can't parallelize across the sequence dimension, and information from early tokens must survive hundreds of recurrent steps, where it degrades even with LSTM gating. Transformers compute attention over all positions simultaneously: one matrix multiply processes the whole sequence, so training saturates GPUs/TPUs, and any token can attend to any other token through a path of length one, so long-range dependencies don't decay through recurrence.
The parallelism point is the economically decisive one. Next-token prediction with a causal mask gives you a training signal at every position of a sequence from a single forward pass - an n-token document yields n loss terms computed in parallel. That efficiency is what made training on trillions of tokens feasible, which is what made scaling laws discoverable, which is what produced modern LLMs.
Worth noting the honest tradeoffs: RNNs have O(1) state per step at inference, while transformers pay O(n²) attention compute and an O(n) KV cache that grows with context. That's why there's active work on state-space models (Mamba-style architectures) and hybrid designs - they revisit the recurrent idea with parallelizable training. But for training-time throughput and quality at scale, attention won, and everything at the frontier today is transformer-based or transformer-hybrid.
A good answer also mentions that self-attention is permutation-invariant, so transformers need explicit positional information (RoPE etc.) - the sequential order RNNs got for free has to be injected.
Worth sketching. Two rows on the board: it makes the gradient path length and the parallelism argument visible at the same time.
flowchart LR
R1["RNN: token 1"] --> R2["token 2"] --> R3["token n"] --> RL["loss<br/>gradient crosses n recurrent steps"]
X["Transformer: all n tokens at once"] --> ATT["self-attention, every pair<br/>path length 1"] --> XL["n loss terms from one forward pass"]
What a good answer covers
- Two reasons: parallelism and gradient paths
- RNNs process tokens sequentially - hidden state t depends on t−1 - so training can't parallelize across…
- Transformers compute attention over all positions simultaneously
- The parallelism point is the economically decisive one
- Next-token prediction with a causal mask gives you a training signal at every position of a sequence…
In the glossary: Attention · Causal mask · Scaling laws · KV cache · RoPE
Interviewer follow-ups to expect
What's the inference-time cost transformers pay that RNNs don't?
Why does causal masking make training so sample-efficient per forward pass?
What are state-space models trying to recover?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep