/Interview Study Guide/Algorithms & data structures
Concepts

Matrices & grids

Data structures

2D arrays addressed by [row][col] — many grid problems are implicit graphs.

Definition

A matrix (2D grid) is an array of arrays addressed by [row][col]. Many grid problems are implicit graphs: each cell is a vertex whose neighbours are the cells up, down, left, and right (sometimes the diagonals), so BFS and DFS apply directly without building an explicit graph.

When to use

Reach for grid BFS/DFS for flood-fill, island-counting, and shortest-path-in-a-maze. Watch for in-place transforms (rotate, set-zeroes, spiral) and, above all, careful bounds checking at the edges.