Solve a linear system Ax = b in NumPy without inverting the matrix

Quick Overview

Implement a NumPy function that solves a square linear system Ax = b and compare it with multiplying by the inverse. It tests LU factorization, the cost and accuracy of each approach, conditioning, and how to handle singular matrices or many right-hand sides.

Solve a linear system Ax = b in NumPy without inverting the matrix

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `solve_system(A, b)` that returns the solution vector `x` (a NumPy array with `A @ x ≈ b`) of the system `A x = b`. Requirements: - Use `np.linalg.solve(A, b)`. - Do not use `np.linalg.inv(A) @ b`. Example: ```python A = np.array([[3.0, 2.0], [1.0, 2.0]]) b = np.array([12.0, 8.0]) solve_system(A, b) # array([2., 3.]) ``` ```hint What each route computes Compare what the two approaches build on the way to `x`: a factorization of `A`, or every entry of the inverse followed by a matrix-vector product. Think about both the work and the number of rounding steps. ``` ### Constraints & Assumptions - `A` is square (`n x n`) and full-rank. - `b` has shape `(n,)`. ### What a Strong Answer Covers - Calls `np.linalg.solve` and returns a float vector of shape `(n,)` - Explains how `solve` finds `x` and why that beats forming the inverse in both cost and accuracy - States the time and memory cost in terms of `n` - Describes what happens when `A` is singular or ill-conditioned, and how to detect the second case - Checks the result with the residual `A @ x - b` instead of exact equality ### Follow-up Questions - Why is `solve` preferred over inverting `A`? - How would you solve for many right-hand sides at once? - What if `A` is singular or ill-conditioned? - If new right-hand sides arrive one at a time for the same `A`, or `A` is symmetric positive definite, what would you change?

Overview: Implement a NumPy function that solves a square linear system Ax = b and compare it with multiplying by the inverse. It tests LU factorization, the cost and accuracy of each approach, conditioning, and how to handle singular matrices or many right-hand sides.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement solve_system(A, b) that returns the solution vector x (a NumPy array with A @ x ≈ b) of the system A x = b.

Requirements:

  • Use np.linalg.solve(A, b) .
  • Do not use np.linalg.inv(A) @ b .

Example:

A = np.array([[3.0, 2.0], [1.0, 2.0]])
b = np.array([12.0, 8.0])
solve_system(A, b)  # array([2., 3.])

Constraints & Assumptions

  • A is square ( n x n ) and full-rank.
  • b has shape (n,) .

What a Strong Answer Covers Guidance

  • Calls np.linalg.solve and returns a float vector of shape (n,)
  • Explains how solve finds x and why that beats forming the inverse in both cost and accuracy
  • States the time and memory cost in terms of n
  • Describes what happens when A is singular or ill-conditioned, and how to detect the second case
  • Checks the result with the residual A @ x - b instead of exact equality

Follow-up Questions Guidance

  • Why is solve preferred over inverting A ?
  • How would you solve for many right-hand sides at once?
  • What if A is singular or ill-conditioned?
  • If new right-hand sides arrive one at a time for the same A , or A is symmetric positive definite, what would you change?