← Back to the arrays page

2D Array / Matrix

Rows × columns of contiguous data. Traversal patterns (spiral, diagonal, transpose) and grid-graph algorithms live here.

What to know

  • Row-major traversal is dramatically faster than column-major due to cache lines.
  • In-place rotation = transpose + reverse each row.
  • Many DP problems (edit distance, unique paths) are matrices where each cell depends on neighbors.

In the wild: Image pixels, spreadsheets, game boards, ML tensors before they grow more dimensions.

Algorithms to reach for

Spiral / diagonal traversal

O(rows · cols)

Layer-by-layer boundary walking

Matrix rotation in place

O(n²)

Rotate image 90° without extra memory

Staircase search

O(rows + cols)

Search a row-and-column sorted matrix from a corner

Practice problems — with full guides

Each problem has its own page: progressive hints, how to approach it, and the full solution.