Challenge - 5 Problems
Sliding Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Sliding Window Maximum for a Simple Array
What is the output of the following JavaScript code that finds the maximum in each sliding window of size 3 using a deque?
DSA Javascript
function maxSlidingWindow(nums, k) {
const deque = [];
const result = [];
for (let i = 0; i < nums.length; i++) {
while (deque.length && nums[deque[deque.length - 1]] <= nums[i]) {
deque.pop();
}
deque.push(i);
if (deque[0] === i - k) {
deque.shift();
}
if (i >= k - 1) {
result.push(nums[deque[0]]);
}
}
return result;
}
console.log(maxSlidingWindow([1,3,-1,-3,5,3,6,7], 3));Attempts:
2 left
💡 Hint
Focus on how the deque stores indices of elements in decreasing order and removes indices out of the current window.
✗ Incorrect
The deque keeps indices of elements in decreasing order of their values. For each window, the front of the deque is the index of the maximum element. The output array collects these maximums for each sliding window.
🧠 Conceptual
intermediateUnderstanding Deque Behavior in Sliding Window Maximum
In the sliding window maximum algorithm using a deque, what is the main reason for removing elements from the back of the deque when a new element is processed?
Attempts:
2 left
💡 Hint
Think about why smaller elements behind a bigger element are useless for maximum queries.
✗ Incorrect
Smaller elements behind a bigger element cannot be maximum in the current or any future window that includes the bigger element, so they are removed to keep the deque efficient.
🔧 Debug
advancedIdentify the Bug in Sliding Window Maximum Implementation
What error will the following code produce when run, and why?
DSA Javascript
function maxSlidingWindow(nums, k) {
const deque = [];
const result = [];
for (let i = 0; i < nums.length; i++) {
while (deque.length && nums[deque[deque.length - 1]] < nums[i]) {
deque.pop();
}
deque.push(i);
if (deque[0] <= i - k) {
deque.shift();
}
if (i >= k - 1) {
result.push(nums[deque[0]]);
}
}
return result;
}
console.log(maxSlidingWindow([1,3,-1,-3,5,3,6,7], 3));Attempts:
2 left
💡 Hint
Check the condition that removes indices out of the current window.
✗ Incorrect
The condition 'deque[0] <= i - k' removes indices that are not strictly out of the window. It should be 'deque[0] === i - k' to remove exactly the index that is out of the window. Using '<=' can remove valid indices prematurely, causing wrong output.
❓ Predict Output
advancedOutput of Sliding Window Maximum with All Equal Elements
What is the output of the following code when all elements in the array are equal?
DSA Javascript
function maxSlidingWindow(nums, k) {
const deque = [];
const result = [];
for (let i = 0; i < nums.length; i++) {
while (deque.length && nums[deque[deque.length - 1]] <= nums[i]) {
deque.pop();
}
deque.push(i);
if (deque[0] === i - k) {
deque.shift();
}
if (i >= k - 1) {
result.push(nums[deque[0]]);
}
}
return result;
}
console.log(maxSlidingWindow([2, 2, 2, 2, 2], 2));Attempts:
2 left
💡 Hint
Consider how many windows of size 2 exist in an array of length 5.
✗ Incorrect
For an array of length 5 and window size 2, there are 4 windows. Since all elements are equal, the maximum in each window is 2, so the output is [2, 2, 2, 2].
🧠 Conceptual
expertTime Complexity of Sliding Window Maximum Using Deque
What is the time complexity of the sliding window maximum algorithm using a deque for an input array of length n and window size k, and why?
Attempts:
2 left
💡 Hint
Think about how many times each element can be added or removed from the deque.
✗ Incorrect
Each element is added to the deque once and removed once, so total operations are proportional to n, making the time complexity O(n).
