Pattern

In-place manipulation

Rearrange an array, matrix, or structure using only the memory it already occupies, without a second copy.

What it is

"In-place" is a constraint more than a single technique: the interviewer wants the transformation done by overwriting the given array, matrix, or tree using O(1) or O(log n) extra space, instead of building a second structure and copying back. The actual mechanics vary by problem — a read/write pointer pair that compacts an array, a index-as-marker trick that reuses array slots to record what's been seen, cycle-following swaps that place each element directly into its final position, or layer-by-layer swaps for a matrix — but all of them share the discipline of never overwriting a value before you've captured what you still need from it.

When it applies

Tells in the problem

The classic mistake

Overwriting a cell before reading the value that was about to be moved out of it — the most common form is using a sentinel or "already cleared" marker (like writing 0 into a matrix cell to mark "clear this row/column later") that then gets misread on a later pass as if it were original data, cascading zeros where none belonged. The general fix is to separate the "which positions need to change" decision from the "apply the change" step: record markers somewhere that can't be confused with real data (a matrix's first row/column, the sign bit of an in-range value, a rotation done via reversal instead of shifting) and only then perform destructive writes, checking each cell's original state before you decide anything from it.

Questions to start with

All 10 in-place 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.