← Questions

Matrix transforms and what eigenvalues mean

0
GoogleCodingLast asked Feb 2026

A coding interview question asked in Google's onsite coding round, last asked Feb 2026. It tests linear algebra and matrices.

A fundamentals round on matrices: implement a few transforms carefully, then explain what eigenvalues tell you. Expect to write code for the first parts and to reason aloud for the rest.

Part 1: transforms in place

Unless you agree on a different interface with the interviewer, assume:

  • transpose(m: list[list[int]]) -> None — transposes a square matrix in place.
  • rotate_90(m: list[list[int]]) -> None — rotates a square matrix 90 degrees clockwise in place, with O(1) extra space.
rotate_90([[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]])   ->  [[7, 4, 1],
                              [8, 5, 2],
                              [9, 6, 3]]

Then: rotate counter-clockwise, and by 180 degrees; what changes for a non-square matrix, where in-place rotation is no longer possible.

Part 2: sparse matrices

Most entries are zero. Choose a representation (a map from (row, col) to value, coordinate lists, or compressed sparse rows), and implement multiply(a, b) for two sparse matrices so the cost depends on the non-zero entries rather than the full size.

a = {(0, 0): 1, (0, 2): 2}            # 2 x 3
b = {(0, 1): 3, (2, 1): 4}            # 3 x 2
multiply(a, b)  ->  {(0, 1): 11}      # 1*3 + 2*4

Part 3: eigenvalues

  • What eigenvectors and eigenvalues of a matrix are: directions the transform only stretches, and by how much.
  • What they say about rotation, scaling and projection matrices (a 2D rotation by 90 degrees has no real eigenvectors).
  • How to compute them for a 2 x 2 matrix from the characteristic polynomial, and how libraries do it for large matrices (power iteration for the largest, QR iteration for all).
  • Where they matter in practice: PCA, stability of iterative methods, PageRank.

Clarifying questions

  • Is the matrix always square? What element types?
  • In place really means O(1) extra memory?
  • How sparse is "sparse", and which operations on it matter most?

What a strong answer covers

  • Rotation as transpose then reverse each row, or a four-way swap ring by ring; index arithmetic checked on a 3 x 3 and a 4 x 4.
  • A sparse format chosen for the operation: row-indexed maps make multiplication iterate only over matching non-zeros.
  • Correct, intuitive explanations of eigenvalues, with a worked 2 x 2 example such as [[2, 0], [0, 3]] (eigenvalues 2 and 3 along the axes).

Follow-ups

  • Extend the rotation idea to a 3D tensor: rotate each 2D slice, or permute axes.
  • How would you store a sparse tensor with three or more dimensions?
  • Power iteration converges slowly. Why, and what helps?

Where it's asked

  • Google · Onsite coding · Last asked Feb 202645 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)

What it tests

linear algebra · matrices

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding