MLStackMLSCCafé
 
 
Sign in with GoogleSign in with Google. Opens in new tab
Master Your ML & AIAI Interview
2103 Curated Machine Learning, Data Science, AI & LLMs Interview Questions
Answered To Get Your Next Six-Figure Job Offer

17 Linear Algebra Interview Questions To Brush Before Data Science Interview

Linear Algebra is a key foundation to the field of machine learning and data science, from notations used to describe the operation of algorithms to the implementation of algorithms in code. Follow along and check the 14 most common Linear Algebra Interview Questions every ML engineer and data scientist shall brush before next daa science interview.

Q1: 
At what conditions does the inverse of a diagonal matrix exist?

Answer
Source: www.quora.com

The invert of a square diagonal matrix exists if all entries of the diagonal are non-zero. If it is the case, the invert is obtained by replacing each element in the diagonal with its reciprocal.

D=(d1000d2000di)D1=(1/d10001/d20001/di)D = \begin{pmatrix} \begin{array}{cccc}d_{1} & 0 & \cdots & 0\\0 & d_{2} & \cdots & 0\\ \vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & d_{i}\end{array} \end{pmatrix} \qquad \Rightarrow D^{-1} = \begin{pmatrix} \begin{array}{cccc} 1/d_{1} & 0 & \cdots & 0\\0 & 1/d_{2} & \cdots & 0\\ \vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & 1/d_{i}\end{array} \end{pmatrix}

Having Machine Learning, Data Science or Python Interview? Check 👉 42 Linear Algebra Interview Questions

Q2: 
How do you find eigenvalues of a matrix? Could you provide an example?

Answer

Given a matrix A, we find its eigenvalues λ by solving the equation:

det(λIA)=0\det \left( \lambda I -A \right) = 0

For example, given the following matrix,

A=(5274)A = \left ( \begin{array}{rr} -5 & 2 \\ -7 & 4 \end{array} \right )

we determine its eigenvalues in the following way:

det(λIA)=λ(1001)(5274)=(λ+527λ4)\begin{aligned} \det ( \lambda I - A ) &= \left| \lambda \left ( \begin{array}{rr} 1 & 0 \\ 0 & 1 \end{array} \right ) - \left ( \begin{array}{rr} -5 & 2 \\ -7 & 4 \end{array} \right ) \right| \\ \\ &= \left| \left ( \begin{array}{cc} \lambda +5 & -2 \\ 7 & \lambda -4 \end{array} \right ) \right| \end{aligned}

Now the characteristic polynomial is:

λ2+λ6=0\lambda ^2 + \lambda - 6 = 0

The solutions of this equation and therefore the eigenvalues are then,

λ1=2,λ2=3\lambda_1 = 2, \lambda_2 = -3


Having Machine Learning, Data Science or Python Interview? Check 👉 42 Linear Algebra Interview Questions

Q3: 
What is Ax=bAx = b? When does Ax=bAx = b has a unique solution?

Answer

Ax = b is a system of linear equations expressed in matrix notation, in which:

  • A is the coefficient matrix of order m x n.
  • x is the incognite variables vector of order n x 1.
  • b is the vector formed by the constants and its order is m x 1.

The system Ax = b has a unique solution if and only if

rank[A]=rank[Ab]=nrank [A] = rank[A|b] = n

where the matrix A|b is matrix A with b appended as an extra column.


Having Machine Learning, Data Science or Python Interview? Check 👉 42 Linear Algebra Interview Questions

Q4: 
What's the difference between Cross Product and Dot Product?

Answer
  • A dot product is a fundamental way we can combine two vectors. Intuitively, it tells us something about how much two vectors point in the same direction or what is the projection of a vector on another vector and it returns a single number. It's defined as:
    uv=uvcos(α)\vec{u} \cdot \vec{v} = \| \vec{u} \|\| \vec{v} \| \cos (\alpha)
    where α is the angle between the vectors.
    In the picture, OA' is the projection of the vector of u on v.
  • A cross product is another way to combine two vectors. The difference here is that the result is another vector that is perpendicular to the initial ones. The cross prodct it's defined as:
    a×b=absin(θ)\vec{a} \times \vec{b} = \| \vec{a} \|\| \vec{b} \| \sin (\theta)
    where θ is the angle between the vectors.

Having Machine Learning, Data Science or Python Interview? Check 👉 42 Linear Algebra Interview Questions

Q5: 
When are two vectors x and y orthogonal?

Two vectors are said to be orthogonal if the dot product of them is equal to zero,

xy=0\vec{x} \cdot \vec{y} = 0

This is because the definition of the dot product:

xy=xycos(θ)\vec{x} \cdot \vec{y} = |x||y| \cos(\theta)

where θ is the angle between the two vectors, therefore if x and y are orthogonal, the angle between them is 90 and cos(90) = 0.


Having Machine Learning, Data Science or Python Interview? Check 👉 42 Linear Algebra Interview Questions

Q6: 
Determine which of the following matrices is normal

Problem
A=(1i01),A = \begin{pmatrix} 1 & i \\ 0 & 1 \end{pmatrix},
B=(1i12+i)B = \begin{pmatrix} 1 & i \\ 1 & 2 + i \end{pmatrix}
Answer

We say that a matrix A is normal if AA=AA,AA^* = A^* A, where A* is the conjugate transpose of matrix A.

  1. Prove equality for A:

    AA=(1i01)(10i1)=(2ii1)AA^* = \begin{pmatrix} 1 & i \\ 0 & 1 \end{pmatrix} \begin{pmatrix} 1 & 0 \\ -i & 1 \end{pmatrix} = \begin{pmatrix} 2 & i \\ -i & 1 \end{pmatrix}
    AA=(10i1)(1i01)=(1ii2)A^*A = \begin{pmatrix} 1 & 0 \\ -i & 1 \end{pmatrix} \begin{pmatrix} 1 & i \\ 0 & 1 \end{pmatrix} = \begin{pmatrix} 1 & i \\ -i & 2 \end{pmatrix}

    AA̸=AAA is not normal.\therefore AA^* \not = A^* A \Rightarrow A \text{ is not normal.}

  1. Prove equality for B:

    BB=(1i12+1)(11i2i)=(22+2i22i6)BB^* = \begin{pmatrix} 1 & i \\ 1 & 2+1 \end{pmatrix} \begin{pmatrix} 1 & 1 \\ -i & 2-i \end{pmatrix} = \begin{pmatrix} 2 & 2+2i \\ 2-2i & 6 \end{pmatrix}
    BB=(11i2i)(1i12+i)=(22+2i22i6)B^*B = \begin{pmatrix} 1 & 1 \\ -i & 2-i \end{pmatrix} \begin{pmatrix} 1 & i \\ 1 & 2+i \end{pmatrix} = \begin{pmatrix} 2 & 2+2i \\ 2-2i & 6 \end{pmatrix}

    BB=BBB is normal.\therefore BB^* = B^* B \Rightarrow B \text{ is normal.}


Having Machine Learning, Data Science or Python Interview? Check 👉 42 Linear Algebra Interview Questions

Q7: 
Find the inverse of the following matrix

Problem

Consider the matrix:

A=[421503126]A = \left[\begin{array}{ccc}4 & -2 & 1\\5 & 0 & 3\\-1 & 2 & 6\end{array}\right]
Answer

We will compute inverse using the following equation: A1=1det(A)adj(A)A^{-1} = \frac{1}{det(A)} adj(A) where adj(A) is the adjugate of matrix A. Now we follow the next steps:

  1. Calculate the determinant of A:

    det(A)=40326(2)5316+15012 =4(06)+2(30+3)+1(100) =52̸=0the matrix is invertible\begin{aligned} det(A) =& 4\left|\begin{matrix}0&3\\2 & 6\end{matrix}\right| -(-2)\left|\begin{matrix}5&3\\-1 & 6\end{matrix}\right|+1\left|\begin{matrix}5&0\\-1& 2\end{matrix}\right| \\ \ \\ = \, & 4(0 - 6) + 2(30 + 3) + 1(10 - 0) \\ \ \\ =& \, 52 \not = 0 \therefore \text{the matrix is invertible} \end{aligned}
  2. Calculate the cofactor of each element:

    C11=(1)1+10326=6,C12=(1)1+25316=33, C13=(1)1+35012=10,C21=(1)2+12126=14, C22=(1)2+24116=25,C23=(1)2+34212=6, C31=(1)3+12103=6,C32=(1)3+24153=7, C33=(1)3+34250=10.\begin{aligned} C_{11} &= \left(-1\right)^{1 + 1} \left|\begin{array}{cc}0 & 3\\2 & 6\end{array}\right| = -6, \qquad C_{12} = \left(-1\right)^{1 + 2} \left|\begin{array}{cc}5 & 3\\-1 & 6\end{array}\right| = -33 , \\ \ \\ C_{13} &= \left(-1\right)^{1 + 3} \left|\begin{array}{cc}5 & 0\\-1 & 2\end{array}\right| = 10, \qquad C_{21} = \left(-1\right)^{2 + 1} \left|\begin{array}{cc}-2 & 1\\2 & 6\end{array}\right| = 14, \\ \ \\ C_{22} &= \left(-1\right)^{2 + 2} \left|\begin{array}{cc}4 & 1\\-1 & 6\end{array}\right| = 25, \qquad C_{23} = \left(-1\right)^{2 + 3} \left|\begin{array}{cc}4 & -2\\-1 & 2\end{array}\right| = -6, \\ \ \\ C_{31} &= \left(-1\right)^{3 + 1} \left|\begin{array}{cc}-2 & 1\\0 & 3\end{array}\right| = -6, \qquad C_{32} = \left(-1\right)^{3 + 2} \left|\begin{array}{cc}4 & 1\\5 & 3\end{array}\right| = -7, \\ \ \\ C_{33} &= \left(-1\right)^{3 + 3} \left|\begin{array}{cc}4 & -2\\5 & 0\end{array}\right| = 10. \end{aligned}

    Thus, the cofactor matrix is: C=[63310142566710]C = \left[\begin{array}{ccc}-6 & -33 & 10\\14 & 25 & -6\\-6 & -7 & 10\end{array}\right]

  3. Obtain the adjugate matrix by transposing cofactor matrix
    adj(A)=[61463325710610]adj(A) = \left[\begin{array}{ccc}-6 & 14 & -6\\-33 & 25 & -7\\10 & -6 & 10\end{array}\right]
  4. Finally, the inverse matrix is the adjugate matrix divided by the determinant:
    A1=152[61463325710610] A1=[3/267/263/2633/5225/527/525/263/265/26]\begin{aligned} A^{-1} &= \dfrac{1}{52} \cdot \left[\begin{matrix}-6 & 14 & -6\\-33&25&-7\\10&-6 & 10\end{matrix}\right] \\ \ \\ A^{-1} &= \left[\begin{matrix}-3/26 & 7/26 & -3/26\\-33/52&25/52&-7/52\\5/26&-3/26 & 5/26\end{matrix}\right] \end{aligned}

Having Machine Learning, Data Science or Python Interview? Check 👉 42 Linear Algebra Interview Questions

Q8: 
How do you diagonalize a matrix?

Answer

Given an nxn matrix A, to find its diagonal matrix D we must follow the next steps:

  1. Find the characteristic polynomial of A.
  2. Find the roots of the characteristic polynomial to obtain the eigenvalues λ of A.
  3. For each eigenvalue λ of A, find their correspondent eigenvectors.
  4. If the total number of eigenvectors m found in step 3 is not equal to n (the numbers of rows and columns of A), then the matrix is not diagonalizable, but if m = n then the diagonal matrix D is given by:
    D=P1AP,\bf D = P^{-1} A P,
    where P is a matrix which columns are the eigenvectors of the matrix A.

Having Machine Learning, Data Science or Python Interview? Check 👉 42 Linear Algebra Interview Questions

Q9: 
How do you find the inverse of a 2x2 matrix?

For an arbitrary A matrix, we can derive it's inverse by following the next steps:

A=(abcd)A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}
  1. Check if the matrix if invertible by finding its determinant :
    A=adbc.|A| = ad -bc.
    If |A| ≠ 0 then the matrix is invertible.
  2. Interchange the two elements on the diagonal.
  3. Take the negatives of the other two elements out of the diagonal.
  4. Divide each element of the matrix by |A|. The result of the inverse of the matrix A is then:
A1=1A(dbca)A^{-1} = \frac{1}{|A|} \begin{pmatrix} d & -b \\ c & a \end{pmatrix}

Having Machine Learning, Data Science or Python Interview? Check 👉 42 Linear Algebra Interview Questions

Q10: 
How many ways of measure a vector do you know?

Answer

There are different ways to measure the magnitude of vectors, the most common are:

  • L0 norm: Although it's not formally speaking a norm, it's often used as if was one. It corresponds to the total number of nonzero elements in a vector. For example, the L0 norm of the vector [0,2] is 1 because there is only one nonzero element.

  • L1 norm: Also known as Manhattan Distance, is the sum of absolute values of the components of the vector. For example, for some vector:

    X=[x1,x2]X = [x_1, x_2]

    The L1 norm is calculated by:

x1=x1+x2\| x\|_1 = |x_1| + |x_2|
  • L2 norm: Also known as the Euclidean norm, it is the shortest distance to go from one point to another. Using the same example as before, the L2 norm for the vector defined above is:

    x2=x12+x22\| x\|_2 = \sqrt{ x_1^2 + x_2^2 }
  • L-infinity norm: Gives the largest magnitude among each element of a vector. For example, having the vector X= [-6, 4, 2], the L-infinity norm is 6.


Having Machine Learning, Data Science or Python Interview? Check 👉 42 Linear Algebra Interview Questions

Q11: 
What are positive definite, negative definite, positive semi definite and negative semi definite matrices?

Answer
  • A Positive definite matrix is a symmetric matrix M such that the number zᵗMz is positive for every nonzero column vector z.
  • A Positive semi-definite matrix is a symmetric matrix M such that the number zᵗMz is positive or zero for every nonzero column vector z.
  • Negative-definite and negative semi-definite matrices are defined analogously.

Given that we can associate each matrix with the quadratic equation zᵗMz, these kinds of matrix helps us to solve optimization problems. For example, a positive definite matrix M will imply a convex function, which guarantees the existence of the global minimum. This allows us to use the Hessian matrix to solve the optimization problem. Similar arguments also hold for negative definite matrices.


Having Machine Learning, Data Science or Python Interview? Check 👉 42 Linear Algebra Interview Questions

Q12: 
What is broadcasting in connection to Linear Algebra?

Broadcasting is a mechanism for relaxing elementwise operations according to the dimension requirements. We say that two matrices are compatible for broadcasting if the corresponding dimensions in each matrix (rows vs rows, columns vs columns) meet the following criteria:

  • The dimensions are equal, or
  • One dimension is of size 1.

The way that broadcasting works are by duplicating the smaller array so that it is the dimensionality and size as the larger array.

Initially, this method was developed for NumPy, but it has also been adopted more broadly in other numerical computational libraries, such as Theano, TensorFlow, and Octave.


Having Machine Learning, Data Science or Python Interview? Check 👉 40 NumPy Interview Questions

Q13: 
What is an Orthogonal Matrix? Why is computationally preferred?

An orthogonal matrix is a type of square matrix whose columns and rows are orthonormal unit vectors, e.g. perpendicular, and have a length or magnitude of 1. Formally, it's defined as follows:

QtQ=QQt=IQ^t Q = Q Q^t = I

Where Q is the orthogonal matrix, Qᵗ indicates the transpose of Q, and I is the identity matrix. From the above definition, we can see that

Q1=QtQ^{-1} = Q^t

Therefore, the orthogonal matrix is preferred because they are computationally cheap and stable to calculate their inverse as simply their transpose.


Having Machine Learning, Data Science or Python Interview? Check 👉 42 Linear Algebra Interview Questions

Q14: 
What is the determinant of a square matrix? How is it calculated?

Answer

The determinant is a scalar value that is a function of the entries of a square matrix. The determinant of a matrix A is denoted det(A), det A, or |A|. Geometrically, it can be viewed as the volume scaling factor of the linear transformation described by the matrix.

In the case of a 2 × 2 matrix the determinant is calculated following the next diagram:

That is, the determinant is equal to the product of the elements along the plus-labeled arrow minus the product of the elements along the minus-labeled arrow.

Similarly, for a 3 × 3 matrix A, its determinant is

Each determinant of a 2 × 2 matrix in the equation above is called a minor of the matrix A.

For an n × n matrix, the previous procedure is extended and provides a recursive definition for the determinant, known as a Laplace expansion.


Having Machine Learning, Data Science or Python Interview? Check 👉 42 Linear Algebra Interview Questions

Q15: 
What’s the difference between a Matrix and a Tensor?

Answer
Source: medium.com
  • In simple terms, a matrix is a grid of n × m (say, 3 × 3) numbers surrounded by brackets. We can add and subtract matrices of the same size, multiply one matrix with another as long as the sizes are compatible, and multiply an entire matrix by a constant.

  • A tensor is a generalization of matrices to N-dimensional space. That is, it could be a 1-D matrix (a vector), a 3-D matrix (something like a cube of numbers), or even a 0-D matrix (a single number), etc. The dimension of the tensor is called rank.

  • A tensor can be also seen as a mathematical entity that lives in a structure and interacts with other mathematical entities. If one transforms the other entities in the structure in a regular way, then the tensor obeys a related transformation rule. This dynamical property of a tensor is also a key to distinguish it from a mere matrix. For example, any rank-2 tensor can be represented as a matrix, but not every matrix is really a rank-2 tensor. The difference depends on the transformation rules that have been applied to the entire system.


Having Machine Learning, Data Science or Python Interview? Check 👉 42 Linear Algebra Interview Questions

Q16: 
Given a matrix M, how do you calculate its Singular Value Decomposition?

Answer
Join MLStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 25k+ Data Scientists Who Trust MLStack.Cafe

Q17: 
When performing regularization, when would you choose L1-norm over L2-norm?

Answer
Join MLStack.Cafe to open this Answer. It's Free!
Sign in with GoogleSign in with Google. Opens in new tab
Join 25k+ Data Scientists Who Trust MLStack.Cafe
 

Prepare for AI developer and engineer interviews with 19 answered OpenClaw questions covering Gateway architecture, channels, agent workspaces, memory, MCP, model failover, multi-agent routing, security, sandboxing, approvals, and remote operations....

Prepare for AI agent developer interviews with 15 Model Context Protocol (MCP) questions covering tools, resources, prompts, JSON-RPC, transports, roots, sampling, security, and practical MCP server design....

Amazone runs the internet as we know it. Amazon Web Services (AWS) offers a comprehensive suite of machine learning (ML) services that cater to various needs and expertise levels. Follow along and learn the 23 most common AWS machine-learning intervi...

Azure Machine Learning (Azure ML) is a cloud-based service for creating and managing machine learning solutions. It’s designed to scale, distribute, and deploy machine learning models to the cloud. Follow along and learn the 23 most common Azure Mach...
Hadoop is an open-source big data processing framework. It leverages distributed computing to store and process large datasets in a fault-tolerant manner. According to recent reports, Apache Hadoop is one of the most sought-after big data skills with...
Apache Spark is a unified analytics engine for large-scale data processing. It is built to handle various use cases in big data analytics, including data processing, machine learning, and graph processing. Follow along and learn the 23 most common an...
Scala is a powerful language with functional programming capabilities that can be a good choice for data science, especially in big data and distributed computing scenarios. As an example, Apache Spark, a popular distributed data processing framework...
PyTorch popularity as a Deep Learning framework of choice is on the rise. As of December 2022, 62% of the academic papers were implemented in PyTorch whereas only 4% were for TensorFlow. Follow along and prepare effectively with these key 30 PyTorch ...
The use of Artificial Intelligence (AI) in machine learning and data science enabled advancements in areas such as natural language processing, computer vision, recommendation systems, fraud detection, predictive analytics, and personalized medicine....
Optimization algorithms are extensively used in training machine learning models. Data engineers employ algorithms like gradient descent, stochastic gradient descent, and variants (e.g., Adam, RMSprop) to optimize the model parameters and minimize th...