ML Coding RoundsIntermediate20 min read5 questions

NumPy, pandas & ML Algorithms From Scratch

The ML coding round is not LeetCode. It is 'implement k-means in 20 minutes' and 'vectorise this loop' — with an interviewer watching how you debug.

Covers: Vectorisation, broadcasting, pandas performance, implementing k-means/kNN/linear regression, gradient descent by hand

ML coding rounds test three things: can you write clean vectorised NumPy, can you implement a standard algorithm from its definition, and can you debug your own code without panicking. They are far more predictable than general algorithm interviews — the set of things they ask is small and learnable.

Filter
0/5 mastered
IntermediatenumpyvectorizationbroadcastingAsked at Meta, Amazon

30-second answer

Use the expansion ‖a−b‖² = ‖a‖² + ‖b‖² − 2a·b so the whole distance matrix is one matmul plus two broadcasts. Broadcasting aligns shapes from the right, and dimensions are compatible if they are equal or one of them is 1; size-1 dimensions are stretched without copying. This turns an O(n·m) Python loop into a single BLAS call — typically 100–1000× faster.

IntermediatekmeansclusteringimplementationAsked at Amazon, Apple

30-second answer

Initialise centroids with k-means++ (pick the first uniformly, then each subsequent one with probability proportional to squared distance from the nearest existing centroid), then alternate assignment (each point to its nearest centroid) and update (each centroid to the mean of its points) until the assignments stop changing or the inertia change falls below a tolerance.

Intermediatelogistic-regressiongradient-descentimplementationAsked at Google, Two Sigma

30-second answer

Use a numerically stable sigmoid and a log-loss computed via logaddexp so you never take log(0). The gradient is Xᵀ(p − y)/n plus the regularisation term, with the intercept excluded from the penalty. Add a convergence check on the gradient norm and support mini-batching.

Advancedpandasperformancedata-engineeringAsked at Instacart, Airbnb

30-second answer

Profile first, then apply in order: eliminate `.apply` and `iterrows` in favour of vectorised operations or `groupby.transform`, downcast dtypes and use categoricals (often 5–10× memory reduction), avoid repeated concatenation in loops, use merge on indexed and sorted keys, chunk or move to a columnar engine (Polars, DuckDB) when the data no longer fits comfortably in memory.

AdvancedpytorchattentionimplementationAsked at OpenAI, Anthropic

30-second answer

Project to Q, K, V, reshape to (batch, heads, seq, head_dim), compute scaled dot-product scores with a causal mask, softmax, multiply by V, then merge heads and apply the output projection. For generation, cache K and V across steps and only compute Q, K, V for the new token — turning each step from O(n²) to O(n).

Showing 5 of 5 questions for numpy-pandas-ml-coding-interview.

Check your understanding

5 questions · no sign-up, nothing stored

0/5 answered
Question 1

1.Why compute pairwise distances with ‖a‖² + ‖b‖² − 2a·b rather than broadcasting the difference?

Question 2

2.The most common bug in a from-scratch k-means implementation is:

Question 3

3.Computing log-loss as `-y*log(p) - (1-y)*log(1-p)` is risky because:

Question 4

4.Replacing `df.apply(fn, axis=1)` with a vectorised `np.where` typically gives:

Question 5

5.With a KV cache during generation, the causal mask needs care because:

Hands-on challenge

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

Build a mini ML library and benchmark it

Implement the core algorithms from scratch, validated against reference implementations.

Requirements

  • k-means with k-means++ and empty-cluster handling; match sklearn's inertia exactly on the same seed.
  • Logistic regression with stable sigmoid and logaddexp loss, L1/L2, and a passing gradient check.
  • kNN with the matmul distance identity, supporting 100k×100k without exceeding memory.
  • Multi-head attention with a KV cache, verified against `F.scaled_dot_product_attention` to 1e-5.

Stretch goals

  • Add mini-batch k-means and compare convergence and wall-clock against the full version at n = 5M.
  • Profile a real pandas pipeline, then rewrite the hot path in Polars or DuckDB and report the speedup.