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 sliding window maximum algorithm using a deque for the array [1, 3, -1, -3, 5, 3, 6, 7] with window size 3?
DSA C
int arr[] = {1, 3, -1, -3, 5, 3, 6, 7}; int n = 8; int k = 3; // Sliding window maximum using deque logic applied here // Output the maximums for each window
Attempts:
2 left
💡 Hint
Remember the deque keeps indexes of elements in decreasing order of their values.
✗ Incorrect
The sliding window maximum for each window of size 3 is calculated by maintaining a deque that stores indexes of useful elements. The maximum for each window is the element at the front of the deque.
❓ Predict Output
intermediate2:00remaining
Sliding Window Maximum Output for All Negative Numbers
What is the output of the sliding window maximum algorithm using a deque for the array [-4, -2, -5, -1, -3] with window size 2?
DSA C
int arr[] = {-4, -2, -5, -1, -3}; int n = 5; int k = 2; // Sliding window maximum using deque logic applied here // Output the maximums for each window
Attempts:
2 left
💡 Hint
Maximum in a window of negative numbers is the least negative (closest to zero).
✗ Incorrect
The sliding window maximum for each window of size 2 is the maximum (least negative) element in that window. Using deque, we track indexes of elements in decreasing order.
🔧 Debug
advanced2:00remaining
Identify the Error in Sliding Window Maximum Implementation
Given the following code snippet for sliding window maximum using deque, what error will it cause when run?
DSA C
void slidingWindowMax(int arr[], int n, int k) { deque<int> dq; for (int i = 0; i < n; i++) { while (!dq.empty() && arr[dq.back()] < arr[i]) dq.pop_back(); dq.push_back(i); if (dq.front() <= i - k) dq.pop_front(); if (i >= k - 1) printf("%d ", arr[dq.front()]); } } int main() { int arr[] = {2, 1, 3, 4, 6, 3, 8, 9, 10, 12, 56}; int n = sizeof(arr)/sizeof(arr[0]); int k = 4; slidingWindowMax(arr, n, k); return 0; }
Attempts:
2 left
💡 Hint
Check the condition for popping from front of deque carefully.
✗ Incorrect
The code correctly maintains the deque and prints the maximum for each sliding window. The condition dq.front() <= i - k correctly removes indexes out of the current window.
🧠 Conceptual
advanced1:30remaining
Why Use Deque for Sliding Window Maximum?
Why is a deque the preferred data structure for implementing the sliding window maximum algorithm efficiently?
Attempts:
2 left
💡 Hint
Think about how the window slides and how elements are added or removed.
✗ Incorrect
Deque supports adding and removing elements from both ends in constant time, which is essential for maintaining the sliding window and tracking maximums efficiently.
🚀 Application
expert3:00remaining
Find Maximum of Sliding Windows with Varying Sizes
Given an array and a list of window sizes, which approach correctly computes the maximum for each sliding window size using deque efficiently?
Attempts:
2 left
💡 Hint
Consider how window size affects the sliding window maximum algorithm.
✗ Incorrect
Each window size requires its own pass because the maximum depends on the window length. Using deque separately for each window size is efficient and correct.
