Just added: Algorithms you can run and practice
All topics / Two Pointers & the Sliding Window

Two Pointers & the Sliding Window

Two array patterns that turn nested-loop O(n²) scans into single-pass O(n) code: converging two pointers on a sorted array, the fixed and variable sliding window, and how to spot which one a problem is quietly asking for.

Download EPUB
  1. Converging Two Pointers The converging two-pointer pattern: start one pointer at each end and walk them inward. Reverse a list in place, check a palindrome, and find a pair summing to a target on a sorted array - all in a single O(n) pass with no extra memory.
  2. The Sliding Window The sliding window pattern: keep a moving range over an array or string and update a running result as it slides, instead of recomputing from scratch. The fixed window (max sum of k consecutive items) and the variable window (longest substring without repeats).
  3. Choosing the Pattern How to recognize which pattern a problem wants - converging two pointers vs the sliding window - from the signals in the problem statement, plus the gotchas that trip people up: unsorted input, the wrong loop bound, and forgetting to shrink the window.