mlprep
Easy15 min·2 rounds

Implement gradient descent for linear regression from scratch

Written by the mlprep team · Updated

Interviewer Mode
Interviewer

Implement gradient descent for linear regression from scratch in NumPy. No sklearn, no autograd. Compute the gradient manually, implement the update rule, and show it converging. Then extend to mini-batch SGD.

Ready to compare your reasoning?
Interviewer

You used a constant learning rate. Implement learning rate decay and momentum — and explain why momentum helps SGD converge faster.

Ready to compare your reasoning?

The key takeaway unlocks after you reveal an expert answer.

Follow-up Questions to Test Yourself

Interviewers often pivot here once you cover the core concept.

  • Implement Adam optimizer from scratch — what does it do differently from SGD with momentum, and why is it the default for deep learning?
  • Your gradient descent converges to a loss of 0.5 but the closed-form solution achieves 0.3. What went wrong?
  • Explain why feature scaling (normalization) is critical for gradient descent but irrelevant for the closed-form solution.

How confident do you feel explaining this?

Track your understanding or flag it to revisit before your next loop.

Related Questions