Challenge - 5 Problems
Sliding Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Sliding Window Sum Calculation
What is the output of the following Go code that calculates the sum of every sliding window of size 3 in the array?
DSA Go
package main import "fmt" func main() { arr := []int{1, 3, 2, 6, -1, 4, 1, 8, 2} k := 3 var result []int windowSum := 0 for i := 0; i < len(arr); i++ { windowSum += arr[i] if i >= k-1 { result = append(result, windowSum) windowSum -= arr[i-k+1] } } fmt.Println(result) }
Attempts:
2 left
💡 Hint
Think about how the window sum moves by adding the new element and removing the oldest element in the window.
✗ Incorrect
The code sums every group of 3 consecutive elements. The sums are: (1+3+2)=6, (3+2+6)=11, (2+6+-1)=7, (6+-1+4)=9, (-1+4+1)=4, (4+1+8)=13, (1+8+2)=11.
🧠 Conceptual
intermediateSliding Window Maximum Explanation
Which statement correctly describes the sliding window maximum algorithm for an array?
Attempts:
2 left
💡 Hint
Think about how to avoid re-checking all elements in each window.
✗ Incorrect
The sliding window maximum algorithm uses a double-ended queue (deque) to store indices of elements in decreasing order. This allows finding the maximum in O(1) time per window, leading to O(n) overall.
🔧 Debug
advancedIdentify the Bug in Sliding Window Average Code
What error will the following Go code produce when calculating the average of every sliding window of size 4?
DSA Go
package main import "fmt" func main() { arr := []int{2, 5, 1, 8, 2, 9, 1} k := 4 var result []float64 windowSum := 0 for i := 0; i < len(arr); i++ { windowSum += arr[i] if i >= k { windowSum -= arr[i-k] avg := float64(windowSum) / float64(k) result = append(result, avg) } } fmt.Println(result) }
Attempts:
2 left
💡 Hint
Check the condition that triggers the average calculation and when the first average is appended.
✗ Incorrect
The condition 'if i >= k' delays the first average calculation until after the first window is passed, so the first window's average is never added. The code does not panic and uses float64 for division.
❓ Predict Output
advancedOutput of Sliding Window Minimum Using Deque
What is the output of this Go code that finds the minimum in every sliding window of size 3?
DSA Go
package main import "fmt" func main() { arr := []int{4, 2, 12, 3, 5, 1, 7} k := 3 var result []int var deque []int for i := 0; i < len(arr); i++ { for len(deque) > 0 && arr[deque[len(deque)-1]] >= arr[i] { deque = deque[:len(deque)-1] } deque = append(deque, i) if deque[0] <= i-k { deque = deque[1:] } if i >= k-1 { result = append(result, arr[deque[0]]) } } fmt.Println(result) }
Attempts:
2 left
💡 Hint
The deque stores indices of elements in increasing order to track minimums.
✗ Incorrect
The minimums for windows are: (4,2,12)=2, (2,12,3)=2, (12,3,5)=3, (3,5,1)=1, (5,1,7)=1.
🧠 Conceptual
expertTime Complexity of Sliding Window Algorithms
What is the time complexity of the sliding window maximum algorithm using a deque for an array of length n and window size k?
Attempts:
2 left
💡 Hint
Consider how many times each element enters and leaves the deque.
✗ Incorrect
Each element is pushed and popped from the deque at most once, so the total operations are proportional to n, making the time complexity O(n).
