Pattern

2D matrix manipulation

Treat a grid as coordinates and boundaries rather than a graph, and get index arithmetic and in-place edits right.

What it is

Matrix questions in this bank are not graph-traversal problems (no flood fill, no shortest path) — they're about manipulating a fixed 2D grid in place: rotating it, zeroing rows and columns, walking it in a spiral, or exploiting sortedness across rows and columns to search faster than checking every cell. The core skill is coordinate bookkeeping: writing down exactly how (row, col) maps to a new position or a new value, and doing that transformation without an extra grid when the interviewer asks for in-place.

When it applies

Tells in the problem

The classic mistake

Corrupting the very data you still need to read — overwriting a cell with a new value (a rotated position, a zero marker, a new generation's state) before every other cell that depends on the original value has been read — is the mistake that shows up most. This happens because in-place transformations look like simple loops, and it's easy to write the update before double-checking whether some other iteration of the same loop — or the next row's computation — still needs the original value. The fix is to identify, before writing any code, which values are read-after-write hazards, and either process in an order that avoids the conflict (corners inward, four cells at a time for rotation) or use an in-band marker (like negating a value, or using a placeholder distinct from the domain) to record "new" state without erasing the "old" state prematurely.

Questions to start with

All 6 2d matrix manipulation questions

Problem statement, examples and hints are free on every page; the worked approach, solution and tests are in the company guides.

Free starter pack

13 must-tier questions with full solutions

Sign in with an email — no purchase — and read them in the full reader.

No password, no spam. One sign-in email; you can read the pack in the full reader.