Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the window sum with the first k elements.
DSA C
int window_sum = 0; for (int i = 0; i < [1]; i++) { window_sum += arr[i]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of k causes summing the whole array.
Using 0 as loop limit results in no iteration.
✗ 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 C
for (int i = [1]; i < n; i++) { window_sum = window_sum - arr[i - k] + arr[i]; // process window_sum }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 0 causes incorrect subtraction of elements.
Starting from 1 misses the first full window.
✗ Incorrect
The loop starts from k to slide the window after the initial sum.
3fill in blank
hardFix the error in the loop condition to avoid out-of-bounds access.
DSA C
for (int i = k; i <= [1]; i++) { window_sum = window_sum - arr[i - k] + arr[i]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using i <= n causes out-of-bounds access.
Using i <= k limits the loop incorrectly.
✗ Incorrect
Loop should run while i is less than n to avoid accessing arr[n], which is out-of-bounds.
4fill in blank
hardFill both blanks to create a sliding window that finds the maximum sum of any window of size k.
DSA C
int max_sum = window_sum; for (int i = [1]; i <= [2]; i++) { window_sum = window_sum - arr[i - k] + arr[i]; if (window_sum > max_sum) { max_sum = window_sum; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using n - k as loop end causes missing last windows.
Starting from 0 causes invalid subtraction.
✗ Incorrect
Loop starts at k and runs until n-1 to cover all windows of size k.
5fill in blank
hardFill all three blanks to create a sliding window that stores sums of all windows of size k in an array.
DSA C
int sums[n - k + 1]; sums[0] = window_sum; for (int i = [1]; i <= [2]; i++) { window_sum = window_sum - arr[i - k] + arr[i]; sums[[3]] = window_sum; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using i as index causes out-of-bounds in sums array.
Loop ending before n-1 misses last windows.
✗ Incorrect
Loop starts at k and ends at n-1; sums are stored at index i - k + 1.
