Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
PROBLEM-SOLVING TECHNIQUES

Sliding Window

Grow and shrink a contiguous range while keeping a running total or count.

Intermediate12 problems3 interactive subtopicsO(n) time · O(k) space
Practice in AI DSA Open full visualizer
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/5Grow to “abc”
window "abc" · best = 3
01234567
a
b
c
a
b
c
b
b
left
right
  • inside the window
  • active this step
  • removed / popped
  • ruled out
Space Play←→ StepR Replay
1 / 5
PYTHONs = "abcabcbb"
1def length_of_longest_substring(s):
2 last = {}
3 left = best = 0
4 for right, ch in enumerate(s):
5 if ch in last and last[ch] >= left:
6 left = last[ch] + 1
7 last[ch] = right
▶8 best = max(best, right - left + 1)
9 return best
STEP 1 OF 5Setup

Grow to “abc”

No repeats yet, so the window grows to length 3.

VARIABLES

left
0
right
2
window
"abc"
best
3
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

Two indices bound a window over an array or string. The right edge grows the window; the left edge shrinks it when the window breaks a rule.

Why it matters

Updating a running total as the window moves avoids recomputing every subarray - O(n) instead of O(n²).

Think of it like this

A train window: scenery enters on one side as it leaves the other, and you only track what’s in view.

Use it when

  • Longest or shortest contiguous subarray or substring meeting a condition
  • Fixed-size windows (averages, maximums)
  • At most k distinct items or k changes

Avoid it when

  • Validity isn’t monotonic - e.g. sums with negative numbers
  • The answer needn’t be contiguous (that’s a subsequence)

Complexity cheatsheet

OperationTimeSpace
Variable-size windowO(n)O(1) to O(k)
Every subarrayO(n²)O(1)
Fixed window of size kO(n)O(1)

Often confused with: prefix sums - they handle range sums with negatives; windows need monotonic validity.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

contiguoussubarraysubstringlongestat most k
  • Positive numbers make “shrink while valid” safe
  • Character rules usually mean a count map inside the window
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Moving left backwards when the repeat is outside the window - check last[ch] >= left
  • Recomputing the window’s total from scratch each step
  • Using a window when negatives break monotonicity

Edge cases to test

  • Empty string - return 0
  • One repeated character like "bbbb"
  • All characters unique
Interview tip: State the window’s rule - “no repeated characters between left and right” - and every move becomes easy to justify.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Why does “shortest subarray with sum ≥ k” break the basic window when negatives are allowed?

06

Practice

Apply what you just watched on real interview problems.

Generate Sliding Window problem in AI DSA Practice →
  • BeginnerMaximum Average Subarray ILeetCode 643 ↗ (opens in a new tab)
  • IntermediateLongest Substring Without Repeating CharactersLeetCode 3 ↗ (opens in a new tab)
  • AdvancedMinimum Window SubstringLeetCode 76 ↗ (opens in a new tab)
Two PointersPrefix Sum