The sliding window pattern uses a subset (or window) that moves across the data structure, typically arrays or strings, to keep track of a specific condition (like max, min, sum, or unique elements) over contiguous elements. It’s great for reducing nested loops (O(n²)) into single-pass solutions (O(n)) by efficiently expanding or shrinking the window.
- When you need to process contiguous subarrays or substrings.
- When tracking sums, counts, or properties within a fixed or variable-sized window.
- When optimizing for minimal/maximal subarray results without reprocessing the entire window each time.
- Expanding the window by moving the right pointer forward.
- Shrinking the window by moving the left pointer when a condition is exceeded.
- Using a hashmap, set, or counter to track elements inside the window.
| # | Name | Java | Python | Go | Link | Company |
|---|---|---|---|---|---|---|
| 1 | Maximum Average Subarray I | ✅ | ✅ | ❌ | LeetCode #643 | |
| 2 | Longest Substring Without Repeating Characters | ✅ | ✅ | ❌ | LeetCode #3 | |
| 3 | Longest Repeating Character Replacement | ✅ | ❌ | ❌ | LeetCode #424 | |
| 4 | Minimum Window Substring | ✅ | ❌ | ❌ | LeetCode #76 | |
| 5 | Permutation in String | ✅ | ✅ | ❌ | LeetCode #567 | |
| 6 | Quadratic Consecutive Sequence Sum | ✅ | ✅ | ❌ | #TrendMicro |
|
| 7 | Distance Between Scrambled Programmer Words | ✅ | ✅ | ❌ | #ExpediaGroup |