What is Sliding Window?
The Sliding Window pattern maintains a dynamic window over a sequence to find optimal subarrays or substrings. It reduces nested loops from O(n²) to O(n) by reusing computations.Quick Recognition: If you see “contiguous subarray” or “substring” + need to find max/min/count, Sliding Window is likely the answer!
Pattern Recognition Checklist
Use Sliding Window When
- Problem mentions contiguous elements
- Need subarray or substring
- Looking for maximum/minimum length
- Constraint on window (at most K, exactly K)
- Can solve by expanding and shrinking
Don't Use When
- Elements don’t need to be contiguous
- Need all subsets (use Backtracking)
- Sorted array hints (use Two Pointers)
- Need position of elements (use HashMap)
Three Types of Sliding Window
When to Use
Contiguous Subarrays
Finding max/min sum, average, or product of k elements
Substring Problems
Longest/shortest substring with certain properties
Fixed Size Window
Problems asking about “exactly k” or “window of size k”
Variable Size Window
Problems with “at most k” or “minimum length”
Pattern Variations
1. Fixed Size Window
Window size remains constant throughout.2. Variable Size Window (Expand and Shrink)
Window grows and shrinks based on conditions.3. Window with HashMap/Counter
Track element frequencies within the window.Classic Problems
1. Maximum Sum Subarray of Size K
1. Maximum Sum Subarray of Size K
Problem: Find the maximum sum of any contiguous subarray of size k.Approach: Fixed window - slide and update sum by adding new element and removing old.Time: O(n) | Space: O(1)
2. Longest Substring Without Repeating Characters
2. Longest Substring Without Repeating Characters
Problem: Find length of longest substring without duplicate characters.Approach: Variable window with HashSet. Shrink when duplicate found.Time: O(n) | Space: O(min(n, m)) where m is charset size
3. Minimum Window Substring
3. Minimum Window Substring
Problem: Find smallest substring containing all characters of another string.Approach: Variable window with character frequency tracking. Expand to include all, shrink to minimize.Time: O(n + m) | Space: O(m)
4. Fruit Into Baskets
4. Fruit Into Baskets
Problem: Maximum fruits you can collect with only 2 baskets (2 types).Approach: Longest subarray with at most 2 distinct elements.Time: O(n) | Space: O(1)
5. Permutation in String
5. Permutation in String
Problem: Check if s2 contains a permutation of s1.Approach: Fixed window of size len(s1), compare character frequencies.Time: O(n) | Space: O(1)
Template Code
Sliding Window Decision Tree
Common Mistakes
Debugging Checklist
When your Sliding Window solution fails:1
Check Expand Logic
Are you correctly adding the new element to your window state?
2
Check Shrink Condition
Is the while condition correct? (
> k vs >= k matters!)3
Check Shrink Logic
Are you correctly removing the left element from state?
4
Check Result Update
Are you updating result at the right time? (Usually after shrink)
5
Check Edge Cases
Empty string? K = 0? All same characters?
Complexity Quick Reference
| Problem Type | Time | Space | Window Type |
|---|---|---|---|
| Max sum of K elements | O(n) | O(1) | Fixed |
| Longest substring no repeat | O(n) | O(min(n,m)) | Variable |
| Min window substring | O(n+m) | O(m) | Variable |
| Subarrays with K distinct | O(n) | O(K) | Variable |
| Max consecutive ones with K flips | O(n) | O(1) | Variable |
Interview Problems by Company
- Easy
- Medium
- Hard
| Problem | Company | Key Concept |
|---|---|---|
| Max Average Subarray I | Amazon | Fixed window basics |
| Contains Duplicate II | Meta | Fixed window + set |
| Max Consecutive Ones | Simple sliding | |
| Find All Anagrams | Amazon, Meta | Fixed window + frequency |
Interview Tips
How to Explain Your Approach
How to Explain Your Approach
Script for interviews:
- “I see this is about contiguous subarrays, so I’ll use Sliding Window.”
- “I’ll maintain a window with two pointers: left and right.”
- “Right pointer expands the window, left pointer shrinks it.”
- “I’ll track [state] to know when window is valid/invalid.”
- “This gives us O(n) time since each element is visited at most twice.”
When Interviewer Says...
When Interviewer Says...
| Interviewer Says | You Should Think |
|---|---|
| ”Find contiguous subarray” | Sliding Window! |
| ”Longest/shortest substring” | Sliding Window! |
| ”At most K distinct” | Variable window + shrink when > K |
| ”Exactly K” | AtMost(K) - AtMost(K-1) |
| “All subarrays of size K” | Fixed window |
| ”Contains permutation” | Fixed window + frequency match |
The AtMost Trick Explained
The AtMost Trick Explained
Why “Exactly K = AtMost(K) - AtMost(K-1)”?
AtMost(K)counts all windows with 0, 1, 2, … K distinct elementsAtMost(K-1)counts all windows with 0, 1, 2, … K-1 distinct elements- Subtracting gives us ONLY windows with exactly K distinct elements
The “Exactly K” Trick
Practice Problems
| Problem | Difficulty | Link |
|---|---|---|
| Maximum Average Subarray I | Easy | LeetCode 643 |
| Longest Substring Without Repeating | Medium | LeetCode 3 |
| Minimum Window Substring | Hard | LeetCode 76 |
| Sliding Window Maximum | Hard | LeetCode 239 |
| Subarrays with K Different Integers | Hard | LeetCode 992 |
Practice Roadmap
1
Day 1: Fixed Window
- Solve: Max Average Subarray, Contains Duplicate II
- Focus: Understanding the slide (add new, remove old)
2
Day 2: Variable Window - Max
- Solve: Longest Substring Without Repeat, Longest Repeating Replacement
- Focus: Expand + shrink when invalid
3
Day 3: Variable Window - Min
- Solve: Minimum Size Subarray Sum, Minimum Window Substring
- Focus: Shrink while valid (opposite of max)
4
Day 4: Exactly K Problems
- Solve: Subarrays with K Different Integers
- Focus: The AtMost(K) - AtMost(K-1) trick
Memory Trick: SLIDE
Remember SLIDE for Sliding Window:- Subarray/Substring problem?
- Length constraints?
- Iterate with two pointers?
- Dynamic window size?
- Expand and shrink?