DSA Patterns: Sliding Window

One of the most powerful tools for working with contiguous subarrays or substrings is the Sliding Window pattern. It helps reduce brute-force O(nΒ²) problems into clean and efficient O(n) solutions by reusing work already done in previous iterations.
Letβs break it down.
π When to Use Sliding Window
Use this when:
- You're dealing with subarrays or substrings
- The problem involves max/min sum, distinct characters, frequency counts, or window sizes
- You want to avoid recomputing things for each subarray
π§° Sliding Window Templates
Here are two general-purpose templates you can use during interviews:
β Fixed-Size Window (e.g., Max Sum Subarray of Size K)
Initialize windowSum = 0
Initialize maxResult = -β
Initialize l = 0
for r from 0 to end of array:
Add array[r] to windowSum
if (window size == k):
Update maxResult using windowSum
Subtract array[l] from windowSum
Move l forward
β Variable-Size Window (e.g., Longest Substring Without Repeating Characters)
Initialize data structure to track window state
Initialize l = 0
for r from 0 to end of array:
Expand window to include array[r]
while (window is invalid):
Shrink window from the left (remove array[l])
Move l forward
Update result if needed
- Use the fixed version when the window size is given.
- Use the variable version when you're sliding based on a condition like uniqueness, frequency, or constraints.
π Core Idea
Instead of recalculating from scratch, maintain a "window" over part of the array/string:
- Expand the window to include new elements
- Shrink the window when a condition is violated (or to optimize)
- Track the result at each valid state
The trick is to slide the window only forward, keeping time complexity at O(n).
π§ͺ Sample Problem 1: Longest Substring Without Repeating Characters (Medium)
Problem:
Given a string s
, find the length of the longest substring without repeating characters.
Input:
s = "abcabcbb"
Expected Output:
3 β "abc"
Why Sliding Window Works Here
- We're asked to find the longest substring, which implies a contiguous window
- The moment we see a duplicate character, the current window becomes invalid
- We can adjust the window dynamically using two pointers:
l
andr
Letβs walk through it step-by-step.
Step-by-Step ASCII Dry Run
We'll track:
l
β start of current windowr
β current charactercharSet
β current unique characters in the window
Initial State:
s = "a b c a b c b b"
β
l, r = 0
charSet = {}
Step 1:
Add 'a' β charSet = {'a'}
[ a ] β maxLen = 1
Step 2:
Move r = 1, add 'b' β charSet = {'a', 'b'}
[ a b ] β maxLen = 2
Step 3:
Move r = 2, add 'c' β charSet = {'a', 'b', 'c'}
[ a b c ] β maxLen = 3
Step 4:
Move r = 3, duplicate 'a' found
β Remove 'a', move l = 1
β Now window is [ b c a ] β charSet = {'b', 'c', 'a'}
β maxLen still 3
Step 5:
Move r = 4, duplicate 'b' found
β Remove 'b', move l = 2
β Now window is [ c a b ] β charSet = {'c', 'a', 'b'}
β maxLen still 3
Step 6:
Move r = 5, duplicate 'c' found
β Remove 'c', move l = 3
β Now window is [ a b c ] β charSet = {'a', 'b', 'c'}
β maxLen still 3
Step 7:
Move r = 6, duplicate 'b' found
β Remove 'a', move l = 4
β Remove 'b', move l = 5
β Now window = [ c b ] β charSet = {'c', 'b'}
β maxLen still 3
Step 8:
Move r = 7, duplicate 'b' found
β Remove 'c', move l = 6
β Remove 'b', move l = 7
β Now window = [ b ] β charSet = {'b'}
β maxLen still 3
Final maxLen = 3 ("abc")
β± Time & Space Complexity
- Time: O(n), each character visited at most twice
- Space: O(k), where k is number of unique chars in window (e.g., 26 for lowercase)
π§ Why This Works
This is a textbook case for sliding window:
- The "longest valid window" expands until invalid
- When a constraint is violated (duplicate found), we shrink from the left
- We never go backward β O(n) time
We turn nested loops into a single pass.
π§ͺ Sample Problem 2: Maximum Sum Subarray of Size K
π§ Intuition
In this problem, youβre given an array of integers and a number k
, and you need to find the maximum sum of any subarray of size k
.
β Brute Force Approach (Why It Fails)
- Loop over every possible subarray of size
k
- For each, compute the sum from scratch
- Time complexity: O(n Γ k) β not efficient for large arrays
β Sliding Window Insight
If we slide a fixed-size window across the array:
- We add the new incoming element
- We remove the outgoing element from the left
- And just update the running sum instead of recalculating it
This makes it O(n) time β a huge improvement.
π§ͺ Problem
Input: nums = [2, 1, 5, 1, 3, 2], k = 3
Expected Output: 9 β subarray [5, 1, 3]
Step-by-Step ASCII Dry Run (Using l
and r
)
We'll track:
l
= left of the windowr
= right of the windowwindowSum
= current running sum of the windowmaxSum
= max sum seen so far
### π§ͺ Dry Run: Max Sum Subarray of Size K
Input: nums = [2, 1, 5, 1, 3, 2], k = 3
Expected Output: 9 β subarray [5, 1, 3]
Initial State
l = 0, r = 0, windowSum = 0, maxSum = 0
Step 1: r = 0
Add 2 β windowSum = 2
Window: [2] β Not full yet
maxSum = 0
Step 2: r = 1
Add 1 β windowSum = 3
Window: [2, 1] β Not full yet
maxSum = 0
Step 3: r = 2
Add 5 β windowSum = 8
Window: [2, 1, 5] β Full window
Update maxSum = 8
Step 4: r = 3
Add 1 β windowSum = 9
Remove nums[l] = 2 β windowSum = 7
Move l = 1
Window: [1, 5, 1]
maxSum = 8
Step 5: r = 4
Add 3 β windowSum = 10
Remove nums[l] = 1 β windowSum = 9
Move l = 2
Window: [5, 1, 3]
Update maxSum = 9
Step 6: r = 5
Add 2 β windowSum = 11
Remove nums[l] = 5 β windowSum = 6
Move l = 3
Window: [1, 3, 2]
maxSum = 9
Final Answer:
maxSum = 9 β subarray [5, 1, 3]
β± Time & Space Complexity
- Time: O(n) β each element is added and removed once
- Space: O(1) β just numbers, no extra structures
β Why This Works
- We're keeping a fixed-length window over the array
Instead of recalculating from scratch, we reuse the sum by:
- Adding new item
- Subtracting the one that fell out of the window
- This gives us linear time β and is exactly what the sliding window is best at
π§ͺ Sample Problem 3: Best Time to Buy and Sell Stock
Problem:
You're given an array where each element represents the stock price on that day.
Find the maximum profit you can make by buying on one day and selling on a later day.
Input:
prices = [7, 1, 5, 3, 6, 4]
Expected Output:
5 (Buy at 1, Sell at 6)
This may not look like a sliding window problem at first, but it is β just not with a fixed window size.
Hereβs how we frame it:
- Think of the
l
pointer as the day we choose to buy (left of the window) - The
r
pointer moves forward, scanning every potential sell day - We calculate profit as
prices[r] - prices[l]
- If we ever find a cheaper buy price (i.e.,
prices[r] < prices[l]
), we updatel
- The goal is to keep updating
maxProfit
with the best opportunity so far
Initial State:
l = 0, r = 1, maxProfit = 0
Step 1: r = 1
prices[r] = 1 < prices[l] = 7
β Update l = 1 (cheaper buy day)
β maxProfit stays 0
Step 2: r = 2
prices[r] = 5 > prices[l] = 1
β Profit = 5 - 1 = 4 β Update maxProfit = 4
Step 3: r = 3
prices[r] = 3 > prices[l] = 1
β Profit = 3 - 1 = 2 β maxProfit stays 4
Step 4: r = 4
prices[r] = 6 > prices[l] = 1
β Profit = 6 - 1 = 5 β Update maxProfit = 5
Step 5: r = 5
prices[r] = 4 > prices[l] = 1
β Profit = 4 - 1 = 3 β maxProfit stays 5
Final Answer:
maxProfit = 5
β
This solution works in O(n) time with two pointers, no extra space.
We dynamically adjust our βbuy dayβ and use the current price as a βsell dayβ to check the best opportunity at each step.
β Final Summary: When to Use Sliding Window
Sliding Window is your go-to pattern when:
- You're working with contiguous ranges
- You need to optimize or satisfy a constraint
- You want to avoid recomputing from scratch
Master these:
- Longest Substring Without Repeating Characters
- Max Sum Subarray of Size K
- Best Time to Buy and Sell Stock
- Minimum Window Substring
- Longest Repeating Character Replacement
- Permutation in String
Subscribe to my newsletter
Read articles from LilSardarX directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

LilSardarX
LilSardarX
Iβm figuring things out in tech β and blogging along the way. I write what I learn, so I can revise it later and maybe help you avoid a few rabbit holes.