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
- Is the answer a contiguous run (substring / subarray)?
- Are you asked for longest / shortest / count under a constraint?
- 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".
Common pitfalls
Shrinking with
ifinstead ofwhile.
Updating the answer before restoring validity.