← Oracle Interview Insights

Oracle·Machine Learning Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
Apr 2026

Summary

Oracle ML Engineer interview, technical phone screen focused on implementing multi-head self-attention from scratch. Pretty deep dive for a single question, they clearly wanted to see if you actually understood the internals rather than just calling PyTorch APIs.

Questions Asked (1)

Q1

Implement multi-head self-attention in PyTorch or NumPy from scratch (no built-in attention modules). Walk through projecting inputs into Q, K, V, reshaping for multiple heads, computing scaled dot-product attention, and merging heads back with a final projection. Also discuss causal and padding masking, and the computational complexity.

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

This wrecked me a little.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the input shapes and the overall architecture, then implement the linear projections and reshaping for multi-head attention. Walk through the scaled dot-product attention with masking, and finally merge the heads and apply the output projection. Emphasize the computational complexity and practical considerations.

Pro tip: Mention that you would use `torch.einsum` or `torch.matmul` with proper broadcasting for efficiency, and that you would verify the implementation against a reference like `nn.MultiheadAttention` in a unit test.

1. Clarify Inputs and Architecture

Define input tensor shapes (batch_size, seq_len, d_model) and the number of heads. Explain that d_model must be divisible by num_heads, and each head has dimension d_k = d_model // num_heads.

2. Linear Projections and Reshaping

Create learnable weight matrices W_q, W_k, W_v of shape (d_model, d_model) and biases. Project inputs to Q, K, V, then reshape to (batch_size, num_heads, seq_len, d_k) by splitting the last dimension.

3. Scaled Dot-Product Attention with Masking

Compute attention scores as Q @ K^T / sqrt(d_k). Apply causal mask (if needed) by setting future positions to -inf before softmax, and padding mask by setting padded positions to -inf. Then compute softmax and weighted sum with V.

4. Merge Heads and Output Projection

Transpose and reshape the attention output back to (batch_size, seq_len, d_model), then apply a final linear projection W_o to combine information from all heads.

5. Discuss Complexity and Trade-offs

Analyze time and space complexity: O(n^2 * d) for attention, and O(n^2) memory for the attention matrix. Mention that multi-head attention increases representational power at the cost of more parameters and computation.

Key Points to Mention

  • Scaling factor 1/sqrt(d_k) to prevent softmax saturation
  • Causal masking for autoregressive decoding (e.g., in GPT) and padding masking for variable-length sequences
  • Efficient implementation using batched matrix multiplication (e.g., torch.bmm or einsum)
  • The need for a final linear projection to combine heads
  • Computational complexity: O(n^2 * d) time and O(n^2) memory, which is quadratic in sequence length
  • Comparison with built-in modules and potential optimizations like FlashAttention

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