When solving problems involving arrays or strings, beginners often reach for nested loops. While this works, it quickly becomes inefficient for large inputs. This is where the Sliding Window Technique comes in.

Sliding Window is one of the most important optimization techniques in Data Structures and Algorithms. It helps reduce time complexity from quadratic to linear by reusing previous computations instead of starting from scratch every time.

Once you understand this technique, a whole category of interview problems becomes much easier.

What Is the Sliding Window Technique?

The Sliding Window Technique is an approach where we process a continuous part of an array or string using a window that moves step by step across the data.

Instead of recalculating values for every possible subarray or substring, we adjust the window by

Adding new elements when the window expands

Removing old elements when the window slides forward

This makes the solution efficient and clean.

Why Sliding Window Is Needed

Consider problems that ask for

Maximum or minimum subarray

Longest or shortest substring

Continuous ranges

A brute-force solution usually involves checking every possible window, which leads to O(n²) time complexity.
Sliding Window solves the same problems in O(n) time by processing each element only once or twice.

Types of Sliding Window

There are two major types of sliding window problems.

Fixed Size Sliding Window

The window size is constant and does not change.

Variable Size Sliding Window

The window size changes based on a condition.

Let’s understand both with examples.

Fixed Size Sliding Window Example

Problem

Find the maximum sum of a subarray of size k.

Input

Array: [2, 1, 5, 1, 3, 2]

k = 3

Expected Output

9

Explanation

The possible subarrays of size 3 are

[2, 1, 5] → sum = 8
[1, 5, 1] → sum = 7
[5, 1, 3] → sum = 9
[1, 3, 2] → sum = 6

The maximum sum is 9.

Optimized Sliding Window Approach

Instead of calculating the sum for each subarray from scratch

We calculate the sum of the first window

Then slide the window by removing the first element and adding the next element

JavaScript Code

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;
}
Dry Run (Fixed Window)

Array: [2, 1, 5, 1, 3, 2]

k = 3

Start

windowSum = 0
i = 0
Add 2 → windowSum = 2
i = 1
Add 1 → windowSum = 3
i = 2
Add 5 → windowSum = 8
First window complete → maxSum = 8
Remove 2 → windowSum = 6
i = 3
Add 1 → windowSum = 7

maxSum stays 8

Remove 1 → windowSum = 6
i = 4
Add 3 → windowSum = 9
Update maxSum = 9
Remove 5 → windowSum = 4
i = 5
Add 2 → windowSum = 6

maxSum remains 9

Final Answer: 9

Variable Size Sliding Window Example

Problem

Find the length of the longest substring without repeating characters.

Input

"abcabcbb"

Expected Output

3

Explanation

The longest substring without repeating characters is "abc".

Approach

We use two pointers

One pointer expands the window

One pointer shrinks the window when a condition is violated

We maintain a set to ensure all characters inside the window are unique.

JavaScript Code

function lengthOfLongestSubstring(s) {
let left = 0;
let maxLength = 0;
const set = new Set();
for (let right = 0; right < s.length; right++) {
while (set.has(s[right])) {
set.delete(s[left]);
left++;
}
set.add(s[right]);
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
}
Dry Run (Variable Window)

Input: "abcabcbb"

Start

left = 0, set = {}
right = 0 → 'a'
Window = "a", maxLength = 1
right = 1 → 'b'
Window = "ab", maxLength = 2
right = 2 → 'c'
Window = "abc", maxLength = 3
right = 3 → 'a'

Duplicate found

Shrink window until 'a' is removed

Window becomes "bca"

Continue until end

Final maxLength = 3

Time and Space Complexity

Time Complexity

O(n) because each element is processed at most twice.

Space Complexity

O(n) in the worst case for the set or hash map.

When Should You Use Sliding Window?

Use sliding window when

The problem involves continuous subarrays or substrings

You want to optimize a brute-force approach

You need maximum or minimum values in a range

Final Thoughts

Sliding Window is not just a technique, it is a mindset. Once you learn how to grow and shrink a window correctly, many complex-looking problems become straightforward.

This technique is a must for coding interviews and real-world problem solving.

DWP | DSA with Piyush

Learn DSA with clarity, logic, and interview focus.