Skip to main content
Back to all problems
Beginner guide, Algorithms & data structures

Math & Geometry

In-place matrix manipulation: rotating, spiraling and marking.

The problems in this category are about grids of numbers (matrices): rotating them, walking them in a pattern, or changing them in place. They don't need a fancy data structure. What they need is careful index work, and a few tricks that turn a messy move into simple steps.

Matrix basics

A matrix is an array of rows. matrix[r][c] is the value in row r, column c:

text
          c=0  c=1  c=2
   r=0  [  1    2    3 ]
   r=1  [  4    5    6 ]
   r=2  [  7    8    9 ]
  • Rows: matrix.length. Columns: matrix[0].length.
  • matrix[1][2] is 6: row 1, column 2. Row first, then column.
  • A square matrix has as many rows as columns (n x n).
  • The main diagonal is where r === c: 1, 5, 9.

Trick 1: break a hard move into easy ones

Moving every cell to a new spot in place is hard, because each move overwrites a value you haven't moved yet. Look for simpler moves that combine into the one you want, especially moves made of swaps:

MoveWhat it doesCode idea
Transpose(r, c) ↔ (c, r): rows become columnsswap above the diagonal, c from r + 1
Reverse each rowmirror left ↔ rightrow.reverse()
Reverse row ordermirror top ↔ bottommatrix.reverse()

Combinations:

  • Rotate 90° clockwise = transpose, then reverse each row (Rotate Image).
  • Rotate 90° counter-clockwise = reverse each row, then transpose.
  • Rotate 180° = reverse each row and reverse the row order.

Trick 2: shrinking boundaries

To walk a matrix in layers (like a spiral), keep four boundaries for the part you haven't handled yet: top, bottom, left, right. After you finish a side, move that boundary inward. Stop when they cross.

text
top    → [ 1  2  3  4 ]
         [ 5  6  7  8 ]
bottom → [ 9 10 11 12 ]
           ↑        ↑
          left    right

Watch out for the last single row or column: check the boundaries again before walking back over it (Spiral Matrix).

Trick 3: the matrix as its own notepad

When a problem asks for O(1) extra memory, look for space inside the input you can borrow. In Set Matrix Zeroes, the first row and first column store "clear this column / row" marks. Save whatever information you're about to overwrite (two booleans), and restore or handle it last.

Two-phase updates

If changing a cell affects how you treat other cells, don't change things while you're still reading. First record what should change, then apply it. Otherwise your own changes look like original data.

How to recognize this category

  • The input is a 2-D grid of numbers, and you must rearrange or walk it in a fixed pattern.
  • The problem says in place or asks for O(1) extra space.
  • There's no searching or optimizing, just a precise transformation.

(Grids where you search for paths or regions, like Number of Islands, belong to Graphs instead.)

Common mistakes

  • Mixing up rows and columns, or m and n, especially in non-square matrices.
  • Off-by-one errors at the boundaries: decide whether each loop includes its last index.
  • Swapping each pair twice (which undoes the swap).
  • Overwriting values you still need. Draw a small example (3 × 3 or 3 × 4) and trace your code on paper before running it.

Practice what you learned

The problems go from easier to harder. Try each one on your own and open a hint when you get stuck.