The Sliding Window Algorithm is a sub-array and sub-string optimization technique. Instead of recalculating contiguous segments from scratch using nested loops ($O(n^2)$), the algorithm maintains a moving "window" that slides across the sequence, updating the internal state incrementally in $O(n)$ linear time.
1. Real-World Analogy: The Train Window
Imagine you are riding a train through the countryside, looking out through a small rectangular window:
As the train moves forward 1 meter, you do not need to rediscover the entire landscape.
One tree enters your field of view from the right, and one tree leaves your field of view from the left.
The remaining scenery inside the window stays identical! This is the essence of sliding window: Add incoming element on the right, subtract outgoing element on the left.
2. The Two Window Paradigms
Type 1: Fixed-Size Window ($K$)
The size of the window is strictly constant (e.g. "Find maximum sum of any contiguous subarray of length $K$").
Advance right pointer until window size equals $K$.
At each subsequent step: add arr[right], subtract arr[left], and increment both pointers (left++, right++).
Type 2: Dynamic (Variable-Size) Window
The window size expands and shrinks dynamically to satisfy a constraint (e.g. "Find the longest substring without repeating characters", "Smallest subarray with sum $\ge S$").
Expand right pointer to include elements and update state.
While the window constraint is violated, shrink the window from the left (left++) until valid again.
Record the maximum/minimum window length at valid states.
3. Fixed Window Implementation: Max Subarray Sum of Size $K$
10for(let right = k; right < nums.length; right++){
11 windowSum += nums[right]- nums[right - k];
12 maxSum = Math.max(maxSum, windowSum);
13}
14
15return maxSum;
16}
4. Dynamic Window: Longest Substring Without Repeating Characters
Given a string s, find the length of the longest substring without duplicate characters:
Python 3:
1deflength_of_longest_substring(s:str)->int:
2 char_index_map ={}# Tracks last seen index of each character
3 max_len =0
4 left =0
5
6for right, char inenumerate(s):
7# If character is already in window, jump left pointer past it!
8if char in char_index_map and char_index_map[char]>= left:
9 left = char_index_map[char]+1
10
11 char_index_map[char]= right
12 max_len =max(max_len, right - left +1)
13
14return max_len
5. Trace Table: s = "abcabcbb"
right
char
left
Window Substring
Action
max_len
0
'a'
0
"a"
Map: {a:0}
1
1
'b'
0
"ab"
Map: {a:0, b:1}
2
2
'c'
0
"abc"
Map: {a:0, b:1, c:2}
3
3
'a'
1
"bca"
Duplicate 'a' seen at 0 → left = 0 + 1 = 1
3
4
'b'
2
"cab"
Duplicate 'b' seen at 1 → left = 1 + 1 = 2
3
5
'c'
3
"abc"
Duplicate 'c' seen at 2 → left = 2 + 1 = 3
3
6
'b'
5
"b"
Duplicate 'b' seen at 4 → left = 4 + 1 = 5
3
7
'b'
7
"b"
Duplicate 'b' seen at 6 → left = 6 + 1 = 7
3
Result: Maximum length is 3 ("abc"), achieved in single-pass $O(n)$ time!
Interactive Quiz• Knowledge Check
Multiple Choice Questions
Question 1
How does a Sliding Window reduce the time complexity of finding the maximum sum subarray of size $K$ from $O(n \times K)$ to $O(n)$?
Question 2
When solving "Longest Substring with At Most K Distinct Characters", when should the left pointer advance?
Question 3
What is the space complexity of the Sliding Window technique when applied to an array of numbers with a fixed size $K$?
Question 4
Which of the following problems is NOT suitable for the Sliding Window pattern?
Question 5
In a dynamic sliding window where both left and right pointers only move forward from 0 to $n$, why is the total time complexity $O(n)$ even if there is a while loop nested inside a for loop?
Next Lesson
Prefix Sums & Difference Arrays for Constant-Time Range Queries
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.