Loading

Quipoin Menu

Learn • Practice • Grow

python-for-ai / Linear Algebra with NumPy
interview

Q1. Solve the linear system 3x + 2y = 5, x + y = 2 using numpy.linalg.solve.
A = np.array([[3,2],[1,1]])
b = np.array([5,2])
solution = np.linalg.solve(A, b)   # [1., 1.]
Check: 3*1+2*1=5, 1+1=2. This is used for normal equations in linear regression.

Q2. Compute the inverse of a matrix A = [[4,7],[2,6]] using np.linalg.inv. Then verify that A * inv(A) = identity.
A = np.array([[4,7],[2,6]])
invA = np.linalg.inv(A)           # [[0.6, -0.7], [-0.2, 0.4]]
product = A @ invA                # approximately identity
Inverses used in many algorithms but computationally expensive.

Q3. Perform matrix multiplication between a 3x2 matrix and a 2x4 matrix using np.dot, @ operator, and .matmul method.
A = np.random.rand(3,2)
B = np.random.rand(2,4)
C1 = np.dot(A, B)
C2 = A @ B
C3 = np.matmul(A, B)   # all produce (3,4)
@ is recommended for readability. Matrix multiplication is core to neural network forward pass.

Q4. Compute the eigenvalues and eigenvectors of matrix [[4,1],[2,3]] using np.linalg.eig.
A = np.array([[4,1],[2,3]])
eigvals, eigvecs = np.linalg.eig(A)
# eigvals ≈ [5., 2.], eigvecs ≈ [[0.7071, -0.4472], [0.7071, 0.8944]]
Eigen decomposition is used in PCA and dimensionality reduction.

Q5. Compute the L2 norm (Euclidean norm) of a vector [3,4] using np.linalg.norm. Also compute the Frobenius norm of a 2x2 matrix.
vec = np.array([3,4])
norm_vec = np.linalg.norm(vec)        # 5.0
mat = np.array([[1,2],[3,4]])
norm_mat = np.linalg.norm(mat)        # sqrt(1+4+9+16) ≈ 5.477
Norms are used for regularization (L2 penalty).