ML Coding Interviews: What to Expect
The ML coding round asks you to write a machine learning algorithm or a model part from scratch, usually in NumPy or PyTorch, in about 45 minutes, while explaining what you are doing. It is not a LeetCode round with ML flavoring: the hard part is rarely the algorithm, which you probably know, but writing it correctly, vectorized and numerically sound under time pressure, and testing it without a library to compare against.
This page covers what the round looks like, what is graded, how to spend the 45 minutes and how to prepare. The rest of this section has one write-up per topic, from NumPy Essentials to a tiny GPT, and ML Coding at Each Company has what candidates reported from specific companies.
What the Round Looks Like
The prompt names an algorithm or a component and gives you an empty file. Classic prompts are k-means, logistic regression, KNN or a decision tree; deep learning prompts are an MLP with a hand-written backward pass, BatchNorm, softmax with its gradient, or attention; LLM prompts go up to a transformer block, a tokenizer or a small model trained end to end. You are expected to run the code, so it must work, not just look right.
Interview reports show four variants around that core, and it pays to ask which one you are in:
- From scratch. Implement the named algorithm, then answer follow-ups on complexity, initialization or stability. Waymo's k-means phone screen is the type (the question).
- From a paper or a spec. Implement an unfamiliar variant from a figure or a description, then test it. An Anthropic phone screen handed over a figure from the Attention Free Transformer paper (the question).
- Debugging. Fix existing code with failing tests or a training run that produces
NaNs; see Debugging ML Code. - Mini-project. Build and train a small model end to end in an hour, sometimes with an AI assistant allowed, as in Mercor's onsite (the question).
Online assessments run the same kinds of tasks with no interviewer, often in parts that build on each other: an Anthropic research scientist assessment built a one-layer transformer in pure NumPy over five questions, starting from softmax (the question). A slow or buggy early part costs you the later ones.
Who Gets It
The round is asked of machine learning engineers, research engineers and research scientists, most of all at AI labs and autonomous-driving companies, where the job is writing model code rather than calling it. It shows up at every stage: as a phone screen instead of a data structures question, as one onsite round among system design and behavioral rounds, and as an online assessment.
Two things in the reports are worth knowing in advance. First, the line between software and ML roles is blurry: Waymo asked k-means in software engineer phone screens, and asked a graph search question in a phone screen for an ML-related role. So prepare both kinds of coding if your role is anywhere near ML. Second, research roles tend to go deeper on the math (backpropagation by hand, why the attention scores are scaled), while engineering roles lean on vectorization, testing and debugging. The level expectations at the end of each write-up reflect that split.
NumPy or PyTorch
Ask which one, and whether built-ins are allowed, before you write anything. The choice changes what is being tested. In NumPy you write the forward and backward passes yourself, so the round tests that you know the gradients; in PyTorch autograd does the backward pass, so the round tests module structure, shapes, masks and the training loop.
As a rule, classic ML and anything that says "by hand" or "backprop" is NumPy, and model parts (attention, a
transformer block, a training loop) are PyTorch. When PyTorch is allowed, built-ins are usually off-limits for
the thing you are asked to build but fine everywhere else, so F.scaled_dot_product_attention can be your test
oracle even if you can't call it in the solution. If you are free to choose, pick the one you write faster, and
say why.
What Is Graded
Interviewers grade five things beyond "does it run", and a solution that is correct but misses the others reads as a junior answer. The short function below shows all five in miniature:
import numpy as np
def cross_entropy(logits: np.ndarray, y: np.ndarray) -> float:
n, C = logits.shape # shapes named and checked
assert y.shape == (n,), y.shape
s = logits - logits.max(axis=1, keepdims=True) # stable: no exp of a large number
log_p = s - np.log(np.exp(s).sum(axis=1, keepdims=True)) # (n, C), vectorized
return float(-log_p[np.arange(n), y].mean()) # one entry per row
logits = np.array([[1000.0, 0.0], [0.0, 0.0]]) # a test you can check by hand
assert np.isclose(cross_entropy(logits, np.array([0, 1])), np.log(2) / 2)
- Correct. The math is right, including edge cases you settled up front: ties, empty clusters, fully masked rows. Prove it with a test, like the last line above.
- Vectorized. No Python loops over examples or features where an array operation will do, and memory you can
state: an
(n, k)distance matrix, not(n, k, d). - Numerically stable. No overflow in
exp, nolog(0), no0 / 0. The max subtraction above is the textbook case; Numerical Stability covers the rest. - Shapes right. Every array's shape is known and written down. Most silent bugs are shape bugs; see Getting Shapes Right.
- Explained. You say why before how: why the scale is
sqrt(d_k), why the mean minimizes squared distance. Several reports list "why" questions after the code, such as why the scores are scaled and why there is a norm.
A 45-Minute Plan
The plan below leaves a third of the time for testing and follow-ups, which is where most candidates run short. Adjust it to the prompt, but keep the order: settle the rules, write the math, build the simplest correct version, test it, then improve it.
| minutes | what you do |
|---|---|
| 0-5 | Clarify inputs, shapes, dtypes, edge cases and the library rules. Write the signatures. |
| 5-10 | Write the math as comments with shapes, e.g. # S = Q K^T / sqrt(d_k) (B, T, T). |
| 10-25 | Write the first correct version. Loops are fine if they get you there faster; say you will vectorize. |
| 25-35 | Test on a tiny input with a known answer, then vectorize or stabilize and test again. |
| 35-45 | Follow-ups: complexity, scale, a variant, the "why" questions. |
Two habits make the plan work. Run each piece as soon as it exists, on a (2, 3) input, instead of writing
everything and running once at minute 35. And narrate the trade-offs as you choose them ("broadcasting is
O(n k d) memory; I'll switch to the expanded square if n is large"), so the interviewer hears the senior
answer even if there isn't time to write it.
How to Prepare
Preparation is mostly repetition: write each core algorithm from a blank file, timed, until the shapes and the stable forms come out without thinking. Work through it in this order:
- The fundamentals first. NumPy and PyTorch essentials, numerical stability, shapes and gradient checking. They are used in every other problem.
- The most asked problems. K-means, softmax and cross-entropy, an MLP with backprop and self-attention come up again and again in reports.
- Your role's specialty. Research and LLM roles: the transformer block, KV cache, tokenizers and decoding. Applied roles: the classic ML group and the evaluation metrics.
- A full training loop. Write one end to end in PyTorch at least once; some rounds build up to it, and several debugging rounds hide bugs in it.
For each problem, write the loop version, the vectorized version and the tests, then answer the follow-ups out loud. If you can't explain a line, you don't know it well enough yet for the "why" questions.
Key Takeaways
- The round is an algorithm or model part from scratch in NumPy or PyTorch, about 45 minutes, run and tested. Variants: from a paper, debugging, a mini-project, an online assessment.
- It is asked of ML engineers, research engineers and scientists, most of all at AI labs and autonomous-driving companies, and sometimes of software engineers.
- Ask NumPy or PyTorch and which built-ins are allowed; NumPy usually means a hand-written backward pass.
- Graded: correct, vectorized, numerically stable, shapes right, explained.
- Spend the last third of the time on tests and follow-ups, and run every piece on a tiny input as you go.
- Prepare by rewriting the core problems from a blank file, timed, with tests.