sagelearnai

ML Coding: Implement from Scratch

In ML coding rounds you implement a core algorithm in NumPy (sometimes PyTorch) in 20–40 minutes. Interviewers watch for correct shapes, numerical stability, vectorization, and whether you test your code. Practice writing each of these from memory, then compare with the reference.

Narrate shapes

Say shapes out loud as you code: "X is (n, d), W is (d, k), so logits are (n, k)." It catches most bugs and shows clarity of thought.

Questions covered

  1. Implement a numerically stable softmax and cross-entropy loss.
  2. Implement logistic regression with gradient descent.
  3. Implement k-means clustering.
  4. Implement scaled dot-product multi-head self-attention (with causal mask) in NumPy.
  5. Implement top-k and top-p (nucleus) sampling.
  6. Implement a 2-layer neural network with backprop (no autograd).
  7. Implement one BPE training merge step.
  8. Implement cosine-similarity top-k retrieval and evaluate recall@k.
  9. Implement linear regression with the closed form and with gradient descent.

This lesson is part of AI & ML Interview Prep. Unlock the full course to read it, take the quiz and track your progress.

See plans →

Last reviewed 2026-11-05.