Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a deque of integers.
DSA C++
std::deque<int> [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using 'queue' or 'stack' which are different container types.
Using 'list' which is a linked list, not a deque.
✗ Incorrect
The deque is declared as std::deque dq; which creates a double-ended queue named dq.
2fill in blank
mediumComplete the code to remove elements from the back of the deque while the current element is greater.
DSA C++
while (!dq.empty() && nums[dq.back()] [1] nums[i]) { dq.pop_back(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' which reverses the logic.
Using '==' which does not remove smaller elements.
✗ Incorrect
We remove elements from the back while nums[dq.back()] < nums[i] to maintain decreasing order in the deque.
3fill in blank
hardFix the error in the condition to remove elements out of the current window from the front of the deque.
DSA C++
if (!dq.empty() && dq.front() [1]= i - k) { dq.pop_front(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' alone misses elements exactly at the boundary.
Using '>' or '>=' reverses the logic.
✗ Incorrect
We remove elements from the front if dq.front() <= i - k, meaning they are out of the current window.
4fill in blank
hardFill both blanks to correctly assign the maximum value of the current window to the result vector.
DSA C++
if (i >= [1] - 1) { result.push_back(nums[[2].front()]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'window' which is undefined.
Using 'i' instead of 'k' for the window size check.
✗ Incorrect
We start adding max values to result when i >= k - 1, and the max is at nums[dq.front()].
5fill in blank
hardFill all three blanks to complete the sliding window maximum function using deque.
DSA C++
std::vector<int> maxSlidingWindow(std::vector<int>& nums, int k) {
std::deque<int> [1];
std::vector<int> result;
for (int i = 0; i < nums.size(); ++i) {
while (![2].empty() && nums[[2].back()] < nums[i]) {
[2].pop_back();
}
[2].push_back(i);
if ([2].front() <= i - k) {
[2].pop_front();
}
if (i >= k - 1) {
result.push_back(nums[[2].front()]);
}
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the deque causing errors.
Using undefined variable names like 'window' or 'queue'.
✗ Incorrect
The deque variable is named 'dq' consistently throughout the function to store indices of elements.
