Sliding Window
The sliding window technique optimizes problems involving contiguous sequences by maintaining a window that expands or contracts based on constraints. It reduces O(n²) brute force to O(n) by avoiding redundant computation.Core Concepts
When to Use Sliding Window
- Contiguous subarray/substring problems
- Need to find optimal window (min/max length)
- Constraint on window contents (sum, characters, duplicates)
- Can adjust window from both ends
Window Types
- Fixed Size: Window of constant size k
- Variable Size: Expand/contract based on condition
- Two Pointers: Left and right boundaries
Key Patterns & Techniques
1. Fixed Window Size
- Window size is given (k)
- Slide window by 1 each time
- Add new element, remove old element
- Time: O(n), Space: O(1) or O(k)
2. Variable Window (Expandable)
- Expand right until condition violated
- Contract left to restore condition
- Track optimal window during process
- Time: O(n), each element visited at most twice
3. At Most K Pattern
- Count subarrays with at most K distinct elements
- Use counter/hash map to track window contents
- Shrink when condition violated
4. Two Counters Pattern
- Track both target and window frequencies
- Expand when missing characters
- Contract when all requirements met
Common Approaches
Fixed Window Template
Variable Window Template
Longest Substring Without Repeating Characters
Minimum Window Substring
Max Consecutive Ones III
Problems in This Category
080. Longest Substring Without Repeating
Medium | Frequency: 40.7%Variable window with hash map tracking last seen position.
037. Minimum Window Substring
Hard | Frequency: 62.4%Two counters pattern - expand until valid, contract for minimum.
040. Max Consecutive Ones III
Medium | Frequency: 62.4%Variable window with constraint on number of zeros.
059. Continuous Subarray Sum
Medium | Frequency: 47.0%Prefix sum modulo with hash map for subarray divisibility.
068. Contains Duplicate II
Easy | Frequency: 40.7%Fixed window of size k to find duplicate within distance.
090. Frequency of Most Frequent Element
Medium | Frequency: 32.0%Sort + sliding window with sum constraint for k operations.
Complexity Characteristics
| Pattern | Time Complexity | Space Complexity | Key Feature |
|---|---|---|---|
| Fixed Window | O(n) | O(1) or O(k) | Slide by 1, add/remove |
| Variable Window | O(n) | O(k) | Each element visited ≤2 times |
| Two Counters | O(n) | O(k) | Track target & window |
| At Most K | O(n) | O(k) | Shrink when count > k |
| With Sort | O(n log n) | O(k) | Sort first for monotonic property |
Interview Tips
Pattern Recognition
- “Longest substring with…” → Variable sliding window
- “Minimum window containing…” → Expand until valid, contract for min
- “Subarray of size k” → Fixed window
- “At most k distinct/zeros” → Variable window with counter
- “All permutations/anagrams” → Fixed window with frequency match
- “Maximum sum subarray of size k” → Fixed window
Common Mistakes
- Moving both pointers in same iteration (double counting)
- Not handling window size correctly (right - left + 1)
- Forgetting to update result before contracting window
- Using set instead of counter when frequencies matter
- Not initializing window state correctly
Key Insights
- Amortized O(n): Each element enters window once, leaves once
- Right pointer always moves forward in outer loop
- Left pointer catches up when constraint violated
- Hash map tracks window contents - increment on add, decrement on remove
- Update result after valid window - either max or min length
- Two pointers on single array - not two separate arrays
Decision Tree
Advanced Patterns
At Most K → Exactly K Trick
Sliding Window with Deque (Min/Max in Window)
Sliding Window on Multiple Arrays
Pro Tip: The key insight for sliding window is that each element is processed at most twice (once when expanding right, once when contracting left), giving amortized O(n) time. If you’re considering all subarrays with nested loops (O(n²)), check if sliding window applies.