Interview
The sliding window technique - when to use it and common patterns
7/23/2025
193
I struggled with sliding window for the longest time until I realized there are basically 3 patterns:
Fixed size window: Easy. Just maintain window of size k.
- Max sum subarray of size k
- First negative in every window
Variable size (find maximum):
- Longest substring without repeating characters
- Longest substring with at most k distinct characters
- Max consecutive ones with at most k flips
Variable size (find minimum):
- Minimum window substring
- Smallest subarray with sum >= target
Once you categorize, the approach becomes mechanical. Anyone have additions to this?
sliding-windowpatternsdsatutorial