Challenge - 5 Problems
Sliding Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of sliding window maximum sum
What is the output of the following code that finds the maximum sum of any subarray of size 3 using sliding window?
DSA Python
arr = [2, 1, 5, 1, 3, 2] window_size = 3 max_sum = 0 window_sum = sum(arr[:window_size]) max_sum = window_sum for i in range(window_size, len(arr)): window_sum += arr[i] - arr[i - window_size] if window_sum > max_sum: max_sum = window_sum print(max_sum)
Attempts:
2 left
💡 Hint
Think about how the window slides and updates the sum by adding the new element and removing the old one.
✗ Incorrect
The sliding window sums are: 2+1+5=8, then 1+5+1=7, then 5+1+3=9, then 1+3+2=6. The maximum is 9.
❓ Predict Output
intermediate2:00remaining
Output of minimum average subarray
What is the output of this code that finds the minimum average of any subarray of size 4?
DSA Python
arr = [4, 2, 2, 5, 1, 5, 8] window_size = 4 window_sum = sum(arr[:window_size]) min_sum = window_sum for i in range(window_size, len(arr)): window_sum += arr[i] - arr[i - window_size] if window_sum < min_sum: min_sum = window_sum min_avg = min_sum / window_size print(min_avg)
Attempts:
2 left
💡 Hint
Calculate sums of each 4-element window and find the smallest sum, then divide by 4.
✗ Incorrect
The sums of windows are: 4+2+2+5=13, 2+2+5+1=10, 2+5+1+5=13, 5+1+5+8=19. The minimum sum is 10, so average is 10/4=2.5.
🔧 Debug
advanced2:00remaining
Identify the error in sliding window code
What error does this code raise when trying to find the maximum sum of subarrays of size 3?
DSA Python
arr = [1, 2, 3, 4, 5] window_size = 3 max_sum = 0 window_sum = sum(arr[:window_size]) for i in range(window_size, len(arr)): window_sum += arr[i] - arr[i - window_size] if window_sum > max_sum: max_sum = window_sum print(max_sum)
Attempts:
2 left
💡 Hint
Check if all variables are defined and indices are valid.
✗ Incorrect
All variables are defined and indices are valid. The code runs without error and outputs 12, which is the sum of last 3 elements (3+4+5).
🚀 Application
advanced2:00remaining
Find the count of subarrays with sum equal to k
Given an array and a number k, which option correctly counts the number of continuous subarrays whose sum equals k using sliding window?
DSA Python
arr = [1, 1, 1, 2, 3] k = 3 count = 0 start = 0 current_sum = 0 for end in range(len(arr)): current_sum += arr[end] while current_sum > k and start <= end: current_sum -= arr[start] start += 1 if current_sum == k: count += 1 print(count)
Attempts:
2 left
💡 Hint
Think about how the window shrinks when sum exceeds k and counts when sum equals k.
✗ Incorrect
The subarrays with sum 3 are: [1,1,1], [1,2], [3]. So count is 3.
🧠 Conceptual
expert2:00remaining
Why sliding window fails for negative numbers in subarray sum problem?
Why does the sliding window technique fail to find the count of subarrays with sum equal to k if the array contains negative numbers?
Attempts:
2 left
💡 Hint
Think about how the window shrinks when sum exceeds k and how negative numbers affect this logic.
✗ Incorrect
Sliding window works by shrinking the window when sum exceeds k, assuming adding more elements increases sum. Negative numbers can decrease sum, breaking this logic.