Implement matrix transforms and discuss eigenvalues
GoogleSoftware Engineer · OnsiteLast updated
This problem tests your ability to perform in-place 2D matrix transformations and your conceptual understanding of how linear algebraic properties like eigenvalues describe matrix behavior.
Part 1: Coding
Given an n x n 2D matrix representing an image, rotate the image by 90 degrees clockwise in-place. You must modify the input matrix directly without allocating a second matrix.
Function Signature
def rotate(matrix: List[List[int]]) -> None:
Constraints
n == matrix.length == matrix[i].length1 <= n <= 500-1000 <= matrix[i][j] <= 1000
Examples
Example 1:
Input: matrix = [[1, 2], [3, 4]]
Output: [[3, 1], [4, 2]]
Example 2:
Input: matrix = [[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]]
Output: [[15, 13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7, 10, 11]]
Part 2: Conceptual Discussion
- Explain what an eigenvalue and eigenvector represent in the context of a linear transformation matrix
A. - If a matrix is used to represent a coordinate transformation, what does a large eigenvalue (relative to others) tell you about how the transformation behaves along the direction of its corresponding eigenvector?
- How would you store a matrix that is 99% filled with zeros efficiently, and how does this storage affect matrix-vector multiplication performance?
Hints
Hint 1
To perform an in-place 90-degree rotation, consider splitting the operation into two steps: a transposition followed by a row reversal.
Hint 2
The eigenvalue equation `Av = λv` implies that vectors scaled by `λ` do not change direction under transformation `A`.
Hint 3
For sparse matrices, avoid standard 2D arrays and consider using coordinate formats (COO) or Compressed Sparse Row (CSR) formats to store only non-zero entries.
Follow-up questions
- How would you modify your rotation algorithm if the matrix were rectangular (m x n) instead of square?
- If the matrix represents a large social network graph, why might computing the principal eigenvalue (the one with the largest absolute value) be useful for ranking nodes?
- How does the concept of eigenvalues extend to higher-order tensors in machine learning, such as in Principal Component Analysis (PCA) or spectral clustering?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.