Discover how a simple queue can speed up finding the hottest days in your data!
Why Sliding Window Maximum Using Deque in DSA Javascript?
Imagine you have a long list of daily temperatures, and you want to find the hottest day in every 3-day period. Doing this by checking each 3-day group one by one feels like repeating the same work over and over.
Manually checking each group means looking at many numbers again and again. This takes a lot of time and can easily cause mistakes, especially if the list is very long.
Using a sliding window with a deque helps keep track of the hottest days efficiently. It remembers only the important days and quickly updates as the window moves, saving time and effort.
function maxSlidingWindow(nums, k) {
let result = [];
for (let i = 0; i <= nums.length - k; i++) {
let max = Math.max(...nums.slice(i, i + k));
result.push(max);
}
return result;
}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;
}This method lets you find maximum values in moving windows quickly, even for very large lists.
Stock traders use this to find the highest stock price in the last 10 days as new prices come in every day.
Manual checking repeats work and is slow.
Deque keeps track of important elements efficiently.
Sliding window maximum helps analyze data streams fast.
Practice
Solution
Step 1: Understand the role of deque in the algorithm
The deque stores indices of elements in the current window, maintaining them in decreasing order of their values to quickly find the maximum.Step 2: Why other options are incorrect
To store all elements of the array for quick access is incorrect because deque does not store all elements, only relevant indices. To sort the entire array before processing and D do not relate to the sliding window maximum logic.Final Answer:
To keep track of indices of elements in the current window in decreasing order of their values -> Option CQuick Check:
Deque stores indices in decreasing order for max [OK]
- Thinking deque stores actual values instead of indices
- Assuming deque sorts the entire array
- Confusing deque with a stack or queue without order
Solution
Step 1: Identify JavaScript data structure for deque
JavaScript does not have a built-in Deque class, so an empty array is used to simulate a deque.Step 2: Why other options are invalid
Options A, B, and D refer to classes not available in standard JavaScript.Final Answer:
const deque = []; -> Option DQuick Check:
Use array as deque in JS [OK]
- Trying to use non-existent Deque class
- Using Queue which lacks double-ended operations
- Confusing JavaScript with other languages' syntax
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));Solution
Step 1: Trace the sliding window maximum for each window
Windows of size 3 and their max values are: [1,3,-1] -> 3, [3,-1,-3] -> 3, [-1,-3,5] -> 5, [-3,5,3] -> 5, [5,3,6] -> 6, [3,6,7] -> 7.Step 2: Confirm the result array matches the expected output
The result array is [3, 3, 5, 5, 6, 7], matching [3, 3, 5, 5, 6, 7].Final Answer:
[3, 3, 5, 5, 6, 7] -> Option BQuick Check:
Sliding window max = [3,3,5,5,6,7] [OK]
- Not removing indices outside the window
- Pushing values instead of indices into deque
- Incorrectly updating the result array
function maxSlidingWindow(nums, k) {
const deque = [];
const result = [];
for (let i = 0; i < nums.length; i++) {
while (deque.length && nums[deque[0]] <= nums[i]) {
deque.shift();
}
deque.push(i);
if (deque[0] <= i - k) {
deque.shift();
}
if (i >= k - 1) {
result.push(nums[deque[0]]);
}
}
return result;
}Solution
Step 1: Analyze the while loop condition
The while loop incorrectly compares nums[deque[0]] (front) with nums[i], but it should compare with nums[deque[deque.length - 1]] (back) to maintain decreasing order.Step 2: Confirm other parts are correct
The deque stores indices correctly, the out-of-window removal condition exists, and the result is returned properly.Final Answer:
The condition in the while loop should compare with the last element, not the first -> Option AQuick Check:
Compare with deque's last element in while loop [OK]
- Comparing with front instead of back in deque
- Storing values instead of indices in deque
- Forgetting to remove indices outside the window
nums = [4, 2, 12, 3, 8, 7, 9] and window size k = 4, what will be the output of the Sliding Window Maximum algorithm using a deque?Solution
Step 1: Identify each window and its maximum
Windows of size 4 are: [4,2,12,3] max=12, [2,12,3,8] max=12, [12,3,8,7] max=12, [3,8,7,9] max=9.Step 2: Collect the maximums in order
The result array is [12, 12, 12, 9], matching [12, 12, 12, 9].Final Answer:
[12, 12, 12, 9] -> Option AQuick Check:
Max per window = [12,12,12,9] [OK]
- Not sliding window correctly by one index
- Forgetting to remove old indices from deque
- Mixing values and indices in deque
Ready to prove it?
Pass the test and this pattern moves unknown
