Core to Deep Dive
Revise Transformer architecture, attention mechanisms, tokenization, context windows, KV cache, and inference fundamentals.
Depth
Showing 5 of 5 questions
Short answer
The Transformer is a neural architecture that processes sequences using self-attention rather than recurrence. Its key components are multi-head attention, feed-forward networks, positional encoding, layer normalization, and residual connections. It excels at parallelising over sequence positions, making it efficient to train on large data.
Interview-ready answer
I would describe the Transformer as an encoder-decoder or decoder-only stack where every token can attend to every other token through self-attention. The attention mechanism computes queries, keys, and values from the input, then uses scaled dot-product attention to aggregate information across positions. Multi-head attention runs several attention copies in parallel so the model can learn different relationship types. Feed-forward layers add per-token nonlinear transformations, while residual connections and layer norm keep training stable. Positional encoding — either learned or sinusoidal — gives the model information about token order since attention itself is permutation invariant.
Common mistakes
Short answer
Each input token produces a query, key, and value vector. Queries and keys determine pairwise attention scores through a dot product, which are then normalised with softmax. The resulting weights are used to compute a weighted sum of the value vectors, producing the attention output for each token.
Interview-ready answer
I think of Q, K, V as a content-based lookup. The query represents what the current token is looking for, the key represents what each token offers, and the value is the information that will be passed along if a match is found. The attention score is the dot product of query and key, scaled by the inverse square root of the dimension to prevent vanishing gradients in the softmax. The weighted sum of values lets each token incorporate context from positions that have high attention scores, making the representation context-aware.
Common mistakes
Short answer
Tokenization converts raw text into integer tokens that the model can process. BPE (Byte Pair Encoding) iteratively merges the most frequent adjacent byte pairs in the training corpus to build a fixed-size vocabulary of subword units, balancing vocabulary size against coverage of rare words.
Interview-ready answer
Tokenisation bridges raw strings and model embeddings. BPE starts with individual bytes or characters as the base vocabulary, then counts adjacent token pairs in the corpus. The most frequent pair is merged into a new token, and the process repeats until the desired vocabulary size is reached. This lets the model handle any input via subword composition while keeping common words as single tokens. In practice, I verify that important domain terms are not split into meaningless pieces and consider adding domain-specific tokens to reduce sequence length and inference cost.
Common mistakes
Short answer
KV cache stores the key and value tensors from earlier attention computations during autoregressive generation. Since each new token only needs to attend to all previous tokens, recomputing all keys and values from scratch is wasteful. Caching them reduces the per-step computation from O(n²) to O(n) and significantly lowers latency.
Interview-ready answer
In autoregressive generation, each new token's attention step still needs keys and values from every prior position. Without KV cache, the model would recompute those tensors for every prefix position on every step. By caching them in memory after the first computation, each subsequent step only computes Q, K, V for the new token, then uses the full KV from the cache for attention. The trade-off is increased memory usage — the cache grows linearly with sequence length — which is why techniques like Paged Attention, sliding-window cache, or quantised caching are important for long sequences.
Common mistakes
Short answer
Temperature scales the logit distribution before softmax — lower values sharpen the distribution toward the most likely token, higher values flatten it for more randomness. Top-k limits sampling to the k highest-probability tokens, while top-p (nucleus) sampling selects the smallest set of tokens whose cumulative probability exceeds p. They are often combined to control creativity and coherence.
Interview-ready answer
Temperature divides logits by the temperature value before applying softmax. At low temperatures the probability mass concentrates on the top token, making output deterministic and repetitive. At high temperatures the distribution becomes more uniform, increasing diversity but risking incoherence. Top-k sets a fixed cutoff so long-tail tokens never get sampled, while top-p adapts the cutoff based on the distribution shape — narrow distributions keep fewer candidates, broad distributions keep more. In production I use top-p with a moderate temperature and tune the combination against the specific task, measuring both quality and diversity metrics.
Common mistakes