0
0
DSA Pythonprogramming~10 mins

Sliding Window Maximum Using Deque in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the deque for sliding window maximum.

DSA Python
from collections import deque

dq = [1]()
Drag options to blanks, or click blank then click option'
Alist
Bset
Cdeque
Ddict
Attempts:
3 left
💡 Hint
Common Mistakes
Using list instead of deque causes inefficient pops from the front.
Using set or dict is incorrect because they don't maintain order.
2fill in blank
medium

Complete the code to remove indices from the deque that are out of the current window.

DSA Python
if dq and dq[0] [1] i - k:
    dq.popleft()
Drag options to blanks, or click blank then click option'
A<=
B>=
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' instead of '<=' causes wrong removal.
Not removing old indices leads to incorrect maximum.
3fill in blank
hard

Fix the error in the loop that removes smaller elements from the deque.

DSA Python
while dq and nums[dq[-1]] [1] nums[i]:
    dq.pop()
Drag options to blanks, or click blank then click option'
A<
B>
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' or '>' causes incorrect removals.
Using '<=' keeps smaller elements which is wrong.
4fill in blank
hard

Fill both blanks to append the maximum for each window to the result list.

DSA Python
if i [1] k - 1:
    result.[2](nums[dq[0]])
Drag options to blanks, or click blank then click option'
A>=
Bappend
C<=
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>=' delays adding results.
Using 'pop' instead of 'append' removes elements incorrectly.
5fill in blank
hard

Fill all three blanks to complete the sliding window maximum function.

DSA Python
def maxSlidingWindow(nums, k):
    from collections import deque
    dq = deque()
    result = []
    for i in range(len(nums)):
        if dq and dq[0] [1] i - k:
            dq.popleft()
        while dq and nums[dq[-1]] [2] nums[i]:
            dq.pop()
        dq.append(i)
        if i [3] k - 1:
            result.append(nums[dq[0]])
    return result
Drag options to blanks, or click blank then click option'
A>=
B<
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing comparison operators causing wrong removals.
Appending results before the window is full.