Derive the least-squares solution for linear regression
I was asked this
No reports yet. Sign in to say where you were asked it.
An AI/ML design interview question asked in Meta's technical phone screen, last asked Feb 2026. It tests linear regression, optimization and ml fundamentals.
Derive the closed-form solution of ordinary least-squares linear regression, starting from the one-dimensional case y ≈ w x + b with the mean squared error loss, and explain each step. Then say when the data makes the solution undefined, what regularization changes, and how the closed form compares with gradient descent.
Requirements
- Write the mean squared error over
npoints(x_i, y_i)and explain why it is the natural loss (it matches maximum likelihood under Gaussian noise). - Take the partial derivatives with respect to
wandb, set them to zero, and solve:w = sum((x_i - x̄)(y_i - ȳ)) / sum((x_i - x̄)^2)andb = ȳ - w x̄. - Show that this is a minimum (the loss is convex).
- Generalize to many features:
w = (XᵀX)⁻¹ Xᵀ y, with a column of ones for the intercept. - Name the degenerate cases: all
x_iequal in one dimension (zero variance, so the slope is undefined), and a singular or ill-conditionedXᵀXwhen features are collinear or there are more features than points. - Compare the closed form with gradient descent.
Clarifying questions
- Should I include an intercept, or assume centered data?
- Is the multivariate form expected, or only the one-dimensional case?
- Should I discuss probabilistic assumptions, or only the optimization?
What a strong answer covers
- A clean derivation with the two normal equations and the final formulas, written in terms of means, covariance and variance.
- A check on a tiny example: points
(1, 2), (2, 4), (3, 6)givew = 2andb = 0. - The matrix form and the cost of solving it: about
O(n d^2 + d^3)fornpoints anddfeatures, and why one solves the linear system (Cholesky, QR) rather than computing an explicit inverse. - Ridge regression:
w = (XᵀX + λI)⁻¹ Xᵀ yalways has a unique solution forλ > 0, and shrinks weights; Lasso has no closed form. - Gradient descent: preferred for very large
nord, streaming data, or when the closed form is ill-conditioned; it needs a learning rate and feature scaling and converges iteratively. - The assumptions behind interpreting the result: linearity, independent errors, constant variance, and the effect of outliers.
Follow-ups
- What happens to the solution if you duplicate one feature column?
- How would you fit the model when the data does not fit in memory?
- Derive the gradient descent update for the same loss.
- The residuals grow with
x. What does that tell you, and what do you do?
Where it's asked
- Meta · Technical phone screen · Last asked Feb 202645 min · Two coding problems in CoderPad, about 20 minutes each · Usually no AI; some newer screens are AI-assisted
What it tests
linear regression · optimization · ml fundamentals
No official answer yet. Draw yours on the right; publish it and it shows under Community.
Nothing published yet. Publish your canvas and it shows here for others to open and remix.
