← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Senior

Senior
May 2026

Summary

Meta SWE coding round with a multi-head attention implementation question. Pretty niche for an interview but I guess ML infrastructure is a real thing there. No behavioral, just straight into the code.

Questions Asked (1)

Q1

Implement a basic multi-head attention mechanism from scratch. Given query, key, and value matrices plus a number of heads, return the weighted output as specified by the standard attention formula.

Algorithms & Data StructuresTechnical Trade-offsSystem Design
Author's notes

This one threw me a bit because I expected LeetCode-style graph or DP stuff and instead got asked to basically implement a core transformer building block.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the input shapes and the desired output shape, then derive the scaled dot-product attention formula step by step. Implement the linear projections for Q, K, V, split into heads, compute attention per head, concatenate, and apply the final output projection. Test with small random matrices to verify correctness.

Pro tip: Mention that you would use stable softmax (subtract max) and that you would vectorize the computation across heads for efficiency. Also, note that in practice you would use optimized libraries like PyTorch's scaled_dot_product_attention, but implementing from scratch demonstrates understanding.

1. Clarify inputs and outputs

Confirm the shapes of query, key, value matrices and the number of heads. Determine the expected output shape and whether any masking is required.

2. Project and split into heads

Apply linear projections to Q, K, V using weight matrices (if provided) or assume they are already projected. Reshape and transpose to split the last dimension into (num_heads, head_dim).

3. Compute scaled dot-product attention per head

For each head, compute attention scores = Q @ K^T / sqrt(head_dim), apply softmax (with numerical stability), then multiply by V to get the head output.

4. Concatenate heads and project output

Concatenate the outputs from all heads along the head dimension, then apply a final linear projection to produce the final output.

5. Test and validate

Test with small random inputs and verify shapes and numerical correctness. Compare against a reference implementation if possible.

Key Points to Mention

  • Scaled dot-product attention formula: softmax(QK^T / sqrt(d_k)) V
  • Splitting into multiple heads allows the model to attend to different representation subspaces
  • Importance of numerical stability in softmax (subtracting max)
  • Efficient implementation using batch matrix multiplication and reshaping
  • Handling of masks (e.g., causal mask for autoregressive models) if applicable
  • Final output projection after concatenating heads

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