Just added: Algorithms you can run and practice
Updated Aug 6, 2026 Edit on GitHub

Two Pointers & the Sliding Window

A huge number of array and string problems have an obvious solution that checks every pair with two nested loops - and that solution is O(n²), which quietly falls over the moment the input gets big. Two patterns rescue most of those problems and bring them down to a single pass, O(n): the two-pointer technique and the sliding window. They look like tricks the first time you see them, but they're really one idea - keep a couple of positions moving through the array so you never re-scan what you've already seen.

Every example here is Python you can run as you read. Once the shape clicks, you'll start recognizing it in problems that never mention "pointers" or "windows" at all.

How to read this

Read in order. Two pointers comes first because it's the simpler motion (two positions walking toward each other); the sliding window is the same instinct applied to a moving range. The last phase is the payoff: how to look at a fresh problem and tell which pattern it wants.

The phases

  1. Converging Two Pointers · 🟢 Basic - two positions walking inward: reverse a list, check a palindrome, and find a pair that sums to a target on a sorted array.
  2. The Sliding Window · 🟡 Intermediate - a moving range over the data: max sum of k consecutive items, and the longest substring with no repeated character.
  3. Choosing the Pattern · 🟢 Basic - the signals that tell you which pattern a problem wants, and the gotchas (unsorted input, off-by-one bounds) that bite everyone once.