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 TypeScript code that finds the maximum in each sliding window of size 3 using a deque?
DSA Typescript
function maxSlidingWindow(nums: number[], k: number): number[] {
const deque: number[] = [];
const result: number[] = [];
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 arrives?
Attempts:
2 left
💡 Hint
Think about how the deque helps quickly find the maximum in the current window.
✗ Incorrect
Removing smaller or equal elements from the back ensures that the deque stores indices of elements in decreasing order of their values. This way, the front always holds the index of the maximum element.
❓ Predict Output
advancedOutput of Sliding Window Maximum with Negative and Repeated Numbers
What is the output of this TypeScript code for sliding window maximum with window size 4?
DSA Typescript
function maxSlidingWindow(nums: number[], k: number): number[] {
const deque: number[] = [];
const result: number[] = [];
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([4, -2, -2, 5, 6, 5, 1, 7], 4));Attempts:
2 left
💡 Hint
Trace the deque updates carefully for each window.
✗ Incorrect
The maximums for windows of size 4 are: [4, -2, -2, 5], max is 5; [ -2, -2, 5, 6], max is 6; [ -2, 5, 6, 5], max is 6; [5, 6, 5, 1], max is 7; [6, 5, 1, 7], max is 7.
🔧 Debug
advancedIdentify the Error in Sliding Window Maximum Implementation
What error will this TypeScript code produce when run, and why?
DSA Typescript
function maxSlidingWindow(nums: number[], k: number): number[] {
const deque: number[] = [];
const result: number[] = [];
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,1,3,4,6,3,8,9,10,12,56], 4));Attempts:
2 left
💡 Hint
Check the condition that removes indices out of the current window.
✗ Incorrect
The condition 'deque[0] <= i - k' incorrectly removes indices that are still in the window. It should be 'deque[0] === i - k' to remove only the index that just moved out of the window.
🚀 Application
expertNumber of Maximum Elements Stored in Deque After Processing
After running the sliding window maximum algorithm with a deque on the array [10, 9, 8, 7, 6, 5, 4] with window size 3, how many elements remain in the deque at the end of the process?
Attempts:
2 left
💡 Hint
Consider how the deque stores indices of elements in decreasing order and removes smaller elements.
✗ Incorrect
Since the array is strictly decreasing, each new element is smaller than the previous, so no elements are removed from the back. The deque holds indices of the last k elements. But as the window slides, indices out of the window are removed from the front. At the end, only the index of the last element remains.
