Sliding Window is a core optimization technique used in array and string problems.
It allows you to process continuous subarrays efficiently by reusing previous computations instead of recalculating everything from scratch.
Once this concept becomes clear, many problems that look different on the surface start following the same underlying pattern.
This technique is commonly used in coding interviews, competitive programming, and real-world stream processing systems.
Who Should Learn Sliding Window
This topic is ideal if you already understand array traversal and have basic familiarity with prefix sum concepts.
It is especially useful for developers preparing for interviews where time complexity and optimization play a major role.
What Is the Sliding Window Technique
Sliding Window is a method where a continuous range of elements, called a window, is maintained and moved across the array.
As the window moves forward
âž• the new element entering the window is added
âž– the element leaving the window is removed
This controlled movement makes calculations efficient and predictable.
A Real-Life Analogy
Consider calculating the average temperature for every three consecutive days.
When the window moves forward by one day, you remove the oldest day’s temperature and add the newest day’s temperature.
You do not recalculate the entire sum again.
This moving range is exactly how the sliding window technique works.
Types of Sliding Window Problems
Sliding window problems can be grouped into two major categories.
Fixed Size Sliding Window
In this type, the size of the window remains constant and only its position changes.
Typical use cases include finding the maximum or average of k consecutive elements.
This is the easier form of sliding window and a good starting point.
Variable Size Sliding Window
In this type, the size of the window changes dynamically based on a condition.
The window expands when the condition is not met and shrinks when the condition becomes valid.
This variation is more flexible and appears frequently in interview questions.
Fixed Size Sliding Window Example
Given the array
[2, 1, 5, 1, 3, 2]
Window size
k = 3The goal is to find the maximum sum of any subarray of size three.
Fixed Window Dry Run
Initial window
→ [2, 1, 5] → sum = 8Window slides forward
→ remove 2, add 1 → [1, 5, 1] → sum = 7
→ remove 1, add 3 → [5, 1, 3] → sum = 9
→ remove 5, add 2 → [1, 3, 2] → sum = 6The maximum sum observed is 9.
JavaScript Implementation (Fixed Window)
function maxSumSubarray(arr, k) {
let windowSum = 0;
let maxSum = 0; for (let i = 0; i < arr.length; i++) {
windowSum += arr[i]; if (i >= k - 1) {
maxSum = Math.max(maxSum, windowSum);
windowSum -= arr[i - (k - 1)];
}
} return maxSum;
}Variable Size Sliding Window Example
In variable size problems, the window grows and shrinks based on a condition.
A common example is finding the smallest subarray whose sum is greater than or equal to a given value.
Given the array
[2, 3, 1, 2, 4, 3]
The window expands until the required sum is reached and then shrinks to minimize its size.
JavaScript Implementation (Variable Window)
function minSubArrayLen(target, nums) {
let left = 0;
let sum = 0;
let minLength = Infinity; for (let right = 0; right < nums.length; right++) {
sum += nums[right]; while (sum >= target) {
minLength = Math.min(minLength, right - left + 1);
sum -= nums[left];
left++;
}
} return minLength === Infinity ? 0 : minLength;
}Time and Space Complexity
The sliding window technique typically runs in linear time.
Each element is added to and removed from the window at most once.
Only constant extra space is used.
Common Interview Patterns Using Sliding Window
>Maximum sum subarray of size k
>Longest substring without repeating characters
>Minimum window substring
>Smallest subarray with a given sum
When a problem mentions consecutive or continuous elements, sliding window should be one of your first thoughts.
When a problem mentions consecutive or continuous elements, sliding window should be one of your first thoughts.
Sliding Window vs Prefix Sum
Prefix sum works best when the array is static and multiple range queries are required.
Sliding window works best when the window needs to move continuously with optimal space usage.
Strong candidates know when to apply each technique.
What to Read Next
DSA RoadMap -
https://www.dsawithpiyush.com/post/data-structures-and-algorithm-complete-roadmap-2026
Array Traversal – Beginner to Interview Level -
https://www.dsawithpiyush.com/post/array-traversal-in-data-structure-or-ds
Prefix Sum Explained With Real Examples -
https://www.dsawithpiyush.com/post/prefix-sum-explained-with-real-examples-dsa
Kadane’s Algorithm Explained Simply -
https://www.dsawithpiyush.com/post/why-kadanes-algorithm-is-important
Final Advice from DSA With Piyush
If a problem talks about subarrays or substrings, start thinking in terms of windows.
Once this pattern clicks, many problems become straightforward.