Introduction

The Sliding Window Technique is one of the most important concepts in DSA interviews, especially for array and string problems. Many learners struggle with this topic because they try to solve it using brute force.

In simple words, sliding window is about looking at a small part of data at a time and moving it step by step, instead of checking everything again and again.

Real-World Meaning of Sliding Window

Think of a train window:

This is exactly how the sliding window works.

What is Sliding Window Technique?

The Sliding Window Technique is used when:

Instead of recalculating from scratch, you:

Why Brute Force is Not a Good Idea

Brute Force Approach

Problems

Interviewers expect an optimized sliding window solution.

Why Deque is Used in Sliding Window Problems

Deque helps because:

Deque allows us to maintain only useful elements inside the window.

Most Common Sliding Window Problem

Problem Statement

Given an array and a window size k, find the maximum element in every window of size k.

Example

Input:  [1, 3, -1, -3, 5, 3, 6, 7]
Window Size (k): 3
Output: [3, 3, 5, 5, 6, 7]

Before vs After Understanding

Without Sliding Window

With Sliding Window + Deque

What Interviewers Are Actually Testing

Interviewers want to see:

Key Idea (Very Simple)

We store indexes in deque, not values.

The deque always stores indexes in such a way that:

Step-by-Step Logic

  1. Create an empty deque

  2. Loop through the array

  3. Remove indexes from front if they are out of the window

  4. Remove smaller elements from the back

  5. Add current index to deque

  6. Once window size is reached, record the front element

Dry Run Example

Array: [1, 3, -1, -3, 5, 3, 6, 7], k = 3

IndexValueDeque (indexes)Output
01[0]-
13[1]-
2-1[1,2]3
3-3[1,2,3]3
45[4]5
53[4,5]5
66[6]6
77[7]7

One-Line Logic Before Code

Keep only useful indexes in deque so the front always gives the maximum.

Code Implementation (Concept Focused)

C++ Code

vector<int> maxSlidingWindow(vector<int>& nums, int k) {
    deque<int> dq;
    vector<int> result;

    for (int i = 0; i < nums.size(); i++) {
        if (!dq.empty() && dq.front() == i - k)
            dq.pop_front();

        while (!dq.empty() && nums[dq.back()] < nums[i])
            dq.pop_back();

        dq.push_back(i);

        if (i >= k - 1)
            result.push_back(nums[dq.front()]);
    }
    return result;
}

Common Beginner Mistakes

Where Sliding Window Using Deque Is Used

Time and Space Complexity

Easy Summary (Explain Like I’m 10)

Imagine you are looking through a moving window and always want to know the biggest thing you see. Instead of checking everything again, you remember only the important things. Deque helps you remember them in the right order so you always know the biggest one quickly.

Summary

The Sliding Window Technique using Deque is a powerful optimization method used to solve problems involving fixed-size subarrays efficiently. By maintaining only useful elements inside a deque and sliding the window step by step, you can reduce time complexity from brute force to linear time. Mastering this technique is essential for cracking array and interview problems that require speed and smart data structure usage.