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

Back to Questions

38. Determinant properties

medium
GeneralGeneral
staff

You’ll practice core determinant properties that show up a lot in linear algebra interviews, like how row operations affect the determinant. Given a square matrix as a NumPy array, compute its determinant using only simple row operations and a recursive base case.

det(A)=(1)ki=1nUii\det(A) = (-1)^k \prod_{i=1}^n U_{ii}

Requirements

Implement the function

python

Rules:

  • Use the determinant identity det(A)=i=1nuiis\det(A)=\prod_{i=1}^{n} u_{ii} \cdot s where (u_{ii}) are diagonal entries after converting (A) to an upper-triangular matrix via row operations, and (s\in{+1,-1}) tracks row swaps.
  • Convert A to an upper-triangular form using only row swaps and “add multiple of row” operations (Gaussian elimination style).
  • Track row swaps to adjust the sign of the final determinant.
  • Do not call np.linalg.det (or any other prebuilt determinant routine).
  • Return a single float.

Example

python

Output:

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

Constraints

  • No np.linalg.det or determinant helpers

  • Use row swaps + row addition only

  • Return single float

Hint 1

Start by creating a float copy mat = A.astype(float).copy() and keep a sign variable initialized to +1.

Hint 2

Perform Gaussian elimination to make the matrix upper-triangular using only:

  • swap rows when the pivot is zero (flip sign),
  • add a multiple of the pivot row to rows below (does not change determinant).
Hint 3

Update rows with vectorization: mat[row, col:] -= factor * mat[col, col:]. Finally, det = sign * np.prod(np.diag(mat)).

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
staff
senior
Tags
determinant
gaussian-elimination
row-operations
numerical-linear-algebra
36 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