What is the computational complexity of solving the Normal Equation?

Quick Overview

Asks for the computational cost of fitting a linear regression with the Normal Equation, in terms of the number of rows and features. Tests the cost of each step, which term dominates, and when gradient descent or an iterative solver is the better choice.

What is the computational complexity of solving the Normal Equation?

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

What is the computational complexity of solving the Normal Equation? Assume a linear regression with $n$ observations and $p$ features, fit in closed form as $\hat\beta = (X^\top X)^{-1} X^\top y$. ```hint Count each step Split the computation into its matrix products and the final solve, and count the cost of each step in terms of $n$ and $p$. ``` ### What a Strong Answer Covers - The cost of each step: forming $X^\top X$, forming $X^\top y$, and solving the $p \times p$ system - The total cost in terms of $n$ and $p$, and which term dominates in which regime - How the cost grows with $n$ and with $p$, including the memory needed for $X^\top X$ - When to switch to gradient descent or other iterative solvers ### Follow-up Questions - Why do libraries solve the system with a Cholesky or QR factorization instead of computing the inverse? - What happens to the Normal Equation when $p > n$, and how does ridge regression change that? - How does the per-iteration cost of gradient descent compare, and what controls the number of iterations it needs? - How would you fit the regression when $X$ does not fit in memory?

Overview: Asks for the computational cost of fitting a linear regression with the Normal Equation, in terms of the number of rows and features. Tests the cost of each step, which term dominates, and when gradient descent or an iterative solver is the better choice.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0

What is the computational complexity of solving the Normal Equation?

Assume a linear regression with nn observations and pp features, fit in closed form as β^=(X⊤X)−1X⊤y\hat\beta = (X^\top X)^{-1} X^\top y.

What a Strong Answer Covers Guidance

  • The cost of each step: forming X⊤XX^\top X , forming X⊤yX^\top y , and solving the p×pp \times p system
  • The total cost in terms of nn and pp , and which term dominates in which regime
  • How the cost grows with nn and with pp , including the memory needed for X⊤XX^\top X
  • When to switch to gradient descent or other iterative solvers

Follow-up Questions Guidance

  • Why do libraries solve the system with a Cholesky or QR factorization instead of computing the inverse?
  • What happens to the Normal Equation when p>np > n , and how does ridge regression change that?
  • How does the per-iteration cost of gradient descent compare, and what controls the number of iterations it needs?
  • How would you fit the regression when XX does not fit in memory?