</> MAANG.io
coding interview · 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

In-Place Partitioning & Rearrangement

Core idea: One pointer reads every element; a second pointer writes only the elements you want to keep, packing them toward the front. The region behind the write pointer is always the finished, cleaned result — so you transform the data in a single forward pass with O(1) extra space.


The read–write pointer, in one picture

Array strip with read pointer sweeping left to right and write pointer marking the clean region

Think of a conveyor belt feeding a packing worker: items stream past (read), and the worker drops only the good ones into a clean box, left to right (write). Bad items are skipped, never shifting anything backward. The clean region grows forward and never needs a second array.

Read/write loop: read an element, decide keep it, write or skip, then read += 1 and repeat

This is the same machine behind garbage-collector compaction (slide live objects over dead ones), log/LSM compaction (drop tombstones), and array defragmentation — all O(n), all in place.


Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account — no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.