Mastering the Sliding Window: Top 50 Problems Tiered & Explained
Sliding window is a powerful technique for optimizing contiguous subarray or substring challenges. This guide presents 50 curated problems—built from trusted sources each with a descriptive summary to highlight what’s being asked and how to think about it.
Easy (Low Complexity) – Foundation Building
-
Maximum Sum Subarray of Size K
Compute the max sum over all subarrays of size k. -
Smallest Window Containing 0, 1, and 2
Find shortest subarray that includes all three specified numbers. -
Check if Permutation of Pattern Exists as Substring
Determine whether any substring is a permutation of a target pattern. -
Count Strictly Increasing Subarrays
Count all contiguous subarrays where elements strictly increase. -
Remove Consecutive Duplicates
Collapse consecutive duplicates in a string. -
Max Sum Subarray ≤ X
Find subarray with max sum that does not exceed X.
Medium (Balanced Complexity) – Broadening Your Window Skills
-
Longest Substring Without Repeating Characters
Length of the longest substring where all characters are distinct. -
Substrings with K Distinct Characters
Count substrings containing exactly K unique characters. -
Fruit-Into-Baskets (Max of Two Types)
Longest subarray containing at most two distinct types. -
Substrings of Length K with k-1 Distinct Elements
Count substrings that are missing just one distinct value. -
Minimum Removals to Meet Target Sum
Remove as few elements as possible to ensure sum ≤ target. -
Longest Repeating Character Replacement
Maximize a repeated character substring by replacing up to k. -
Binary Subarray with Sum = K
Find subarray in binary array whose sum equals K. -
Subarrays Product Less Than K
Count contiguous subarrays with product < k. -
Count Anagrams in String
Total substrings that are anagrams of a given pattern. -
Max Sum Subarray ≥ K
Maximize sum with a constraint on minimum length. -
Count Distinct in Every Window of Size K
For each window, count unique elements. -
Subarray With Given Sum (Positive Ints)
Locate exact sum in positive-integer arrays. -
First Negative in Every Window Size K
For each window, report the first negative value. -
Longest Subarray Sum = K
Longest contiguous subarray summing exactly to K. -
Smallest Window Containing Characters from Another String
Shortest substring containing all target string characters. -
Count Equivalent Subarrays
Count windows that share the same multiset or signature.
Hard (Advanced Patterns) – Peak Sliding Window Challenges
-
Maximum of Minimums for Every Window Size
For each window size, find the maximum among all window minimums. -
Longest Substring with Exactly K Unique Characters
Length of longest substring with exactly K distinct chars. -
Minimum Window Substring
Identify the shortest substring covering all characters of another string. -
Largest Sum Subarray ≥ K
Maximize sum while ensuring window length ≥ k. -
Sliding Window Maximum
For each size-k window, find the maximum. -
Permutation in String
Check if s2 contains a permutation of s1. -
Max Consecutive Ones III
With up to k flips, compute the longest segment of 1s. -
Minimum Operations to Reduce X to Zero
Remove elements from either end to bring sum to X optimally. -
Count Subarrays with Exactly K Distinct Integers
Calculate subarrays with exactly K different values. -
Replace Substring to Balance
Minimal window size to replace so the string becomes balanced (e.g., “QWER”). -
Longest Continuous Subarray with |max – min| ≤ Limit
Find longest window with value differences within limit. -
Minimum Consecutive Cards to Pick Up
Shortest subarray with duplicate values. -
Count Nice Subarrays (K Odd Numbers)
Count subarrays containing exactly k odd numbers. -
Get Equal Substrings Within Budget
Count substrings within cost constraints to match target. -
Longest Nice Subarray
Longest subarray with bitwise AND of all pairs = 0. -
Grumpy Bookstore Owner
Maximize satisfied customers by applying a special technique on a window. -
Minimum Swaps to Group All 1s Together II
Circular array: minimize swaps to group all ones. -
Maximize Exam Confusion
Flip ≤ k answers to maximize same-character substring. -
Frequency of Most Frequent Element
Longest subarray where value + k allows maximum repetition. -
Count Subarrays Where Max Appears ≥ K Times
Count windows where a specific element appears frequently. -
Count Continuous Subarrays |max – min| ≤ Threshold
Count subarrays where the range of values stays bounded.
44–50: Additional Challenging Sliding Window Variants (New Additions)
-
Fixed-Length Window via Binary Search on Length
Binary search on window size + sliding window check for constraint validity. -
Longest Subarray Where Sum of Elements is Divisible by K
Sliding window + modular arithmetic + hashing. -
Maximum MEX (Minimum Excludant) in All Subarrays of Size K
Compute MEX (smallest missing non-negative integer). -
Dynamic Window Searching for Max Toys Within Budget
Sliding window + prefix sums + binary search to compute max number of toys purchasable. -
Longest K-Unique-Character Substring (Hard Variant)
Longest substring with at most k unique characters. -
Subarrays with Sum = K in Binary Array
Sliding window optimized for binary arrays. -
Combine Fixed & Variable Sliding Window via Two-pointer + Hash
Solve hybrid constraint where window must satisfy both fixed-length and content-based constraints.
Why This Tiered, Detailed Guide Works
-
Progressive Learning:
Start with problem basics and advance to nuanced, high-constraint variants. -
Clarity in Description:
Each problem summary reveals exact challenge and hinted technique, enabling quick pattern recognition. -
Source-Validated:
Problems and variants drawn from GFG and LeetCode community collections, including combination approaches like sliding window + binary search, MEX, and modular arithmetic.
Recommended Strategy to Prepare
- Start with Easy problems – build pattern fluency.
- Tackle Medium problems – refine adaptive thinking and constraint tracking.
- Challenge yourself with Hard-level + variants (44–50) – implement and time-box solutions.
Track progress:
Note Problem | Tier | Solved? | Key Insight in a spreadsheet.
=> Revisit with spaced practice – repetition is key.
