All visualizationsDSA Patterns · 74 of 76
🧪 Your experiment — type any numbers, then run the algorithm on them
4 numbers (1–9) and a target (2–30). The code below rewrites itself with your values.
nums =target =
Sliding Window Lab — run it on YOUR numbers
Type any four numbers and a target, then watch the real algorithm hunt for the shortest window through them.
💡
THE BIG IDEA
You have watched the sliding window run on our examples. Now it runs on yours. Type four numbers and a target — the code rewrites itself with your values, and every step is computed live from what you typed. Try to build an input where one number wins alone, and one where no window ever qualifies. If you can predict what the window will do before pressing play, you have got the pattern.
Move your mouse over any line to see what it does. Hover a loop line and it plays through every repeat. Or press ▶ Play to watch the whole thing. On a phone, tap the arrows.
1nums = [2, 1, 4, 3]
2target = 7
3left, total, best = 0, 0, 99
4for right in range(4):
5 total = total + nums[right]
6 while total >= target:
7 best = min(best, right - left + 1)
8 total = total - nums[left]
9 left = left + 1
10print("smallest window =", best)
Your numbers are [2, 1, 4, 3] and your target is 7. We want the SHORTEST run of neighbours adding up to at least 7.
Press play and watch the window work on the numbers you chose. Change them afterwards and see how the story changes.
📦 Memory boxes
what the program is remembering right now
nothing remembered yet
🖥️ What the computer shows
the answers the program prints out
nothing yet
1/30
Pattern: Sliding Window (variable size)
UP NEXT IN DSA PATTERNS
Sliding Window — longest stretch with no repeats
Watch the window stretch to the right, hit a repeat, shrink from the left, and carry on.
Coin Change Lab — race the table against greedySliding Window — longest stretch with no repeats

Sliding Window (variable size)

Two markers that only ever move forward.

Reach for it when

  • You need the longest or shortest run of NEIGHBOURING items that satisfies some rule.
  • The obvious solution restarts the search at every position, giving you n² work.
  • Extending the run and shrinking it can both be done cheaply — usually by adding or subtracting one item.

The skeleton

left = 0
for right in range(len(items)):
    add items[right] to the window
    while the window breaks the rule (or is good enough to shorten):
        remove items[left] from the window
        left = left + 1
    record the answer for the current window

Where people go wrong

  • Recording the answer at the wrong moment. For "shortest", record BEFORE shrinking — once you drop an item the window may no longer qualify.
  • Moving left backwards. If you ever need to, it is not a sliding window — the linear cost depends on both markers being one-way.
  • Starting best at 0 for a minimum problem. It must start impossibly high, or nothing can improve on it.
  • Forgetting that the window is the range left..right INCLUSIVE, so its length is right - left + 1, not right - left.
Time: O(n) — each item enters the window once and leaves at most once
Space: O(k) for whatever you keep about the window
▶ Longest stretch with no repeats▶ Smallest run reaching a targetArrays — Sliding Window →Arrays — Two Pointers →