What is the main purpose of using a deque in the Sliding Window Maximum problem?
A deque helps keep track of indices of elements in the current window in a way that the front always holds the index of the maximum element. It allows efficient insertion and removal from both ends to maintain the maximum in O(1) amortized time per operation.
Click to reveal answer
intermediate
In the Sliding Window Maximum algorithm using deque, why do we remove elements from the back of the deque when a new element arrives?
We remove elements from the back if they are smaller than the new element because they cannot be the maximum if a bigger element is present later. This keeps the deque elements in decreasing order of their values.
Click to reveal answer
beginner
What does the deque store in the Sliding Window Maximum algorithm?
The deque stores indices of elements from the array, not the elements themselves. This helps to check if an element is out of the current window and to access the element's value easily.
Click to reveal answer
intermediate
How do you know when to remove the front element of the deque in the Sliding Window Maximum algorithm?
If the index at the front of the deque is outside the current window (less than current index minus window size plus one), it should be removed because it is no longer part of the window.
Click to reveal answer
beginner
What is the time complexity of the Sliding Window Maximum algorithm using a deque?
The time complexity is O(n) because each element is added and removed from the deque at most once, making all operations linear in total.
Click to reveal answer
What does the deque store in the Sliding Window Maximum algorithm?
AWindow sizes
BActual element values
CSum of elements
DIndices of elements
✗ Incorrect
The deque stores indices to efficiently check window boundaries and access element values.
Why do we remove elements from the back of the deque when a new element arrives?
ABecause they are smaller than the new element
BBecause they are larger than the new element
CTo keep the deque size fixed
DTo maintain ascending order
✗ Incorrect
Smaller elements at the back are removed because the new bigger element will overshadow them for maximum.
When do we remove the front element of the deque?
AWhen it is outside the current window
BWhen it is the maximum element
CWhen it is inside the current window
DWhen the deque is empty
✗ Incorrect
The front element is removed if its index is outside the current sliding window.
What is the time complexity of the Sliding Window Maximum algorithm using deque?
AO(n^2)
BO(n log n)
CO(n)
DO(log n)
✗ Incorrect
Each element is added and removed at most once, so the total time is linear.
What is the main advantage of using a deque for this problem?
AIt sorts the window elements
BIt allows constant time access to the maximum
CIt stores all elements permanently
DIt reduces memory usage
✗ Incorrect
Deque maintains elements in a way that the maximum is always at the front, allowing O(1) access.
Explain step-by-step how the deque is updated when sliding the window over the array.
Think about how the deque keeps only useful elements for maximum.
You got /4 concepts.
Describe why the Sliding Window Maximum problem can be solved in O(n) time using a deque.
Focus on how many times each element enters and leaves the deque.
You got /4 concepts.
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
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.
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.
Final Answer:
To store indices of elements in a way that helps find the maximum in O(1) time per window -> Option D
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
Step 1: Understand C data structure declaration
C does not have built-in deque type; we use structs or arrays to implement it.
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.
Final Answer:
struct deque dq; // assuming a deque struct is defined -> Option A
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
Step 1: Slide window and find max for each window of size 4
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
Step 1: Analyze deque maintenance logic
The code pops smaller elements from back before pushing current index, ensuring decreasing order.
Step 2: Check window boundary condition
The code pops front if the front index is out of current window (<= i-k), which is correct.
Step 3: Confirm output printing
Printing starts after first full window (i >= k-1), printing max at front index.
Final Answer:
No bug, code is correct -> Option B
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
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.
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.
Final Answer:
Change the comparison from arr[dq.back()] <= arr[i] to arr[dq.back()] >= arr[i] when popping from back -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.