Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using a deque in the Sliding Window Maximum problem?
easy
A. To store the sum of elements in each window
B. To sort the array elements before processing
C. To reverse the array for easier traversal
D. To store indices of elements in a way that helps find the maximum in O(1) time per window

Solution

  1. Step 1: Understand the role of deque

    A deque stores indices of elements in a way that the front always has the index of the maximum element for the current window.
  2. Step 2: Why deque helps in O(1) max retrieval

    Because the deque maintains elements in decreasing order, the maximum is always at the front, allowing quick access without scanning the window.
  3. Final Answer:

    To store indices of elements in a way that helps find the maximum in O(1) time per window -> Option D
  4. Quick Check:

    Deque stores max indices for O(1) max retrieval [OK]
Hint: Deque keeps max at front for quick max retrieval [OK]
Common Mistakes:
  • Thinking deque stores actual values instead of indices
  • Using deque to sort elements
  • Using deque to store sums
2. Which of the following is the correct way to declare a deque in C for storing integer indices?
easy
A. struct deque dq; // assuming a deque struct is defined
B. deque<int> dq;
C. int dq[];
D. deque dq = new deque();

Solution

  1. Step 1: Understand C data structure declaration

    C does not have built-in deque type; we use structs or arrays to implement it.
  2. Step 2: Identify correct syntax

    struct deque dq; // assuming a deque struct is defined shows a struct declaration which is the correct way in C if a deque struct is defined.
  3. Final Answer:

    struct deque dq; // assuming a deque struct is defined -> Option A
  4. Quick Check:

    C uses structs for custom data types [OK]
Hint: Use struct for deque in C, no built-in deque type [OK]
Common Mistakes:
  • Using C++ syntax like deque<int>
  • Trying to declare deque like an array without size
  • Using Java or other language syntax
3. What is the output of the sliding window maximum for array [2, 1, 3, 4, 6, 3, 8, 9, 10, 12, 56] with window size 4?
medium
A. [3, 4, 6, 6, 8, 9, 10, 12, 56]
B. [2, 3, 4, 6, 8, 9, 10, 12]
C. [4, 6, 6, 8, 9, 10, 12, 56]
D. [6, 6, 8, 9, 10, 12, 56]

Solution

  1. Step 1: Slide window and find max for each window of size 4

    Windows and max values:
    Window 1: [2,1,3,4] max=4
    Window 2: [1,3,4,6] max=6
    Window 3: [3,4,6,3] max=6
    Window 4: [4,6,3,8] max=8
    Window 5: [6,3,8,9] max=9
    Window 6: [3,8,9,10] max=10
    Window 7: [8,9,10,12] max=12
    Window 8: [9,10,12,56] max=56
  2. Step 2: Collect all max values in order

    The max values are [4, 6, 6, 8, 9, 10, 12, 56]
  3. Final Answer:

    [4, 6, 6, 8, 9, 10, 12, 56] -> Option C
  4. Quick Check:

    Max per window = [4,6,6,8,9,10,12,56] [OK]
Hint: Slide window, track max with deque front [OK]
Common Mistakes:
  • Including partial windows smaller than size 4
  • Off-by-one errors in window indexing
  • Confusing max values with sums
4. Given this snippet for sliding window maximum, what is the bug?
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()]);
}
medium
A. The condition to pop front should be < instead of <=
B. No bug, code is correct
C. The condition to pop front should be checked before pushing current index
D. The order of popping from back and pushing is wrong

Solution

  1. Step 1: Analyze deque maintenance logic

    The code pops smaller elements from back before pushing current index, ensuring decreasing order.
  2. Step 2: Check window boundary condition

    The code pops front if the front index is out of current window (<= i-k), which is correct.
  3. Step 3: Confirm output printing

    Printing starts after first full window (i >= k-1), printing max at front index.
  4. Final Answer:

    No bug, code is correct -> Option B
  5. Quick Check:

    Deque logic and conditions are correct [OK]
Hint: Check deque front removal after pushing current index [OK]
Common Mistakes:
  • Removing front before pushing current index
  • Using wrong comparison operators in conditions
  • Printing output before full window
5. How would you modify the sliding window maximum algorithm using deque to find the sliding window minimum instead?
hard
A. Change the comparison from arr[dq.back()] <= arr[i] to arr[dq.back()] >= arr[i] when popping from back
B. Reverse the array before applying the same algorithm
C. Use a stack instead of a deque
D. Change the window size to half

Solution

  1. Step 1: Understand max vs min logic in deque

    For max, we pop from back while elements are smaller or equal. For min, we pop while elements are greater or equal.
  2. Step 2: Modify comparison operator

    Change condition from arr[dq.back()] <= arr[i] to arr[dq.back()] >= arr[i] to maintain increasing order for min.
  3. Final Answer:

    Change the comparison from arr[dq.back()] <= arr[i] to arr[dq.back()] >= arr[i] when popping from back -> Option A
  4. Quick Check:

    Reverse comparison for min sliding window [OK]
Hint: Flip comparison sign to find min instead of max [OK]
Common Mistakes:
  • Reversing array instead of changing comparison
  • Using stack which doesn't maintain order
  • Changing window size unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard