Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
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.
Implement the function
Rules:
A to an upper-triangular form using only row swaps and “add multiple of row” operations (Gaussian elimination style).np.linalg.det (or any other prebuilt determinant routine).Output:
| Argument | Type |
|---|---|
| A | np.ndarray |
| Return Name | Type |
|---|---|
| value | float |
No np.linalg.det or determinant helpers
Use row swaps + row addition only
Return single float
Start by creating a float copy mat = A.astype(float).copy() and keep a sign variable initialized to +1.
Perform Gaussian elimination to make the matrix upper-triangular using only:
sign),Update rows with vectorization: mat[row, col:] -= factor * mat[col, col:]. Finally, det = sign * np.prod(np.diag(mat)).