How do sinusoidal positional encodings work, and how do they compare to learned positional embeddings?
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
Self-attention is permutation-invariant, so position must be injected explicitly. The original transformer added sinusoidal encodings to token embeddings: for position pos and dimension pair i, PE(pos, 2i) = sin(pos / 10000^(2i/d)) and PE(pos, 2i+1) = cos(pos / 10000^(2i/d)). Each dimension pair oscillates at a different frequency, geometrically spaced from wavelength 2π up to ~10000·2π - like a binary counter in continuous form, or clock hands ticking at different speeds. Low-frequency dims distinguish coarse position; high-frequency dims distinguish adjacent tokens.
Full worked answer
Self-attention is permutation-invariant, so position must be injected explicitly. The original transformer added sinusoidal encodings to token embeddings: for position pos and dimension pair i, PE(pos, 2i) = sin(pos / 10000^(2i/d)) and PE(pos, 2i+1) = cos(pos / 10000^(2i/d)). Each dimension pair oscillates at a different frequency, geometrically spaced from wavelength 2π up to ~10000·2π - like a binary counter in continuous form, or clock hands ticking at different speeds. Low-frequency dims distinguish coarse position; high-frequency dims distinguish adjacent tokens.
Two elegant properties: no learned parameters (works for any position, including ones never seen in training - in theory), and for any fixed offset k, PE(pos+k) is a linear function of PE(pos) - a rotation in each frequency pair - so relative offsets are, in principle, easy for attention to compute. In practice extrapolation beyond trained lengths still degrades, because the model never learned to use the unseen combinations.
Learned absolute embeddings (GPT-2, BERT): just an embedding table indexed by position, added to token embeddings. Simpler, lets the model learn whatever positional structure helps, and empirically matched sinusoidal at trained lengths. Hard limitations: the table has a fixed size - position 1025 simply has no embedding in a 1024-trained model - and each position index is trained only as often as it appears, so late positions are under-trained.
Both share a deeper weakness: they encode absolute position, while language mostly cares about relative position ("the adjective before this noun"), and adding position to the embedding entangles content with position throughout the residual stream. That motivated the modern generation of methods that inject relative position directly into the attention computation - RoPE (rotate Q/K per position, so scores depend on offset) and ALiBi (distance-penalty bias on scores) - which is what current models actually use. Sinusoidal/learned absolute embeddings are now mostly of historical and conceptual importance.
What a good answer covers
- Self-attention is permutation-invariant, so position must be injected explicitly
- The original transformer added sinusoidal encodings to token embeddings
- Each dimension pair oscillates at a different frequency, geometrically spaced from wavelength 2π up to…
- Low-frequency dims distinguish coarse position; high-frequency dims distinguish adjacent tokens
- Two elegant properties: no learned parameters (works for any position, including ones never seen in…
Key terms: sinusoidal encodings · Learned absolute embeddings · absolute · relative
In the glossary: Attention · Residual stream · RoPE · ALiBi
Interviewer follow-ups to expect
Why is relative position more useful than absolute for language?
Both share a deeper weakness: they encode absolute position, while language mostly cares about relative position ("the adjective before this noun"), and adding position to the embedding entangles content with position throughout the residual stream. That motivated the modern generation of methods that inject relative position directly into the attention computation - RoPE (rotate Q/K per position, so scores depend on offset) and ALiBi (distance-penalty bias on scores) - which is what current models actually use. Sinusoidal/learned absolute embeddings are now mostly of historical and conceptual importance.
Show why a fixed offset is a linear transform of a sinusoidal encoding. Why does adding position to embeddings entangle content and position, and why does that matter?
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