← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Senior

Senior
May 2026

Summary

Meta Research Scientist technical screen focused almost entirely on the transformer attention mechanism. Dense topic, they went pretty deep and I wasn't totally ready for some of the optimization side of things.

Questions Asked (7)

Q1

Walk through the self-attention mechanism in transformers, including Q/K/V projections, scaled dot-product attention, and why you divide by the square root of d_k.

Technical Trade-offsAlgorithms & Data Structures
Author's notes

I started with the matrix projections and felt pretty solid there.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining the high-level purpose of self-attention: to compute context-aware representations by allowing each token to attend to all others. Then walk through the Q/K/V projections, the scaled dot-product attention formula, and finally justify the scaling factor with a variance argument. Use a concrete example or analogy to make it intuitive.

Pro tip: Mention that the scaling by sqrt(d_k) is crucial for stable gradients, especially with large d_k, and that without it, softmax saturates and learning slows. This shows you understand the practical implications beyond the math.

1. Motivation for Self-Attention

Explain that self-attention allows each token to weigh the importance of all other tokens in the sequence, capturing long-range dependencies. Contrast with RNNs/CNNs to highlight parallelization and flexibility.

2. Q/K/V Projections

Describe how each input token embedding is linearly projected into three vectors: Query (Q), Key (K), and Value (V) using learned weight matrices. Explain their roles: Q and K determine attention weights, V carries the information to be aggregated.

3. Scaled Dot-Product Attention

Present the formula: Attention(Q,K,V) = softmax(QK^T / sqrt(d_k)) V. Explain that QK^T computes similarity scores between queries and keys, softmax converts to probabilities, and the weighted sum of V produces the output.

4. Why Divide by sqrt(d_k)

Explain that for large d_k, the dot products grow large in magnitude, pushing softmax into regions with tiny gradients. Dividing by sqrt(d_k) scales the dot products to have variance ~1, stabilizing gradients and improving training.

5. Complexity and Trade-offs

Mention that self-attention has O(n^2) complexity in sequence length, which is a trade-off for its expressiveness. Briefly note optimizations like sparse attention or linear attention for long sequences.

Key Points to Mention

  • Self-attention computes pairwise interactions between all tokens, enabling global context.
  • Q, K, V are learned linear projections of the input embeddings.
  • Attention scores are computed as dot products between Q and K, then scaled and softmaxed.
  • Scaling by sqrt(d_k) prevents softmax saturation and stabilizes gradients.
  • The output is a weighted sum of V, where weights are the attention probabilities.
  • Self-attention is parallelizable and has O(n^2) time and memory complexity.

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 work and what's the motivation for splitting into multiple heads rather than using a single large attention operation?

System DesignTechnical Trade-offs
Author's notes

Explained the concatenation and final projection fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining the mechanics of multi-head attention: how queries, keys, and values are linearly projected into multiple lower-dimensional subspaces, attention is computed in parallel, and outputs are concatenated and projected. Then motivate it by discussing the benefits of attending to information from different representation subspaces at different positions, and contrast with single-head attention in terms of expressiveness and computational efficiency.

Pro tip: Emphasize that multi-head attention is not just about parallelism but about enabling the model to jointly attend to information from different representation subspaces, which is crucial for capturing diverse linguistic relationships. Also, mention that the total computational cost is similar to single-head with full dimensionality, making it an efficient design choice.

1. Define Multi-Head Attention

Explain that it splits the model dimension into multiple heads, each performing scaled dot-product attention independently on projected queries, keys, and values.

2. Describe the Computation

Walk through the steps: linear projections for each head, parallel attention computations, concatenation of outputs, and a final linear projection.

3. Motivate Multiple Heads

Discuss how multiple heads allow the model to attend to different types of information (e.g., syntactic vs. semantic) and capture diverse relationships, which a single head might miss.

4. Compare with Single-Head

Contrast with a single large attention operation: single-head averages attention, potentially losing specialization, while multi-head maintains representational diversity without increasing computational cost significantly.

5. Highlight Trade-offs and Practical Considerations

Mention that while multi-head adds complexity, it improves performance and is standard in Transformers; also note that head count is a hyperparameter and too many heads can reduce per-head dimension, affecting expressiveness.

Key Points to Mention

  • Scaled dot-product attention and the role of queries, keys, values
  • Linear projections to lower-dimensional subspaces for each head
  • Parallel computation and concatenation of head outputs
  • Ability to attend to different representation subspaces and positions
  • Computational efficiency: total cost similar to single-head with full dimension
  • Empirical success in Transformer architectures (e.g., BERT, GPT)

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

Q3

How do positional encodings work and why are they necessary in transformer architectures?

Algorithms & Data Structures
Author's notes

Pretty straightforward.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining that transformers process all tokens in parallel, so they lack inherent sequence order, making positional encodings necessary to inject position information. Then describe how positional encodings are added to input embeddings, and compare common methods like sinusoidal and learned embeddings, highlighting their properties and trade-offs.

Pro tip: Mention that sinusoidal encodings allow the model to extrapolate to longer sequences than seen during training, which is a key advantage for production systems at Meta. Also, note that relative positional encodings (e.g., in Transformer-XL) can be more effective for certain tasks, showing awareness of modern variants.

1. Explain the need for positional information

Describe how transformers process tokens in parallel without recurrence or convolution, so they have no inherent notion of order. Without positional encodings, the model would treat 'dog bites man' and 'man bites dog' identically.

2. Define positional encodings

Explain that positional encodings are vectors added to input embeddings to inject information about the position of each token in the sequence. They have the same dimension as embeddings, allowing element-wise addition.

3. Describe common methods

Compare sinusoidal encodings (fixed, using sine and cosine functions of different frequencies) and learned positional embeddings (trainable parameters). Mention that sinusoidal encodings can extrapolate to longer sequences, while learned embeddings are simpler but limited to training length.

4. Discuss properties and trade-offs

Highlight that sinusoidal encodings are deterministic and allow the model to attend by relative positions due to linear relationships. Learned embeddings are flexible but may overfit and cannot handle unseen lengths. Mention relative positional encodings as an alternative that encodes pairwise distances.

5. Conclude with importance

Summarize that positional encodings are crucial for transformers to understand sequence order, enabling them to perform tasks like language modeling, translation, and any task where order matters.

Key Points to Mention

  • Transformers lack inherent sequence order due to parallel processing.
  • Positional encodings are added to input embeddings to inject position information.
  • Sinusoidal encodings use sine and cosine functions of different frequencies.
  • Learned positional embeddings are trainable parameters but limited to training sequence length.
  • Sinusoidal encodings allow extrapolation to longer sequences and enable relative position attention.
  • Relative positional encodings (e.g., in Transformer-XL) encode pairwise distances and can be more effective for certain tasks.

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

Q4

What are attention masks and how do causal masks differ from padding masks?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Causal masking sets future positions to negative infinity before softmax so they zero out, used in autoregressive decoding.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining attention masks as additive or multiplicative tensors that control which positions a model can attend to. Then contrast causal masks (preventing future token attention) with padding masks (ignoring padding tokens), emphasizing their distinct purposes and how they can be combined in practice.

Pro tip: Mention that in frameworks like PyTorch, masks are often boolean or additive, and that combining causal and padding masks requires careful broadcasting to avoid shape mismatches. This shows hands-on experience with real implementations.

1. Define attention masks

Explain that attention masks are used to prevent the model from attending to certain positions, typically by adding a large negative value before softmax or by setting attention weights to zero.

2. Explain causal masks

Describe causal masks as lower-triangular matrices that ensure each position can only attend to previous positions, crucial for autoregressive generation.

3. Explain padding masks

Describe padding masks as masks that ignore padding tokens in a batch, ensuring they don't affect attention computations, often derived from sequence lengths.

4. Compare and contrast

Highlight that causal masks are about temporal order (preventing future information), while padding masks are about variable-length sequences (ignoring non-informative tokens).

5. Discuss combination and implementation

Explain how both masks can be combined (e.g., by adding or logical OR) and mention practical considerations like broadcasting and efficiency.

Key Points to Mention

  • Attention masks are applied before softmax to set attention weights to zero (or negative infinity).
  • Causal masks are typically lower-triangular matrices, ensuring autoregressive property.
  • Padding masks are derived from sequence lengths and mask out padding tokens.
  • Causal masks are used in decoders (e.g., GPT), while padding masks are used in both encoders and decoders for batched sequences.
  • Combining masks: often done by adding (if additive) or logical OR (if boolean), but need to handle broadcasting.
  • Efficiency: masks can be precomputed and reused, and sparse masks can save computation.

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

Q5

What is the computational complexity of self-attention and what are the main bottlenecks at scale?

System DesignTechnical Trade-offs
Author's notes

Quadratic in sequence length, O(n^2 * d).

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by stating the computational complexity of self-attention in terms of sequence length and model dimension, then discuss the quadratic scaling with sequence length as the primary bottleneck. Follow up with memory and compute bottlenecks at scale, and mention common optimizations like sparse attention or linear approximations.

Pro tip: Quantify the impact: for a sequence length of 10,000, the attention matrix has 100 million entries, which is infeasible for large batches. This shows you understand real-world constraints.

1. Define Complexity

State that self-attention has O(n^2 * d) time and O(n^2) memory complexity, where n is sequence length and d is model dimension.

2. Identify Bottlenecks

Explain that the quadratic scaling with sequence length is the main bottleneck, leading to high memory usage and slow computation for long sequences.

3. Discuss Scale Implications

Mention that at scale, the attention matrix becomes too large to fit in memory, requiring techniques like gradient checkpointing or distributed training.

4. Mention Optimizations

List common optimizations such as sparse attention, low-rank approximations, or linear attention to reduce complexity.

5. Relate to System Design

Connect to system design trade-offs, such as choosing between model quality and efficiency, and how Meta might handle these in production.

Key Points to Mention

  • Quadratic complexity O(n^2) in sequence length for both time and memory.
  • Memory bottleneck: attention matrix of size n x n.
  • Compute bottleneck: matrix multiplications for queries, keys, values.
  • Optimizations: sparse attention, linear attention, low-rank approximations.
  • Trade-offs: accuracy vs. efficiency, hardware constraints.
  • Real-world examples: long documents, high-resolution images.

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

Q6

How does self-attention differ from cross-attention, and where is cross-attention used?

Algorithms & Data StructuresSystem Design
Author's notes

Cross-attention has queries from one sequence and keys/values from another, classic example being encoder-decoder architectures where the decoder attends to encoder outputs.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining both mechanisms in terms of query, key, and value sources, then contrast them with a concrete example. Explain that self-attention uses the same sequence for Q, K, V, while cross-attention uses different sequences for Q and K/V. Finally, list common applications of cross-attention in real-world systems.

Pro tip: Tie the explanation to a real system like a transformer decoder or a multimodal model, and mention how cross-attention enables conditioning on external information, which is crucial for tasks like machine translation and image captioning.

1. Define self-attention

Explain that self-attention computes attention within a single sequence, where queries, keys, and values all come from the same input. Mention that it captures intra-sequence dependencies.

2. Define cross-attention

Explain that cross-attention computes attention between two different sequences: queries from one sequence (e.g., decoder) and keys/values from another (e.g., encoder). It captures inter-sequence dependencies.

3. Contrast the two

Highlight the key difference: source of Q, K, V. In self-attention, all come from the same sequence; in cross-attention, Q comes from one sequence and K, V from another. Also note that cross-attention allows conditioning on external context.

4. Provide examples of cross-attention usage

List applications: transformer decoder attending to encoder outputs in machine translation, multimodal models (e.g., image captioning where text attends to image features), and retrieval-augmented generation.

5. Summarize with a practical implication

Conclude by noting that cross-attention is essential for tasks requiring alignment between different modalities or sequences, and it enables flexible integration of information.

Key Points to Mention

  • Self-attention: Q, K, V from same sequence; cross-attention: Q from one sequence, K and V from another.
  • Self-attention captures intra-sequence relationships; cross-attention captures inter-sequence relationships.
  • Cross-attention is used in transformer decoders to attend to encoder outputs (e.g., in translation).
  • Cross-attention is used in multimodal models (e.g., text-to-image, image captioning) to align different modalities.
  • Cross-attention enables conditioning on external information, such as in retrieval-augmented generation.
  • Computational complexity: cross-attention can be more expensive if the external sequence is long, as it scales with the product of sequence lengths.

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

Q7

Can you explain FlashAttention and why it's faster than standard attention implementations?

System DesignTechnical Trade-offs
Author's notes

This is where I got a bit shaky.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining FlashAttention as an IO-aware exact attention algorithm that reduces memory reads/writes by tiling and recomputation. Then explain the key optimizations (tiling, recomputation, kernel fusion) and contrast with standard attention's memory bottlenecks. Finally, discuss the trade-offs and why it matters for long sequences and large models.

Pro tip: Emphasize that FlashAttention is not an approximation—it computes exact attention—and highlight the practical impact on training speed and memory usage, which resonates with Meta's large-scale AI infrastructure.

1. Define FlashAttention

State that FlashAttention is an exact attention algorithm that reduces memory access overhead by using tiling and recomputation, making it faster and more memory-efficient than standard attention.

2. Explain Standard Attention Bottlenecks

Describe how standard attention materializes the full N×N attention matrix, leading to O(N^2) memory usage and excessive HBM reads/writes, which becomes a bottleneck for long sequences.

3. Describe FlashAttention Optimizations

Detail the key techniques: tiling the attention computation into blocks that fit in SRAM, recomputing attention scores during the backward pass instead of storing them, and fusing operations to minimize HBM access.

4. Quantify Benefits and Trade-offs

Mention that FlashAttention achieves significant speedups (2-4x) and memory savings (up to 20x) for long sequences, but may require custom CUDA kernels and careful implementation for different hardware.

5. Connect to Real-World Impact

Relate how FlashAttention enables training and inference of large language models with longer context windows, which is crucial for Meta's AI products and research.

Key Points to Mention

  • IO-awareness: minimizing HBM reads/writes by keeping data in SRAM.
  • Tiling: dividing the attention matrix into blocks that fit in on-chip memory.
  • Recomputation: recomputing attention scores during backward pass to avoid storing the full matrix.
  • Exactness: FlashAttention computes exact attention, not an approximation.
  • Memory complexity: reduces memory from O(N^2) to O(N) for attention.
  • Speedup: achieves 2-4x speedup on common sequence lengths and up to 20x memory savings.

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