Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

170. Matrix inverse

easy
GeneralGeneral
senior

Compute the inverse of a square matrix (when it exists) using basic linear algebra operations, which is a common building block in solving linear systems. The inverse (A^{-1}) is defined such that:

AA−1=IAA^{-1} = I

Requirements

Implement the function

python

Rules:

  • Compute and return the inverse of A as a NumPy array.
  • Do not implement Gaussian elimination manually; use NumPy linear algebra operations.
  • Keep the output values as floats (even if the inverse has integer-looking entries).
  • No printing; just return the result.

Example

python

Output:

python
Input Signature
ArgumentType
Anp.ndarray
Output Signature
Return NameType
valuenp.ndarray

Constraints

  • Input is np.ndarray

  • Return inverse as np.ndarray

  • Use np.linalg.inv; no manual elimination

Hint 1

Use NumPy’s built-in linear algebra inversion (np.linalg.inv) to compute the inverse.

Hint 2

The function should accept and return a np.ndarray directly.

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
numpy
matrix-inverse
linear-algebra
type-conversion
37 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit