Bird
0
0
DSA Cprogramming~10 mins

Sliding Window on Arrays in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aarr
Bn
C0
Dk
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.
2fill in blank
medium

Complete 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'
Ak
B0
Cn
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 0 causes incorrect subtraction of elements.
Starting from 1 misses the first full window.
3fill in blank
hard

Fix 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'
Ak
Bn
Cn - 1
Dn + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using i <= n causes out-of-bounds access.
Using i <= k limits the loop incorrectly.
4fill in blank
hard

Fill 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'
Ak
Bn - 1
Cn - k
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using n - k as loop end causes missing last windows.
Starting from 0 causes invalid subtraction.
5fill in blank
hard

Fill 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'
Ak
Bn - 1
Ci - k + 1
Di
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.