Bird
0
0
DSA Cprogramming~20 mins

Sliding Window on Arrays in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sliding Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Sliding Window Maximum
What is the output of the following code that finds the maximum in each sliding window of size 3?
DSA C
int arr[] = {1, 3, -1, -3, 5, 3, 6, 7};
int n = 8, k = 3;
int result[6];
int max_index = 0;
for (int i = 1; i < k; i++) {
    if (arr[i] > arr[max_index]) max_index = i;
}
result[0] = arr[max_index];
for (int i = k; i < n; i++) {
    if (max_index <= i - k) {
        max_index = i - k + 1;
        for (int j = max_index + 1; j <= i; j++) {
            if (arr[j] > arr[max_index]) max_index = j;
        }
    } else if (arr[i] > arr[max_index]) {
        max_index = i;
    }
    result[i - k + 1] = arr[max_index];
}
for (int i = 0; i <= n - k; i++) {
    printf("%d ", result[i]);
}
A1 3 3 5 6 7
B3 3 5 5 6 7
C3 -1 5 3 6 7
D3 3 5 3 6 7
Attempts:
2 left
💡 Hint
Track the maximum index carefully when the window slides.
Predict Output
intermediate
2:00remaining
Sum of Sliding Window
What is the output of this code that calculates the sum of each sliding window of size 4?
DSA C
int arr[] = {2, 1, 5, 1, 3, 2};
int n = 6, k = 4;
int sum = 0;
int result[3];
for (int i = 0; i < k; i++) {
    sum += arr[i];
}
result[0] = sum;
for (int i = k; i < n; i++) {
    sum += arr[i] - arr[i - k];
    result[i - k + 1] = sum;
}
for (int i = 0; i <= n - k; i++) {
    printf("%d ", result[i]);
}
A11 10 9
B9 10 12
C2 1 5 1
D9 10 11
Attempts:
2 left
💡 Hint
Add the new element and remove the old element from the sum as the window slides.
🔧 Debug
advanced
2:00remaining
Identify the error in sliding window minimum code
What error does the following code produce when trying to find the minimum in each sliding window of size 2?
DSA C
int arr[] = {4, 2, 12, 3, 5};
int n = 5, k = 2;
int min_index = 0;
for (int i = 1; i < k; i++) {
    if (arr[i] < arr[min_index]) min_index = i;
}
printf("%d ", arr[min_index]);
for (int i = k; i < n; i++) {
    if (min_index <= i - k) {
        min_index = i - k + 1;
        for (int j = min_index + 1; j <= i; j++) {
            if (arr[j] < arr[min_index]) min_index = j;
        }
    } else if (arr[i] < arr[min_index]) {
        min_index = i;
    }
    printf("%d ", arr[min_index]);
}
ARuntime error: out-of-bounds access
BNo error, prints: 2 2 3 3
CNo error, prints: 4 2 3 3
DLogic error: skips some minimum values
Attempts:
2 left
💡 Hint
Check the condition that updates min_index when the window slides.
Predict Output
advanced
2:00remaining
Output of Sliding Window with Variable Window Size
What is the output of this code that prints the maximum of sliding windows with sizes increasing from 1 to 3?
DSA C
int arr[] = {1, 3, 2, 5};
int n = 4;
for (int k = 1; k <= 3; k++) {
    for (int i = 0; i <= n - k; i++) {
        int max_val = arr[i];
        for (int j = i + 1; j < i + k; j++) {
            if (arr[j] > max_val) max_val = arr[j];
        }
        printf("%d ", max_val);
    }
    printf("\n");
}
A
1 3 2 5 
3 2 5 
3 5 5 
B
1 3 2 5 
3 3 5 
2 5 5 
C
1 3 2 5 
3 3 5 
3 5 5 
D
1 3 2 5 
3 3 5 
3 3 5 
Attempts:
2 left
💡 Hint
For each window size, find the max in each window carefully.
🧠 Conceptual
expert
2:00remaining
Sliding Window Algorithm Optimization
Which data structure is best suited to optimize the sliding window maximum problem to O(n) time complexity?
ADeque (double-ended queue)
BPriority Queue (Heap)
CQueue
DStack
Attempts:
2 left
💡 Hint
Think about a structure that allows insertion and removal from both ends efficiently.