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 sliding window maximum algorithm using a deque for the given array and window size?
DSA C++
std::vector<int> nums = {1, 3, -1, -3, 5, 3, 6, 7}; int k = 3; // Sliding window maximum algorithm using deque std::deque<int> dq; std::vector<int> result; for (int i = 0; i < nums.size(); ++i) { while (!dq.empty() && dq.front() <= i - k) dq.pop_front(); while (!dq.empty() && nums[dq.back()] < nums[i]) dq.pop_back(); dq.push_back(i); if (i >= k - 1) result.push_back(nums[dq.front()]); } for (int val : result) std::cout << val << " ";
Attempts:
2 left
💡 Hint
Remember the deque stores indices of elements in decreasing order of their values within the current window.
✗ Incorrect
The sliding window maximum for window size 3 moves over the array and picks the maximum in each window. The output is the maximum values for windows: [1,3,-1], [3,-1,-3], [-1,-3,5], [-3,5,3], [5,3,6], [3,6,7].
🧠 Conceptual
intermediateUnderstanding the Role of Deque in Sliding Window Maximum
Why do we use a deque to solve the sliding window maximum problem efficiently?
Attempts:
2 left
💡 Hint
Think about how we maintain the maximum candidate elements as the window slides.
✗ Incorrect
Deque helps keep track of indices of elements in decreasing order of their values. It allows removing elements that are out of the current window or smaller than the current element efficiently from both ends.
🔧 Debug
advancedIdentify the Bug in Sliding Window Maximum Code
What error does the following code produce when run for sliding window maximum?
DSA C++
std::vector<int> nums = {4, 2, 12, 3, 8}; int k = 3; std::deque<int> dq; std::vector<int> result; for (int i = 0; i < nums.size(); ++i) { while (!dq.empty() && nums[dq.back()] <= nums[i]) dq.pop_back(); dq.push_back(i); if (i >= k - 1) { if (dq.front() <= i - k) dq.pop_front(); result.push_back(nums[dq.front()]); } } for (int val : result) std::cout << val << " ";
Attempts:
2 left
💡 Hint
Check the order of popping elements outside the window and pushing the maximum.
✗ Incorrect
The code checks and removes indices outside the window after pushing the maximum, so stale indices may be included in the result. The check for removing old indices should happen before pushing the maximum value.
❓ Predict Output
advancedOutput of Sliding Window Maximum for Decreasing Array
What is the output of the sliding window maximum algorithm using a deque for the array [9, 7, 5, 3, 1] with window size 2?
DSA C++
std::vector<int> nums = {9, 7, 5, 3, 1}; int k = 2; std::deque<int> dq; std::vector<int> result; for (int i = 0; i < nums.size(); ++i) { while (!dq.empty() && dq.front() <= i - k) dq.pop_front(); while (!dq.empty() && nums[dq.back()] < nums[i]) dq.pop_back(); dq.push_back(i); if (i >= k - 1) result.push_back(nums[dq.front()]); } for (int val : result) std::cout << val << " ";
Attempts:
2 left
💡 Hint
The array is strictly decreasing, so the first element in each window is the maximum.
✗ Incorrect
For window size 2, the windows are [9,7], [7,5], [5,3], [3,1]. The maximums are the first elements in each window: 9, 7, 5, 3.
🧠 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 array of size n and window size k?
Attempts:
2 left
💡 Hint
Consider how many times each element can be added or removed from the deque.
✗ Incorrect
Each element is pushed into the deque once and popped at most once, so total operations are proportional to n, making the algorithm O(n).
