← All patterns
3 / 32
Scanning & Windows

Sliding Window

When you see it

Signal words

If the prompt uses any of these, this pattern should come to mind first.

substringsubarraycontiguouslongestshortestat most kwindow
Practice

Drill this pattern with an AI trainer — reply only with Hint, Next, Lost, or paste your code.

Ask yourself

  1. Is the answer a contiguous run (substring / subarray)?
  2. Are you asked for longest / shortest / count under a constraint?
  3. Can you expand right, then shrink left while invalid?

Classic problem

Longest Substring Without Repeating Characters (LeetCode 3) — return the length of the longest substring with all-distinct characters. Grow right, and shrink left until the window is valid again.

Input: s = "abcabcbb"

Output: 3 — the longest all-distinct substring is "abc".

Watch it run

Common pitfalls

Shrinking with if instead of while.

Updating the answer before restoring validity.