0
0
DSA Pythonprogramming~10 mins

Sliding Window Maximum Using Deque in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Sliding Window Maximum Using Deque
Start with empty deque
For each element in array
Remove indices from back if current element is greater
Add current index to back
Remove front if out of window
Record front element as max for current window
Move to next element
Repeat until end of array
We keep indexes of useful elements in a deque, removing smaller elements from the back and removing elements out of the window from the front, so the front always holds the max.
Execution Sample
DSA Python
from collections import deque

def max_sliding_window(nums, k):
    dq = deque()
    result = []
    for i in range(len(nums)):
        while dq and nums[dq[-1]] < nums[i]:
            dq.pop()
        dq.append(i)
        if dq[0] <= i - k:
            dq.popleft()
        if i >= k - 1:
            result.append(nums[dq[0]])
    return result
This code finds the maximum in each sliding window of size k using a deque to track indices of max candidates.
Execution Table
StepOperationCurrent Index (i)Deque Content (indices)Deque ValuesActionMax Recorded
1Start-[][]Initialize empty deque-
2Process element0[0][1]Add index 0-
3Process element1[1][3]Pop 0 (1 < 3), add 1-
4Process element2[1,2][3, -1]Add 2 (no pop)-
5Process element3[3][6]Pop 2 (-1 < 6), pop 1 (3 < 6), add 36
6Process element4[3,4][6, 4]Add 4 (no pop)6
7Process element5[3,5][6, 5]Pop 4 (4 < 5), add 56
8Process element6[6][7]Pop 5 (5 < 7), pop 3 (6 < 7), add 67
9Process element7[7][8]Pop 6 (7 < 8), add 78
10Process element8[8][9]Pop 7 (8 < 9), add 89
11End-[8][9]Finished processing all elements9
💡 All elements processed, max for each window recorded.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10Final
i-012345678-
deque (indices)[][0][1][1,2][3][3,4][3,5][6][7][8][8]
max recorded[][][][][6][6,6][6,6,6][6,6,6,7][6,6,6,7,8][6,6,6,7,8,9][6,6,6,7,8,9]
Key Moments - 3 Insights
Why do we remove elements from the back of the deque when the current element is greater?
Because smaller elements behind the current one cannot be maximum in the current or future windows, so we remove them to keep only useful candidates (see steps 3, 5, 8 in execution_table).
Why do we remove the front element if it is out of the current window?
The front holds the index of the maximum element for the current window. If it is out of the window (older than i - k), it must be removed to keep the window valid (as per the code condition if dq[0] <= i - k).
Why do we record the max only when i >= k - 1?
Because before that, the window is not full size yet, so we don't have a complete window maximum to record (see steps 2-4 where max is not recorded).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the content of the deque after processing element at index 3?
A[2, 3]
B[1, 2, 3]
C[3]
D[0, 3]
💡 Hint
Check the 'Deque Content (indices)' column at step 5 in execution_table.
At which step does the first maximum get recorded in the max recorded list?
AStep 2
BStep 5
CStep 4
DStep 3
💡 Hint
Look at the 'Max Recorded' column in execution_table to see when the first value appears.
If the window size k was 2 instead of 3, how would the max recording step change?
AMax would be recorded starting at i=1
BMax would be recorded starting at i=2
CMax would be recorded starting at i=0
DMax would be recorded starting at i=3
💡 Hint
Recall max is recorded when i >= k - 1; check variable_tracker for i values.
Concept Snapshot
Sliding Window Maximum Using Deque:
- Use deque to store indices of elements
- Remove smaller elements from back before adding new
- Remove front if out of window
- Front always max of current window
- Record max when window size reached
- Efficient O(n) solution for max in sliding windows
Full Transcript
This visualization shows how to find the maximum in each sliding window of size k in an array using a deque. We keep indices of useful elements in the deque. For each new element, we remove smaller elements from the back because they cannot be maximum anymore. We add the current index to the back. If the front index is out of the current window, we remove it. The front of the deque always holds the index of the maximum element for the current window. We record the maximum once the window is fully formed. The execution table traces each step, showing how the deque changes and when maximums are recorded. Key moments clarify why we remove elements and when we record maximums. The quiz tests understanding of deque content and max recording timing. This method runs efficiently in linear time.