← Questions

Derive the least-squares solution for linear regression

0
MetaAI/ML designLast asked Feb 2026

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 n points (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 w and b, set them to zero, and solve: w = sum((x_i - x̄)(y_i - ȳ)) / sum((x_i - x̄)^2) and b = ȳ - 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_i equal in one dimension (zero variance, so the slope is undefined), and a singular or ill-conditioned XᵀX when 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) give w = 2 and b = 0.
  • The matrix form and the cost of solving it: about O(n d^2 + d^3) for n points and d features, and why one solves the linear system (Cholesky, QR) rather than computing an explicit inverse.
  • Ridge regression: w = (XᵀX + λI)⁻¹ Xᵀ y always has a unique solution for λ > 0, and shrinks weights; Lasso has no closed form.
  • Gradient descent: preferred for very large n or d, 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

Your canvas
Draw your answer hereBoxes, arrows and notes, saved for you to come back to.Sign in to start drawing