← Openai Interview Insights

Openai·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
Apr 2026

Summary

OpenAI ML engineer interview with a pretty brutal debugging exercise. They handed me a broken Transformer implementation and wanted me to find and fix four distinct bugs across the architecture. No hints, just code and a timer.

Questions Asked (4)

Q1

Given a Transformer implementation with four planted bugs, find and fix the shape error in the feed-forward (MLP) block. Explain why the broken version fails during training.

Root Cause AnalysisTechnical Trade-offs
Author's notes

This one I actually caught fastest.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by tracing the tensor shapes through the MLP block, comparing the broken and correct implementations to pinpoint the mismatch. Then explain how the shape error causes a runtime failure or silent incorrectness during training, and propose a fix with justification.

Pro tip: Demonstrate that you understand the MLP's role in Transformers and the importance of shape consistency; mention that such bugs often manifest as cryptic errors, so systematic shape checking is key.

1. Identify the MLP block structure

Recall that a Transformer MLP typically consists of two linear layers with a nonlinearity in between, often expanding then projecting back to the model dimension.

2. Trace tensor shapes

Manually compute the expected shape after each operation (input, first linear, activation, second linear) and compare with the broken code to find the mismatch.

3. Diagnose the failure mode

Determine whether the shape error causes an immediate runtime error (e.g., matrix multiplication dimension mismatch) or a silent issue (e.g., broadcasting leading to wrong gradients).

4. Propose and validate the fix

Adjust the offending layer's dimensions or reshape operation to restore shape consistency, and explain why this resolves the training failure.

5. Explain training impact

Describe how the bug would prevent the model from learning, either by crashing the training loop or by producing incorrect outputs that hinder optimization.

Key Points to Mention

  • The MLP block's role: expanding to a higher dimension (often 4x) and projecting back.
  • Shape consistency: input (batch, seq_len, d_model) -> (batch, seq_len, d_ff) -> (batch, seq_len, d_model).
  • Common bugs: incorrect output dimension of first linear layer, missing or wrong reshape, or misordered operations.
  • Runtime error: matrix multiplication requires inner dimensions to match; a mismatch throws an error.
  • Silent bug: broadcasting can mask shape errors but lead to incorrect computations and poor training.
  • Fix: ensure the second linear layer's input dimension matches the first's output dimension (d_ff).

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

Q2

Identify and fix the attention mask bug. The mask may be applied with the wrong shape, wrong sign, or to the wrong tensor entirely. How does this manifest during training?

Root Cause AnalysisAlgorithms & Data Structures
Author's notes

Spent way too long on this one.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, explain how attention masks work and the three common bugs: wrong shape, wrong sign, and wrong tensor. Then, describe how each bug manifests during training, such as loss not decreasing, NaN gradients, or attention weights not summing to 1. Finally, outline a systematic debugging process to identify and fix the issue.

Pro tip: Emphasize that you always validate mask shapes and values with assertions or unit tests before training, and monitor attention entropy and gradient norms to catch mask issues early.

1. Understand the role of attention masks

Explain that attention masks prevent the model from attending to padding tokens or future tokens in autoregressive settings. They are typically added to attention logits before softmax, with -inf for masked positions.

2. Identify the three common mask bugs

Wrong shape: mask not broadcastable to attention scores (e.g., missing head dimension). Wrong sign: using +inf or large positive values instead of -inf, causing softmax to focus on masked positions. Wrong tensor: applying mask to values instead of logits, or to the wrong attention layer.

3. Analyze training manifestations

Wrong shape: runtime error or silent broadcasting that masks unintended positions. Wrong sign: loss diverges or model attends to padding/future tokens, leading to poor generalization. Wrong tensor: gradients vanish or explode, loss plateaus, or model fails to learn dependencies.

4. Debug systematically

Check mask shape against attention scores, verify mask values are 0 and -inf, and ensure mask is added to logits before softmax. Use small synthetic examples to validate attention outputs.

5. Fix and validate

Correct the mask shape, sign, or application point. Add assertions and unit tests to catch future issues. Monitor attention entropy and gradient norms during training to confirm the fix.

Key Points to Mention

  • Attention mask shape must be broadcastable to attention scores: typically (batch_size, num_heads, seq_len, seq_len) or (batch_size, 1, 1, seq_len).
  • Mask values should be 0 for allowed positions and -inf (or a large negative number) for masked positions before softmax.
  • Mask must be applied to attention logits (before softmax), not to the values or after softmax.
  • Wrong sign (e.g., +inf) causes softmax to assign high probability to masked positions, leading to loss divergence or NaN.
  • Wrong tensor (e.g., masking values) can cause gradients to vanish or explode, and the model may fail to learn.
  • Use assertions and unit tests to validate mask shape and values, and monitor attention entropy and gradient norms during training.

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

Q3

Find the loss backpropagation bug in the code. The gradient may not be flowing correctly due to a detached tensor, premature .item() call, wrong reduction dimension, or an in-place operation breaking autograd. Fix it and verify backward pass correctness.

Root Cause AnalysisTechnical Trade-offs
Author's notes

The .item() one is a classic trap and I said so out loud, which maybe came off as overconfident.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Systematically inspect the code for common autograd pitfalls: detached tensors, premature .item() calls, incorrect reduction dimensions, and in-place operations. Use a minimal reproduction and compare gradients to a reference implementation to isolate the bug. Then fix the issue and verify with torch.autograd.gradcheck or by checking gradient flow.

Pro tip: Always enable anomaly detection (torch.autograd.set_detect_anomaly(True)) to pinpoint where the backward pass fails, and remember that .item() detaches the tensor from the graph—use .detach() only when you intend to stop gradients.

1. Identify suspicious operations

Scan the code for .detach(), .item(), .numpy(), in-place operations (e.g., +=, .add_()), and reductions (sum, mean) that might use the wrong dimension.

2. Isolate the bug with a minimal example

Create a small input and run forward and backward passes, checking if gradients are None or incorrect. Use torch.autograd.gradcheck to verify.

3. Fix the identified issue

Remove unnecessary .detach() or .item(), correct the reduction dimension, or replace in-place operations with out-of-place equivalents.

4. Verify gradient correctness

Re-run the backward pass and compare gradients to a reference implementation or use gradcheck. Ensure all parameters receive gradients.

Key Points to Mention

  • Detached tensors: .detach() or .item() breaks the computation graph, preventing gradient flow.
  • Premature .item() calls: converting to Python scalar detaches the tensor; use .item() only for logging, not in the loss computation.
  • Wrong reduction dimension: summing or averaging over the wrong axis can lead to incorrect gradient shapes or values.
  • In-place operations: operations like += or .add_() can modify tensors needed for gradient computation, causing autograd errors.
  • Gradient checking: use torch.autograd.gradcheck or compare gradients to numerical approximations to verify correctness.
  • Anomaly detection: torch.autograd.set_detect_anomaly(True) helps identify the exact operation where backward fails.

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

Q4

Identify the positional encoding initialization bug. Before diagnosing, clarify with the interviewer which PE variant is used (sinusoidal, RoPE, ALiBi, or learnable). Then pinpoint the specific defect and fix it.

Root Cause AnalysisSystem Design
Author's notes

The clarification step actually saved me here.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify with the interviewer which positional encoding variant is in use, as the bug differs by type. Then systematically inspect the initialization code for that variant, comparing against known correct implementations to identify the defect. Finally, propose a concrete fix and explain how you would verify it.

Pro tip: Demonstrate deep understanding by noting that initialization bugs often manifest as subtle training instabilities or degraded long-range performance, not immediate crashes. Mention that you would add unit tests for PE properties (e.g., relative distances for RoPE, monotonic decay for ALiBi) to catch such issues early.

1. Clarify PE Variant

Ask the interviewer which positional encoding is used (sinusoidal, RoPE, ALiBi, or learnable) to narrow down the potential bug space.

2. Review Initialization Code

Examine the code that initializes the positional encoding, looking for common mistakes such as incorrect frequency computation, wrong scaling, or improper caching.

3. Identify the Defect

Pinpoint the specific bug by comparing against the correct mathematical formulation or reference implementation, and explain why it's wrong.

4. Propose a Fix

Describe the corrected initialization, including any necessary changes to parameters or logic, and ensure it aligns with the intended PE variant.

5. Verification Plan

Outline how to verify the fix, such as unit tests for PE properties, training stability checks, or performance on long sequences.

Key Points to Mention

  • Sinusoidal PE: ensure frequencies follow the geometric progression and are not accidentally swapped or scaled.
  • RoPE: verify that rotation angles are computed correctly and applied to the correct dimensions, and that the base frequency is appropriate.
  • ALiBi: check that slopes are initialized correctly and that the bias is added to attention scores before softmax.
  • Learnable PE: confirm that embeddings are initialized with a sensible distribution (e.g., normal with small std) and not all zeros.
  • Common pitfalls: off-by-one errors in position indices, incorrect handling of padding, or caching issues in autoregressive generation.
  • Impact: initialization bugs can lead to poor extrapolation, training divergence, or degraded performance on long sequences.

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