Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the window sum with the first k elements.
DSA Java
int windowSum = 0; for (int i = 0; i < [1]; i++) { windowSum += nums[i]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using nums.length instead of k causes summing the whole array.
Using k - 1 misses the last element in the window.
✗ Incorrect
We sum the first k elements to initialize the sliding window sum.
2fill in blank
mediumComplete the code to slide the window by removing the left element and adding the right element.
DSA Java
for (int i = [1]; i < nums.length; i++) { windowSum = windowSum - nums[i - k] + nums[i]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 0 causes index errors when subtracting.
Starting from k - 1 misses the first slide.
✗ Incorrect
The loop starts from index k to slide the window forward.
3fill in blank
hardFix the error in the code to update the maximum sum correctly inside the loop.
DSA Java
int maxSum = windowSum; for (int i = k; i < nums.length; i++) { windowSum = windowSum - nums[i - k] + nums[i]; maxSum = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.min updates maxSum incorrectly.
Adding maxSum and windowSum accumulates sums wrongly.
✗ Incorrect
We update maxSum to the larger of current maxSum and windowSum.
4fill in blank
hardFill both blanks to create a sliding window that finds the minimum sum of any subarray of size k.
DSA Java
int windowSum = 0; for (int i = 0; i < [1]; i++) { windowSum += nums[i]; } int minSum = windowSum; for (int i = [2]; i < nums.length; i++) { windowSum = windowSum - nums[i - k] + nums[i]; minSum = Math.min(minSum, windowSum); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using k - 1 causes off-by-one errors.
Starting sliding from 0 causes index errors.
✗ Incorrect
Both loops use k to define the window size and start sliding from index k.
5fill in blank
hardFill all three blanks to create a sliding window that counts how many subarrays of size k have sum greater than threshold.
DSA Java
int count = 0; int windowSum = 0; for (int i = 0; i < [1]; i++) { windowSum += nums[i]; } if (windowSum > [2]) { count++; } for (int i = [3]; i < nums.length; i++) { windowSum = windowSum - nums[i - k] + nums[i]; if (windowSum > threshold) { count++; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using k - 1 for loop start causes missing subarrays.
Comparing with wrong variable instead of threshold.
✗ Incorrect
Initialize window with first k elements, compare sum with threshold, and slide starting from k.
