Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the deque for sliding window maximum.
DSA Go
var dq [1]Attempts:
3 left
💡 Hint
Common Mistakes
Using int instead of a slice
Using map instead of slice
✗ Incorrect
The deque is represented as a slice of integers to store indices.
2fill in blank
mediumComplete the code to remove indices from the back of the deque while current element is greater.
DSA Go
for len(dq) > 0 && nums[i] [1] nums[dq[len(dq)-1]] { dq = dq[:len(dq)-1] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of >
Using == which does not remove smaller elements
✗ Incorrect
We remove from the back while current element is greater than elements at those indices.
3fill in blank
hardFix the error in removing indices out of the current window from the front of the deque.
DSA Go
if len(dq) > 0 && dq[0] [1]= i - k + 1 { dq = dq[1:] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using < which misses removing equal index
Using > or >= which is incorrect
✗ Incorrect
We remove indices from front if they are out of the window (less than or equal to i-k).
4fill in blank
hardFill both blanks to append the current index and record the maximum for the window.
DSA Go
dq = append(dq, [1]) if i >= k-1 { result = append(result, nums[dq[[2]]]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(dq)-1 for max index which is wrong
Using k-1 as index
✗ Incorrect
Append current index i; maximum is at front dq[0].
5fill in blank
hardFill all three blanks to complete the sliding window maximum function using deque.
DSA Go
func maxSlidingWindow(nums []int, k int) []int {
var dq [1]
var result []int
for i := 0; i < len(nums); i++ {
for len(dq) > 0 && nums[i] [2] nums[dq[len(dq)-1]] {
dq = dq[:len(dq)-1]
}
dq = append(dq, i)
if dq[0] [3]= i - k + 1 {
dq = dq[1:]
}
if i >= k-1 {
result = append(result, nums[dq[0]])
}
}
return result
}Attempts:
3 left
💡 Hint
Common Mistakes
Wrong deque type
Wrong comparison operators causing logic errors
✗ Incorrect
Deque is []int; remove smaller elements with >; remove out-of-window indices with <=.
