Challenge - 5 Problems
Sliding Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Sliding Window Maximum for a Simple Array
What is the output of the following Python code that finds the maximum in each sliding window of size 3 using a deque?
DSA Python
from collections import deque def sliding_window_max(nums, k): dq = deque() result = [] for i in range(len(nums)): while dq and dq[0] <= i - k: dq.popleft() while dq and nums[dq[-1]] < nums[i]: dq.pop() dq.append(i) if i >= k - 1: result.append(nums[dq[0]]) return result print(sliding_window_max([1,3,-1,-3,5,3,6,7], 3))
Attempts:
2 left
💡 Hint
Remember the deque stores indices of elements in decreasing order of their values.
✗ Incorrect
The code maintains a deque of indices where the values are in decreasing order. For each window, the front of the deque is the index of the maximum element. The output is the list of these maximums for each sliding window.
🧠 Conceptual
intermediate1:30remaining
Why Use a Deque for Sliding Window Maximum?
Why is a deque the best data structure to find the maximum in a sliding window efficiently?
Attempts:
2 left
💡 Hint
Think about how the sliding window moves and how elements enter and leave the window.
✗ Incorrect
A deque supports adding and removing elements from both ends in constant time. This allows us to keep only useful elements (potential maximums) in the deque and discard others efficiently as the window slides.
🔧 Debug
advanced2:00remaining
Identify the Error in Sliding Window Maximum Implementation
What error does the following code produce when run, and why?
from collections import deque
def sliding_window_max(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 i >= k - 1:
if dq[0] <= i - k:
dq.popleft()
result.append(nums[dq[0]])
return result
print(sliding_window_max([1,3,-1,-3,5,3,6,7], 3))
Attempts:
2 left
💡 Hint
Check the order of removing indices outside the window and appending the maximum.
✗ Incorrect
The code appends the maximum before removing indices that are out of the current window, causing the maximum to sometimes be from an old window. The removal of indices outside the window should happen before appending the maximum.
❓ Predict Output
advanced1:30remaining
Output of Sliding Window Maximum with Window Size 1
What is the output of this code when the window size is 1?
DSA Python
from collections import deque def sliding_window_max(nums, k): dq = deque() result = [] for i in range(len(nums)): while dq and dq[0] <= i - k: dq.popleft() while dq and nums[dq[-1]] < nums[i]: dq.pop() dq.append(i) if i >= k - 1: result.append(nums[dq[0]]) return result print(sliding_window_max([4, 2, 12, 3, 8], 1))
Attempts:
2 left
💡 Hint
When the window size is 1, each element is its own window.
✗ Incorrect
With window size 1, the maximum in each window is the element itself, so the output is the original list.
🧠 Conceptual
expert1:30remaining
Time Complexity of Sliding Window Maximum Using Deque
What is the time complexity of finding the sliding window maximum using a deque for an array of length n and window size k, and why?
Attempts:
2 left
💡 Hint
Consider how many times each element can be pushed or popped from the deque.
✗ Incorrect
Each element is pushed into the deque once and popped at most once, so the total operations are proportional to n, making the algorithm O(n).