</> 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

Same Direction / Expanding Window

Core idea: Both pointers travel left โ†’ right, never backward. A fast (read/scan) pointer explores ahead while a slow (write/anchor) pointer trails, marking a boundary you maintain โ€” the end of the compacted region, or the start of the current run. Because each pointer crosses the array at most once, the work is O(n).


The shape of the pattern

Two same-direction pointers, slow and fast, over an array with a maintained region bracket

The two same-direction pointers move at different speeds, and the gap between them encodes something useful: how many elements you've kept, how long the current window is, or where a run began. This is what lets you avoid the O(nยฒ) "for each element, re-scan the rest" loop.


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.