Sliding Window Pattern: Template + 116 LeetCode Problems
Collapse a nested loop over every subarray into a single pass with two indices.
- 12 Easy
- 74 Medium
- 30 Hard
- O(n) time
What the sliding window pattern is
A sliding window holds one contiguous stretch of the input between two indices, along with a running summary of what is inside it — a sum, a character count, a distinct-element count. Because the summary is updated incrementally as an index moves, advancing the window costs O(1) instead of re-reading it, which is what turns an O(n²) scan of every subarray into one linear pass. The right index always moves forward; the left index moves only while the window violates the constraint, so each element is added once and removed at most once. Two forms cover almost everything: a fixed window slides both ends in lockstep and answers questions about every subarray of length k, while a variable window grows greedily and shrinks from the left until it is legal again, answering questions about the longest or shortest subarray satisfying a condition.
When to use it
- The answer is a contiguous subarray or substring — not a subsequence, which cannot be described by two indices.
- You can update the window's summary in O(1) when an element enters or leaves it.
- The constraint is monotone: if a window is too long or too costly, extending it further cannot fix it.
- The brute force is two nested loops where the inner one recomputes work the outer one already did.
The sliding window template in Python
The shape, not a solution to any one problem. Adapt the condition and the summary being maintained; the skeleton stays the same across the 116 problems listed below.
def longest_valid_window(nums):
left = 0
window = 0 # running summary of nums[left : right + 1]
best = 0
for right, value in enumerate(nums):
window += value # extend on the right
while window_is_illegal(window):
window -= nums[left] # shrink on the left until legal again
left += 1
best = max(best, right - left + 1)
return bestComplexity characteristics
- Time
- O(n)
- Auxiliary space
- O(1) to O(k)
Each index enters the window once and leaves it at most once, so the two pointers together travel at most 2n steps no matter how long any individual window gets — that is what makes a single pass equivalent to the O(n²) scan over every subarray. The space is whatever the running summary costs: O(1) for a sum or a counter, O(k) for a map of the elements currently inside the window, bounded by the alphabet rather than by the input length.
All 116 sliding window LeetCode problems
Every problem in the library the sliding window pattern applies to, grouped by LeetCode's own difficulty rating. 102 of the 116 carry a complete Python solution with a worked example and complexity analysis; the rest are listed for completeness, with the LeetCode Premium ones marked.
Easy (12)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 219 | Contains Duplicate II | Easy | Array, Hash Table, Sliding Window |
| 594 | Longest Harmonious Subsequence | Easy | Array, Hash Table, Counting +2 |
| 643 | Maximum Average Subarray I | Easy | Array, Sliding Window |
| 1176 | Diet Plan PerformancePremium | Easy | Array, Sliding Window |
| 1652 | Defuse the Bomb | Easy | Array, Sliding Window |
| 1763 | Longest Nice Substring | Easy | Bit Manipulation, Hash Table, String +2 |
| 1876 | Substrings of Size Three with Distinct Characters | Easy | Hash Table, String, Counting +1 |
| 1984 | Minimum Difference Between Highest and Lowest of K Scores | Easy | Array, Sorting, Sliding Window |
| 2269 | Find the K-Beauty of a Number | Easy | Math, String, Sliding Window |
| 2379 | Minimum Recolors to Get K Consecutive Black Blocks | Easy | String, Sliding Window |
| 2760 | Longest Even Odd Subarray With Threshold | Easy | Array, Sliding Window |
| 2932 | Maximum Strong Pair XOR I | Easy | Bit Manipulation, Trie, Array +2 |
Medium (74)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 3 | Longest Substring Without Repeating Characters | Medium | Hash Table, String, Sliding Window |
| 187 | Repeated DNA Sequences | Medium | Bit Manipulation, Hash Table, String +3 |
| 209 | Minimum Size Subarray Sum | Medium | Array, Binary Search, Prefix Sum +1 |
| 395 | Longest Substring with At Least K Repeating Characters | Medium | Hash Table, String, Divide and Conquer +1 |
| 413 | Arithmetic Slices | Medium | Array, Dynamic Programming, Sliding Window |
| 424 | Longest Repeating Character Replacement | Medium | Hash Table, String, Sliding Window |
| 438 | Find All Anagrams in a String | Medium | Hash Table, String, Sliding Window |
| 567 | Permutation in String | Medium | Hash Table, Two Pointers, String +1 |
| 658 | Find K Closest Elements | Medium | Array, Two Pointers, Binary Search +3 |
| 713 | Subarray Product Less Than K | Medium | Array, Binary Search, Prefix Sum +1 |
| 718 | Maximum Length of Repeated Subarray | Medium | Array, Binary Search, Dynamic Programming +3 |
| 1004 | Max Consecutive Ones III | Medium | Array, Binary Search, Prefix Sum +1 |
| 1456 | Maximum Number of Vowels in a Substring of Given Length | Medium | String, Sliding Window |
| 1493 | Longest Subarray of 1's After Deleting One Element | Medium | Array, Dynamic Programming, Sliding Window |
| 159 | Longest Substring with At Most Two Distinct CharactersPremium | Medium | Hash Table, String, Sliding Window |
| 340 | Longest Substring with At Most K Distinct CharactersPremium | Medium | Hash Table, String, Sliding Window |
| 487 | Max Consecutive Ones IIPremium | Medium | Array, Dynamic Programming, Sliding Window |
| 837 | New 21 Game | Medium | Math, Dynamic Programming, Sliding Window +1 |
| 904 | Fruit Into Baskets | Medium | Array, Hash Table, Sliding Window |
| 930 | Binary Subarrays With Sum | Medium | Array, Hash Table, Prefix Sum +1 |
| 978 | Longest Turbulent Subarray | Medium | Array, Dynamic Programming, Sliding Window |
| 1016 | Binary String With Substrings Representing 1 To N | Medium | Bit Manipulation, Hash Table, String +1 |
| 1031 | Maximum Sum of Two Non-Overlapping Subarrays | Medium | Array, Dynamic Programming, Sliding Window |
| 1040 | Moving Stones Until Consecutive II | Medium | Array, Math, Sorting +1 |
| 1052 | Grumpy Bookstore Owner | Medium | Array, Sliding Window |
| 1100 | Find K-Length Substrings With No Repeated CharactersPremium | Medium | Hash Table, String, Sliding Window |
| 1151 | Minimum Swaps to Group All 1's TogetherPremium | Medium | Array, Sliding Window |
| 1156 | Swap For Longest Repeated Character Substring | Medium | Hash Table, String, Sliding Window |
| 1208 | Get Equal Substrings Within Budget | Medium | String, Binary Search, Prefix Sum +1 |
| 1234 | Replace the Substring for Balanced String | Medium | String, Sliding Window |
| 1248 | Count Number of Nice Subarrays | Medium | Array, Hash Table, Math +2 |
| 1297 | Maximum Number of Occurrences of a Substring | Medium | Hash Table, String, Sliding Window |
| 1343 | Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold | Medium | Array, Sliding Window |
| 1358 | Number of Substrings Containing All Three Characters | Medium | Hash Table, String, Sliding Window |
| 1423 | Maximum Points You Can Obtain from Cards | Medium | Array, Prefix Sum, Sliding Window |
| 1438 | Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit | Medium | Queue, Array, Ordered Set +3 |
| 1477 | Find Two Non-overlapping Sub-arrays Each With Target Sum | Medium | Array, Hash Table, Binary Search +2 |
| 1658 | Minimum Operations to Reduce X to Zero | Medium | Array, Hash Table, Binary Search +2 |
| 1695 | Maximum Erasure Value | Medium | Array, Hash Table, Sliding Window |
| 1838 | Frequency of the Most Frequent Element | Medium | Greedy, Array, Binary Search +3 |
| 1839 | Longest Substring Of All Vowels in Order | Medium | String, Sliding Window |
| 1852 | Distinct Numbers in Each SubarrayPremium | Medium | Array, Hash Table, Sliding Window |
| 1871 | Jump Game VII | Medium | String, Dynamic Programming, Prefix Sum +1 |
| 1888 | Minimum Number of Flips to Make the Binary String Alternating | Medium | String, Dynamic Programming, Sliding Window |
| 1918 | Kth Smallest Subarray SumPremium | Medium | Array, Binary Search, Sliding Window |
| 2024 | Maximize the Confusion of an Exam | Medium | String, Binary Search, Prefix Sum +1 |
| 2067 | Number of Equal Count SubstringsPremium | Medium | Hash Table, String, Counting +1 |
| 2090 | K Radius Subarray Averages | Medium | Array, Sliding Window |
| 2107 | Number of Unique Flavors After Sharing K CandiesPremium | Medium | Array, Hash Table, Sliding Window |
| 2110 | Number of Smooth Descent Periods of a Stock | Medium | Array, Math, Two Pointers +2 |
| 2134 | Minimum Swaps to Group All 1's Together II | Medium | Array, Sliding Window |
| 2260 | Minimum Consecutive Cards to Pick Up | Medium | Array, Hash Table, Sliding Window |
| 2271 | Maximum White Tiles Covered by a Carpet | Medium | Greedy, Array, Binary Search +3 |
| 2401 | Longest Nice Subarray | Medium | Bit Manipulation, Array, Sliding Window |
| 2411 | Smallest Subarrays With Maximum Bitwise OR | Medium | Bit Manipulation, Array, Binary Search +1 |
| 2461 | Maximum Sum of Distinct Subarrays With Length K | Medium | Array, Hash Table, Sliding Window |
| 2516 | Take K of Each Character From Left and Right | Medium | Hash Table, String, Sliding Window |
| 2537 | Count the Number of Good Subarrays | Medium | Array, Hash Table, Sliding Window |
| 2555 | Maximize Win From Two Segments | Medium | Array, Binary Search, Sliding Window |
| 2653 | Sliding Subarray Beauty | Medium | Array, Hash Table, Sliding Window |
| 2730 | Find the Longest Semi-Repetitive Substring | Medium | String, Sliding Window |
| 2743 | Count Substrings Without Repeating CharacterPremium | Medium | Hash Table, String, Sliding Window |
| 2747 | Count Zero Request Servers | Medium | Array, Hash Table, Sorting +1 |
| 2762 | Continuous Subarrays | Medium | Queue, Array, Ordered Set +3 |
| 2779 | Maximum Beauty of an Array After Applying Operation | Medium | Array, Binary Search, Sorting +1 |
| 2799 | Count Complete Subarrays in an Array | Medium | Array, Hash Table, Sliding Window |
| 2831 | Find the Longest Equal Subarray | Medium | Array, Hash Table, Binary Search +1 |
| 2841 | Maximum Sum of Almost Unique Subarray | Medium | Array, Hash Table, Sliding Window |
| 2875 | Minimum Size Subarray in Infinite Array | Medium | Array, Hash Table, Prefix Sum +1 |
| 2904 | Shortest and Lexicographically Smallest Beautiful String | Medium | String, Sliding Window |
| 2958 | Length of Longest Subarray With at Most K Frequency | Medium | Array, Hash Table, Sliding Window |
| 2962 | Count Subarrays Where Max Element Appears at Least K Times | Medium | Array, Sliding Window |
| 2981 | Find Longest Special Substring That Occurs Thrice I | Medium | Hash Table, String, Binary Search +2 |
| 2982 | Find Longest Special Substring That Occurs Thrice II | Medium | Hash Table, String, Binary Search +2 |
Hard (30)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 30 | Substring with Concatenation of All Words | Hard | Hash Table, String, Sliding Window |
| 76 | Minimum Window Substring | Hard | Hash Table, String, Sliding Window |
| 220 | Contains Duplicate III | Hard | Array, Bucket Sort, Ordered Set +2 |
| 239 | Sliding Window Maximum | Hard | Queue, Array, Sliding Window +2 |
| 480 | Sliding Window Median | Hard | Array, Hash Table, Sliding Window +1 |
| 632 | Smallest Range Covering Elements from K Lists | Hard | Greedy, Array, Hash Table +3 |
| 689 | Maximum Sum of 3 Non-Overlapping Subarrays | Hard | Array, Dynamic Programming, Prefix Sum +1 |
| 683 | K Empty SlotsPremium | Hard | Binary Indexed Tree, Segment Tree, Queue +5 |
| 727 | Minimum Window SubsequencePremium | Hard | String, Dynamic Programming, Sliding Window |
| 862 | Shortest Subarray with Sum at Least K | Hard | Queue, Array, Binary Search +4 |
| 992 | Subarrays with K Different Integers | Hard | Array, Hash Table, Counting +1 |
| 995 | Minimum Number of K Consecutive Bit Flips | Hard | Bit Manipulation, Queue, Array +2 |
| 1044 | Longest Duplicate Substring | Hard | String, Binary Search, Suffix Array +3 |
| 1425 | Constrained Subsequence Sum | Hard | Queue, Array, Dynamic Programming +3 |
| 1499 | Max Value of Equation | Hard | Queue, Array, Sliding Window +2 |
| 1610 | Maximum Number of Visible Points | Hard | Geometry, Array, Math +2 |
| 1703 | Minimum Adjacent Swaps for K Consecutive Ones | Hard | Greedy, Array, Prefix Sum +1 |
| 2009 | Minimum Number of Operations to Make Array Continuous | Hard | Array, Hash Table, Binary Search +1 |
| 2106 | Maximum Fruits Harvested After at Most K Steps | Hard | Array, Binary Search, Prefix Sum +1 |
| 2156 | Find Substring With Given Hash Value | Hard | String, Sliding Window, Hash Function +1 |
| 2302 | Count Subarrays With Score Less Than K | Hard | Array, Binary Search, Prefix Sum +1 |
| 2398 | Maximum Number of Robots Within Budget | Hard | Queue, Array, Binary Search +4 |
| 2444 | Count Subarrays With Fixed Bounds | Hard | Queue, Array, Sliding Window +1 |
| 2524 | Maximum Frequency Score of a SubarrayPremium | Hard | Stack, Array, Hash Table +2 |
| 2528 | Maximize the Minimum Powered City | Hard | Greedy, Queue, Array +3 |
| 2781 | Length of the Longest Valid Substring | Hard | Array, Hash Table, String +1 |
| 2902 | Count of Sub-Multisets With Bounded Sum | Hard | Array, Hash Table, Dynamic Programming +1 |
| 2935 | Maximum Strong Pair XOR II | Hard | Bit Manipulation, Trie, Array +2 |
| 2953 | Count Complete Substrings | Hard | Hash Table, String, Sliding Window |
| 2968 | Apply Operations to Maximize Frequency Score | Hard | Array, Binary Search, Prefix Sum +2 |
Related patterns
Problems sit in more than one pattern more often than not, and the overlap is where the interesting follow-up questions live.
Sliding Window pattern FAQ
What is the sliding window pattern?
A sliding window holds one contiguous stretch of the input between two indices, along with a running summary of what is inside it — a sum, a character count, a distinct-element count.
How many LeetCode problems use the sliding window pattern?
This page lists 116 LeetCode problems that the sliding window pattern applies to: 12 Easy, 74 Medium and 30 Hard. 102 of them carry a complete Python solution with complexity analysis.
What is the time complexity of the sliding window pattern?
O(n) time and O(1) to O(k) space. Each index enters the window once and leaves it at most once, so the two pointers together travel at most 2n steps no matter how long any individual window gets — that is what makes a single pass equivalent to the O(n²) scan over every subarray. The space is whatever the running summary costs: O(1) for a sum or a counter, O(k) for a map of the elements currently inside the window, bounded by the alphabet rather than by the input length.
When should I use the sliding window pattern in an interview?
The answer is a contiguous subarray or substring — not a subsequence, which cannot be described by two indices. You can update the window's summary in O(1) when an element enters or leaves it.
Which sliding window problem should I start with?
LeetCode 219. Contains Duplicate II is the lowest-numbered Easy problem on this page, which makes it the usual starting point: the technique is visible without the problem's own complications getting in the way.
What patterns are related to sliding window?
Two Pointers, Prefix Sum, Hash Map, Monotonic Stack. Problems frequently sit in more than one of these, and the overlap is where the interesting follow-up questions come from.
More ways in: all 22 patterns, the curated study lists, or the full problem list.
Meet the sliding window problem you did not practise
Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.