Linear Regression from Scratch

Asked at Capital One · Coding & Leetcode-style Questions · Difficulty: Medium · Last asked Jan 19, 2026

Fit a straight line (or hyperplane) to data. Given rows of numeric features and a numeric target, find the coefficients that minimise the mean squared error, then use them to predict. Write it in pure Python (standard library only, no NumPy or scikit-learn), in two ways: with the closed-form normal equation, and with gradient descent.

Loading the problem…