Bird
0
0
DSA Cprogramming~20 mins

Sliding Window Maximum Using Deque in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sliding Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
A[3, -1, -3, 5, 3, 6]
B[1, 3, -1, -3, 5, 3]
C[3, 3, 5, 5, 6, 7]
D[7, 6, 5, 3, -3, -1]
Attempts:
2 left
💡 Hint
Remember the deque keeps indexes of elements in decreasing order of their values.
Predict Output
intermediate
2: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
A[-4, -2, -5, -1]
B[-4, -5, -3, -1]
C[-1, -1, -3, -3]
D[-2, -2, -1, -1]
Attempts:
2 left
💡 Hint
Maximum in a window of negative numbers is the least negative (closest to zero).
🔧 Debug
advanced
2: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;
}
ACorrect output: 4 6 6 8 9 10 12 56
BRuntime error due to invalid deque access
CCompilation error due to missing header
DNo output; infinite loop
Attempts:
2 left
💡 Hint
Check the condition for popping from front of deque carefully.
🧠 Conceptual
advanced
1:30remaining
Why Use Deque for Sliding Window Maximum?
Why is a deque the preferred data structure for implementing the sliding window maximum algorithm efficiently?
ABecause deque automatically sorts elements, so maximum is always at front
BBecause deque allows insertion and deletion from both ends in O(1) time, enabling efficient window updates
CBecause deque stores elements in a tree structure for fast searching
DBecause deque uses hashing to find maximum elements quickly
Attempts:
2 left
💡 Hint
Think about how the window slides and how elements are added or removed.
🚀 Application
expert
3: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?
ARun the sliding window maximum algorithm separately for each window size using deque
BUse a single deque to process all window sizes simultaneously in one pass
CSort the array once and pick maximums for all windows from sorted array
DUse a stack instead of deque to track maximums for all window sizes
Attempts:
2 left
💡 Hint
Consider how window size affects the sliding window maximum algorithm.