Matrix Coding Problems: 107 Questions with Solutions

107 matrix coding problems — 28 easy · 57 medium · 22 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.

  • Problems: 107
  • By difficulty: 28 easy · 57 medium · 22 hard
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
  • Cost: Free on every plan; sign in to run and submit

A two-dimensional grid: walk it in spiral order, rotate it in place, search a sorted one, count islands, mark rows and columns. Matrix problems are index bookkeeping with a purpose — keeping (row, column) straight, staying inside the bounds, and choosing between visiting every cell and exploiting the grid's structure.

How matrix works, step by step

before123456789after012012741852963rotated 90° clockwise
Rotating a square matrix 90° clockwise in place, with transpose and mirror. Example: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  1. A 90° clockwise turn sends the value at (i, j) to (j, 2 − i). Doing that directly needs cycles of four moves; it is simpler as two passes of plain swaps, a transpose and then a mirror of each row.
  2. Transpose first: swap m[0][1] = 2 with m[1][0] = 4 across the main diagonal. The diagonal cells 1, 5 and 9 map to themselves and never move.
  3. Swap m[0][2] = 3 with m[2][0] = 7 across the main diagonal. Only cells above the diagonal start a swap, so no pair is swapped back.
  4. Swap m[1][2] = 6 with m[2][1] = 8 across the main diagonal. The transpose is done: row i now holds what column i held.
  5. Now mirror each row. Row 0, [1, 4, 7], becomes [7, 4, 1]: its ends swap and the middle stays. Together with the transpose this moves (i, j) to (j, 2 − i), exactly the turn.
  6. Row 1, [2, 5, 8], becomes [8, 5, 2]. Each row is mirrored on its own, so the rows can be done in any order.
  7. Row 2, [3, 6, 9], becomes [9, 6, 3], the last swap: every value has reached its rotated place.
  8. Rotated: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]. The old first column 7, 4, 1, read bottom-up, is the new first row. Every cell is touched a constant number of times: O(n²) time and O(1) extra space.

Matrix study plan

14 of the 107 Matrix problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 93 in the full list below are practice at your own pace. Then move on to Simulation.

Day 1

Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

Day 2

Medium problems: the same pattern with one twist each. Name the twist before you code.

Day 3

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 4

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 5

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 6

Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

Day 7

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Day 8

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Next topic: Simulation

Matrix: the essentials

When to reach for it

The input is m × n rows and columns: an image, a game board, a map of land and water, a table sorted along its rows and columns. The question picks the tool. Transforming the whole grid (rotate, transpose, spiral order) is index arithmetic. Anything about connected regions or the fewest moves between cells is a graph search with cells as nodes — see Breadth-First Search.

The pattern

Name the dimensions once, rows = len(grid) and cols = len(grid[0]), and walk neighbours with a list of directions rather than four copied blocks. A 90° clockwise rotation is a transpose followed by reversing each row. To mark cells in place, borrow spare state from the grid itself — the first row and column, or a value outside the input's range — instead of allocating a second grid.

DIRS = [(1, 0), (-1, 0), (0, 1), (0, -1)]

def neighbours(grid, r, c):
    rows, cols = len(grid), len(grid[0])
    for dr, dc in DIRS:
        nr, nc = r + dr, c + dc
        if 0 <= nr < rows and 0 <= nc < cols:
            yield nr, nc

Cost

Visiting every cell is O(m × n), the floor for most matrix questions. A matrix sorted along rows and columns can be searched in O(m + n) by starting at a corner. In-place transforms use O(1) extra space.

Common mistakes

  • Mixing up rows and columns: grid[r][c] is row r, column c, and naming them x and y invites grid[x][y] slips.
  • Assuming a square grid, so code that works for n × n breaks on m × n.
  • Overwriting a cell whose original value a later step still needs (Game of Life, Set Matrix Zeroes).
  • Building rows with [[0] * n] * m in Python, which makes every row the same list.

Start with

All matrix problems

Easy (28)

Medium (57)

Hard (22)

Companies that ask matrix problems

Next topic: Simulation