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

Back to Questions

276. Matrix rank

medium
GeneralGeneral
senior

Compute the rank of a matrix, which is the number of linearly independent rows (or columns) in a matrix. This helps you understand how much “unique information” the matrix contains in a linear algebra sense.

The rank can be defined as the number of non-zero singular values from an SVD:

rank(A)=∣{ i  :  σi>Δ }∣\text{rank}(A) = \left|\{\, i \;:\; \sigma_i > \varepsilon \,\}\right|

where (\sigma_i) are the singular values of (A) and (\varepsilon) is a small tolerance.

Requirements

Implement the function

python

Rules:

  • Treat A as a 2D list (not a NumPy array input), but you may convert it to NumPy internally.
  • Compute rank by counting singular values strictly greater than eps.
  • Do not use np.linalg.matrix_rank (or any other built-in rank helper).
  • Return a single integer.
  • Keep it in one function.

Example

python

Output:

python
Input Signature
ArgumentType
Alist
epsfloat
Output Signature
Return NameType
valueint

Constraints

  • Input A is 2D Python list

  • Use SVD; count singular values > eps

  • Do not call np.linalg.matrix_rank

Hint 1

Convert the input 2D list A into a numeric matrix representation, then compute its singular values; the rank is how many are non-zero (above a tolerance).

Hint 2

Use np.linalg.svd(A_np, compute_uv=False) to get the 1D array of singular values s; then count sum(s > eps) (strictly greater).

Hint 3

Be careful about shapes and edge cases: empty input, non-rectangular lists, and ensuring you don’t call np.linalg.matrix_rank; return an int from int(np.sum(s > eps)).

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
svd
numerical-linear-algebra
matrix-rank
numpy
48 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