← Startups.com Interview Insights

Startups.com·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
Jun 2026

Summary

Technical screen for an ML Engineer role, focused entirely on Transformer attention mechanisms. Pretty deep dive, they clearly wanted someone who understood not just the math but the practical tradeoffs for inference.

Questions Asked (4)

Q1

Walk me through scaled dot-product attention mathematically, and explain why the scaling factor is there. What are the tensor shapes for Q, K, V in a batched setting?

System DesignTechnical Trade-offsAlgorithms & Data Structures
Author's notes

The math itself was fine, softmax over QK^T divided by sqrt(d_k) times V.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining the inputs and their shapes, then walk through the mathematical operations step-by-step: linear projections, dot product, scaling, softmax, and weighted sum. Explain the scaling factor's role in preventing softmax saturation and maintaining stable gradients, and finally clarify the batched tensor shapes.

Pro tip: Mention that the scaling factor is particularly important for large d_k, and that without it, the softmax gradients vanish, leading to slow training. Also, note that in practice, the scaling is often applied before the softmax for numerical stability.

1. Define inputs and projections

State that Q, K, V are obtained by linear projections of the input, and specify their shapes in a batched setting: (batch_size, seq_len, d_model) for input, and (batch_size, seq_len, d_k) for Q and K, (batch_size, seq_len, d_v) for V.

2. Compute attention scores

Explain that attention scores are computed as the dot product of Q and K transposed, resulting in a (batch_size, seq_len, seq_len) matrix. Then apply scaling by 1/sqrt(d_k).

3. Apply softmax and weighted sum

Describe applying softmax over the last dimension to get attention weights, then multiply by V to get the output of shape (batch_size, seq_len, d_v).

4. Explain the scaling factor

Discuss that scaling prevents the dot products from growing large in magnitude, which would push softmax into regions with tiny gradients, hindering learning. The factor 1/sqrt(d_k) assumes Q and K elements are independent with zero mean and unit variance.

5. Summarize with tensor shapes

Reiterate the shapes: Q: (B, T, d_k), K: (B, T, d_k), V: (B, T, d_v), scores: (B, T, T), output: (B, T, d_v). Mention that d_k and d_v can differ.

Key Points to Mention

  • Scaled dot-product attention formula: Attention(Q, K, V) = softmax(QK^T / sqrt(d_k)) V
  • Tensor shapes: Q, K: (batch_size, seq_len, d_k); V: (batch_size, seq_len, d_v); output: (batch_size, seq_len, d_v)
  • Scaling factor 1/sqrt(d_k) prevents softmax saturation and vanishing gradients
  • The scaling is derived from the variance of the dot product of random vectors
  • Softmax is applied along the last dimension (over keys)
  • In practice, d_k is often equal to d_v, but they can differ

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

How does multi-head attention differ from single-head attention? Cover the projection matrices, per-head computation, concatenation, and the output projection. Also walk through compute and memory complexity in terms of sequence length, number of heads, and head dimension.

System DesignTechnical Trade-offsAlgorithms & Data Structures
Author's notes

This one I felt okay about.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining single-head attention and then explain how multi-head attention extends it by projecting queries, keys, and values into multiple subspaces, applying attention in parallel, concatenating the results, and projecting back. Then analyze the computational and memory complexity, showing that multi-head attention with h heads of dimension d_k is equivalent in total dimension to single-head with d_model, but offers representational benefits at similar cost.

Pro tip: Emphasize that multi-head attention allows the model to jointly attend to information from different representation subspaces at different positions, which is crucial for capturing diverse relationships. Also, mention that while the total computation is similar to single-head with full dimension, the parallelizability and expressiveness make it a key innovation in Transformers.

1. Define single-head attention

Explain that single-head attention computes a weighted sum of values based on compatibility between queries and keys, using learned projection matrices W_Q, W_K, W_V to project inputs into a single representation space.

2. Introduce multi-head attention

Describe how multi-head attention uses h separate sets of projection matrices (W_Q^i, W_K^i, W_V^i) to project inputs into h lower-dimensional subspaces (dimension d_k = d_model / h), applies attention independently in each subspace, and then concatenates the outputs.

3. Explain concatenation and output projection

After computing attention outputs for each head, concatenate them along the feature dimension to get a vector of size d_model, then apply a final linear projection W_O to mix information across heads and produce the final output.

4. Analyze computational complexity

For sequence length n, number of heads h, and head dimension d_k, the per-head attention scores require O(n^2 d_k) time, and across h heads this sums to O(n^2 h d_k) = O(n^2 d_model). The projections also cost O(n d_model^2) overall, so total time is O(n^2 d_model + n d_model^2).

5. Analyze memory complexity

Memory for storing attention scores per head is O(n^2), and across h heads it's O(h n^2). Storing intermediate activations for all heads requires O(n h d_k) = O(n d_model) memory. The total memory is dominated by the attention matrices, O(h n^2), which can be large for long sequences.

Key Points to Mention

  • Multi-head attention uses h independent sets of projection matrices, each mapping to a lower-dimensional space (d_k = d_model / h).
  • Each head computes scaled dot-product attention: Attention(Q_i, K_i, V_i) = softmax(Q_i K_i^T / sqrt(d_k)) V_i.
  • Outputs from all heads are concatenated and then linearly projected with W_O to produce the final output of dimension d_model.
  • Computational complexity: O(n^2 d_model + n d_model^2) time, similar to single-head with full dimension, but with h times more parallelism.
  • Memory complexity: O(h n^2) for attention matrices, which can be reduced by optimizations like sparse attention or memory-efficient attention.
  • Multi-head attention enables the model to attend to different representation subspaces, improving expressiveness over single-head attention.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

What is grouped-query attention and how does it sit between standard multi-head attention and multi-query attention?

System DesignTechnical Trade-offs
Author's notes

Knew this one cold because I'd been reading about LLaMA and Mistral.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining standard multi-head attention (MHA) and multi-query attention (MQA), then position GQA as a generalization that interpolates between them by grouping query heads to share key/value heads. Explain the trade-offs in memory, speed, and quality, and give a concrete example of how the number of groups controls this balance.

Pro tip: Mention that GQA is used in production models like Llama 2 and 3 to reduce KV cache memory during inference, and that the number of groups is a hyperparameter you can tune based on latency and quality requirements.

1. Define standard multi-head attention (MHA)

Explain that in MHA, each attention head has its own query, key, and value projections, allowing diverse representation but requiring large memory for KV cache during inference.

2. Define multi-query attention (MQA)

Describe MQA as an extreme where all query heads share a single key and value head, drastically reducing KV cache size and speeding up inference but often hurting model quality.

3. Introduce grouped-query attention (GQA)

Position GQA as a middle ground: query heads are divided into groups, and each group shares one key/value head. This reduces KV cache compared to MHA while maintaining better quality than MQA.

4. Explain the trade-offs and use cases

Discuss how the number of groups controls the trade-off: more groups approach MHA (higher quality, more memory), fewer groups approach MQA (lower memory, faster, but potentially lower quality). Mention that GQA is used in large language models like Llama 2 and 3 for efficient inference.

Key Points to Mention

  • MHA: each head has independent Q, K, V projections; high memory for KV cache.
  • MQA: all heads share one K and V; minimal KV cache but potential quality drop.
  • GQA: query heads grouped; each group shares one K and V; interpolates between MHA and MQA.
  • Number of groups (G) is a hyperparameter: G=1 is MQA, G=number of heads is MHA.
  • Benefits: reduced KV cache memory, faster inference, and better quality than MQA.
  • Used in models like Llama 2 and 3, and supported in frameworks like Hugging Face Transformers.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q4

In a real deployment scenario, when would you actually choose MHA over GQA or MQA? Think about model quality, KV cache size, memory bandwidth, and any practical constraints.

Technical Trade-offsSystem Design
Author's notes

Honestly the most interesting part of the interview.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the trade-offs: MHA offers the highest model quality but at the cost of larger KV cache and memory bandwidth, while GQA and MQA reduce memory footprint and increase inference speed at some quality loss. Then, discuss specific scenarios where MHA is preferred, such as when model quality is paramount and memory/bandwidth constraints are less critical, or when the model size is small enough that KV cache is not a bottleneck.

Pro tip: Quantify the trade-offs: for example, mention that MHA can use up to num_heads times more KV cache than MQA, and that in practice, GQA often provides a sweet spot, but MHA might still be chosen for research or when fine-tuning quality is critical.

1. Define the attention variants

Briefly explain MHA, GQA, and MQA, focusing on how they differ in KV cache size and computational complexity.

2. Analyze trade-offs

Compare model quality, KV cache memory, and memory bandwidth requirements, noting that MHA has the highest quality but largest memory footprint.

3. Identify scenarios for MHA

Discuss situations where MHA is preferable, such as when memory is abundant, latency is not critical, or when the highest accuracy is needed.

4. Consider practical constraints

Mention deployment constraints like hardware limitations, batch size, sequence length, and whether the model is pre-trained or fine-tuned.

5. Conclude with a balanced recommendation

Summarize that MHA is chosen when quality outweighs efficiency, but in most production settings, GQA or MQA are preferred for scalability.

Key Points to Mention

  • MHA uses separate key and value heads per attention head, leading to larger KV cache and higher memory bandwidth.
  • GQA groups query heads to share key/value heads, reducing KV cache size while maintaining quality close to MHA.
  • MQA uses a single key/value head, minimizing KV cache but often at a noticeable quality drop.
  • MHA is beneficial when model quality is critical and memory/bandwidth are not bottlenecks, e.g., in research or small-scale deployments.
  • In large-scale deployments, KV cache size and memory bandwidth often dominate, making GQA or MQA more practical.
  • Consider the specific hardware: if memory is limited (e.g., edge devices), MHA is impractical; if memory is plentiful (e.g., high-end GPUs), MHA might be feasible.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.