← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Senior

Senior
May 2026

Summary

Technical screen for a Research Engineer role at Meta. One meaty implementation question that took up basically the whole session, walked through the math before touching any code.

Questions Asked (1)

Q1

Implement scaled dot-product self-attention from scratch using NumPy or PyTorch. Given query, key, and value tensors of shape (batch, seq_len, d_k), compute the attention output using softmax(QK^T / sqrt(d_k)) * V. Walk through the math before writing any code, and optionally handle causal masking.

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

They asked me to explain the formula before coding, which I actually appreciated because I'd fumbled this before by jumping straight to implementation and getting the matrix dimensions wrong.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, explain the mathematical formulation of scaled dot-product attention, including the role of the scaling factor and optional causal masking. Then, implement it step-by-step in NumPy or PyTorch, ensuring numerical stability and efficiency. Finally, discuss trade-offs and potential optimizations.

Pro tip: Mention that using `torch.softmax` or `np.exp` with max subtraction improves numerical stability, and that causal masking can be implemented by setting future positions to -inf before softmax.

1. Explain the math

Describe the formula: Attention(Q, K, V) = softmax(QK^T / sqrt(d_k)) V. Explain each component: Q, K, V shapes, scaling factor, softmax, and optional causal mask.

2. Outline implementation steps

List the steps: compute QK^T, scale by sqrt(d_k), apply causal mask if needed, apply softmax, and multiply by V. Mention handling batch dimensions.

3. Write code

Implement in NumPy or PyTorch, using efficient matrix operations. For PyTorch, use torch.matmul and torch.softmax; for NumPy, use np.matmul and stable softmax.

4. Discuss numerical stability and masking

Explain how to avoid overflow in softmax by subtracting the max, and how to apply causal masking by setting masked positions to -inf before softmax.

5. Analyze trade-offs and optimizations

Discuss time and space complexity, and mention optimizations like using fused kernels or memory-efficient attention for large sequences.

Key Points to Mention

  • Scaling factor 1/sqrt(d_k) prevents softmax saturation
  • Causal masking ensures autoregressive property
  • Numerical stability via max subtraction in softmax
  • Batch matrix multiplication for efficiency
  • Time complexity O(n^2 d) and space complexity O(n^2)
  • Potential optimizations: FlashAttention, memory-efficient attention

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