Python/Algorithms for interviews 9 min

Two pointers and sliding windows

Scan a sequence once instead of comparing every pair.

two pointerssliding window

On a sorted sequence, two indexes moving toward each other find pairs in linear time: if the sum is too small move the left pointer up, if too big move the right one down.

python · editable
loading editor…

A sliding window keeps a running summary of a contiguous block: add the entering element, drop the leaving one, never recompute the whole block.

python · editable
loading editor…

Try it: Return the starting index of the best window too.

Variable-size windows grow the right edge while a condition holds and shrink the left edge when it breaks — 'longest substring without repeats' is the classic.