Knowledge Guide
HomeDSASliding Window

Sliding Window — Fixed vs Variable, Traced

From O(n²) nested loops to one O(n) pass

Many "longest/shortest/contains substring or subarray" problems look like "check every range" = O(n²). The sliding window keeps a range [L, R] and moves both pointers only right: grow R to include more, shrink L when a constraint breaks. Each index enters and leaves the window once → O(n).

A window over a character array grows by moving R right and shrinks by moving L right when a duplicate appears
A window over a character array grows by moving R right and shrinks by moving L right when a duplicate appears

The two shapes

// VARIABLE window — longest substring without repeating chars
int L=0, best=0; Set<Character> win = new HashSet<>();
for (int R=0; R < s.length(); R++) {
    while (win.contains(s.charAt(R))) win.remove(s.charAt(L++));  // shrink until valid
    win.add(s.charAt(R));
    best = Math.max(best, R - L + 1);                            // window is valid here
}
return best;

// FIXED window of size k — slide both together
long sum=0, best=Long.MIN_VALUE;
for (int R=0; R < n; R++) {
    sum += a[R];
    if (R >= k) sum -= a[R - k];   // drop the element leaving the window
    if (R >= k-1) best = Math.max(best, sum);
}

The trace ("abcabcbb")

RcharwindowLbest
0–2a b c"abc"03
3a (dup)shrink past old a → "bca"13
4b (dup)"cab"23

Pitfalls

Takeaways


Re-authored for this guide; window-slide diagram hand-authored as SVG. See also: Two Pointers, Hashing, Arrays.

🤖 Don't fully get this? Learn it with Claude

Stuck on Sliding Window — Fixed vs Variable, Traced? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Sliding Window — Fixed vs Variable, Traced** (DSA) and want to truly understand it. Explain Sliding Window — Fixed vs Variable, Traced from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Sliding Window — Fixed vs Variable, Traced** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Sliding Window — Fixed vs Variable, Traced** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Sliding Window — Fixed vs Variable, Traced** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes