Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the deque as an empty array.
DSA Typescript
const deque: number[] = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using {} which creates an object, not an array.
Using null or 0 which are not arrays.
✗ Incorrect
The deque is initialized as an empty array to store indices.
2fill in blank
mediumComplete the code to remove indices from the back of the deque while the current element is greater.
DSA Typescript
while (deque.length > 0 && nums[i] [1] nums[deque[deque.length - 1]]) { deque.pop(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which removes wrong elements.
Using '==' which does not remove any elements.
✗ Incorrect
We remove from the back while current element is greater than elements at those indices.
3fill in blank
hardFix the error in the condition to remove indices out of the current window from the front of the deque.
DSA Typescript
if (deque.length > 0 && deque[0] [1] i - k + 1) { deque.shift(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '>=' which removes wrong indices.
Using '<=' which removes indices equal to window start incorrectly.
✗ Incorrect
We remove indices from front if they are less than the start index of the window.
4fill in blank
hardFill both blanks to add the current index to the deque and record the max for the window.
DSA Typescript
deque.[1](i); if (i >= k - 1) { result.push(nums[deque[2]]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes elements instead of adding.
Using deque.length - 1 gets last element, not front.
✗ Incorrect
We add current index to back with push and get max at front (deque[0]) which is index 0.
5fill in blank
hardFill all three blanks to complete the sliding window maximum function using deque.
DSA Typescript
function maxSlidingWindow(nums: number[], k: number): number[] {
const deque: number[] = [1];
const result: number[] = [];
for (let i = 0; i < nums.length; i++) {
while (deque.length > 0 && nums[i] [2] nums[deque[deque.length - 1]]) {
deque.pop();
}
deque.push(i);
if (deque[0] [3] i - k + 1) {
deque.shift();
}
if (i >= k - 1) {
result.push(nums[deque[0]]);
}
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators causing incorrect removals.
Not initializing deque as an array.
✗ Incorrect
Initialize deque as empty array, remove smaller elements with '>', remove out-of-window indices with '<'.
