Bird
0
0
DSA Cprogramming~20 mins

Prefix Sum Array in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prefix Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Prefix Sum Array Construction
What is the output of the prefix sum array after running the following code?
DSA C
int arr[] = {2, 4, 6, 8};
int n = 4;
int prefix[n];
prefix[0] = arr[0];
for(int i = 1; i < n; i++) {
    prefix[i] = prefix[i-1] + arr[i];
}
for(int i = 0; i < n; i++) {
    printf("%d ", prefix[i]);
}
A2 4 6 8
B2 6 12 20
C2 8 14 22
D0 2 6 12
Attempts:
2 left
💡 Hint
Remember prefix sum at index i is sum of all elements from 0 to i.
🧠 Conceptual
intermediate
1:30remaining
Understanding Prefix Sum Array Usage
Which of the following is the main advantage of using a prefix sum array?
AIt reduces the array size by half.
BIt sorts the array elements in ascending order.
CIt allows quick calculation of sum of any subarray in constant time after preprocessing.
DIt finds the maximum element in the array.
Attempts:
2 left
💡 Hint
Think about how prefix sums help in range sum queries.
🔧 Debug
advanced
2:00remaining
Identify the Error in Prefix Sum Calculation
What error will this code produce when calculating prefix sums?
DSA C
int arr[] = {1, 3, 5};
int n = 3;
int prefix[n];
for(int i = 0; i <= n; i++) {
    if(i == 0) prefix[i] = arr[i];
    else prefix[i] = prefix[i-1] + arr[i];
}
for(int i = 0; i < n; i++) {
    printf("%d ", prefix[i]);
}
AOutput: 1 3 5
BOutput: 1 4 9
CCompilation error due to missing semicolon
DRuntime error: Array index out of bounds
Attempts:
2 left
💡 Hint
Check the loop condition carefully for array indexing.
🚀 Application
advanced
1:30remaining
Calculate Sum of Subarray Using Prefix Sum Array
Given prefix sum array prefix = {3, 8, 15, 21, 28}, what is the sum of elements from index 2 to 4 (0-based)?
A20
B13
C15
D28
Attempts:
2 left
💡 Hint
Use formula: sum(l, r) = prefix[r] - prefix[l-1]
Predict Output
expert
2:30remaining
Output of Prefix Sum with Negative Numbers
What is the output of the prefix sum array after running this code?
DSA C
int arr[] = {5, -2, 7, -3, 4};
int n = 5;
int prefix[n];
prefix[0] = arr[0];
for(int i = 1; i < n; i++) {
    prefix[i] = prefix[i-1] + arr[i];
}
for(int i = 0; i < n; i++) {
    printf("%d ", prefix[i]);
}
A5 3 10 7 11
B0 5 3 10 7
C5 3 10 13 17
D5 -2 7 -3 4
Attempts:
2 left
💡 Hint
Add each element to the sum of all previous elements.