Math & Statistics FoundationsFoundational18 min read5 questions

Linear Algebra for Machine Learning Interviews

Every embedding, attention head and gradient update is linear algebra. Master the five questions interviewers actually ask about vectors, matrix decompositions and derivatives.

Covers: Vectors & matrices, matrix multiplication cost, eigendecomposition, SVD, norms, matrix calculus

Machine learning interviews rarely ask you to invert a matrix by hand. They ask whether you understand what the operations mean, because that understanding is what lets you debug an exploding loss, explain why an embedding table is 4 GB, or reason about why attention is .

This lesson covers the five linear-algebra questions that show up most often in ML engineer screens at product companies and AI labs. For each one you get the 30-second spoken answer, the deeper explanation, runnable code, and the follow-up the interviewer will hit you with next.

Filter
0/5 mastered
FoundationalvectorsembeddingssimilarityAsked at Meta, Pinterest

30-second answer

The dot product is the length of one vector times the projection of the other onto it: it grows with both magnitude and alignment. Cosine similarity divides that by both norms, isolating the angle. Embeddings encode meaning in direction, not length, so cosine removes the nuisance magnitude that comes from token frequency or document length.

IntermediatematmulcomplexityinferenceAsked at OpenAI, Anthropic

30-second answer

X @ W costs roughly 2·B·d·h FLOPs (one multiply and one add per term) and produces a B×h output. In transformers this is why the feed-forward block dominates FLOPs, why attention is O(n²·d) in sequence length, and why decoding is memory-bandwidth-bound rather than compute-bound.

Intermediatesvdpcadimensionality-reductionAsked at Amazon, Microsoft

30-second answer

Eigendecomposition factors a square matrix into directions that only get scaled, A = QΛQ⁻¹. SVD generalises this to any m×n matrix, A = UΣVᵀ, always exists, and is numerically stable. PCA is the SVD of the mean-centred data matrix: right singular vectors are principal directions, and squared singular values are proportional to explained variance.

Intermediatenormsregularizationnumerical-stabilityAsked at Google, Stripe

30-second answer

Norms measure size. L2 penalises large weights smoothly and shrinks them proportionally; L1 has a constant-magnitude gradient that drives weights exactly to zero, giving sparsity. Frobenius is the L2 norm of a flattened matrix. A matrix is ill-conditioned when its largest and smallest singular values differ by orders of magnitude — the loss surface becomes a narrow ravine and plain gradient descent zig-zags.

Advancedmatrix-calculusbackpropgradientsAsked at Anthropic, DeepMind

30-second answer

With upstream gradient g = ∂L/∂y of shape (B×h): ∂L/∂W = xᵀg (d×h), ∂L/∂x = gWᵀ (B×d), and ∂L/∂b = g summed over the batch dimension (h,). The reliable trick is that gradients must match the shape of the thing they differentiate — that determines the transposes uniquely.

Showing 5 of 5 questions for linear-algebra-for-machine-learning.

Check your understanding

5 questions · no sign-up, nothing stored

0/5 answered
Question 1

1.You L2-normalise every vector before inserting them into your vector index. Which statement is now true?

Question 2

2.Approximately how many FLOPs does multiplying a 32×1024 activation matrix by a 1024×4096 weight matrix take?

Question 3

3.Why does Lasso drive coefficients to exactly zero while Ridge does not?

Question 4

4.For y = xW + b with x of shape (B, d) and W of shape (d, h), what is the shape of ∂L/∂W?

Question 5

5.Why is single-token LLM decoding memory-bandwidth-bound?

Hands-on challenge

Build it — this is what you talk about in a deep-dive round.

Implement PCA from scratch and profile a matmul

Write a small NumPy module that reproduces scikit-learn's PCA and empirically confirms the FLOP model from question 2.

Requirements

  • `fit(X, k)` that centres the data, runs `np.linalg.svd`, and stores components plus explained-variance ratio.
  • `transform(X)` and `inverse_transform(Z)`; verify reconstruction error decreases monotonically with k.
  • Assert your explained-variance ratios match `sklearn.decomposition.PCA` to 1e-8.
  • Time `A @ B` for square sizes 256…4096 and plot achieved GFLOP/s using the 2·n³ model.

Stretch goals

  • Add randomised SVD (Halko et al.) and compare accuracy vs. wall-clock on a 20000×500 matrix.
  • Show empirically that centring before PCA changes the first component when the mean is far from the origin.