Challenge - 5 Problems
Prefix Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Prefix Sum Array Construction
What is the output of the prefix sum array after running this Go code?
DSA Go
package main import "fmt" func main() { arr := []int{3, 1, 4, 1, 5} prefix := make([]int, len(arr)) prefix[0] = arr[0] for i := 1; i < len(arr); i++ { prefix[i] = prefix[i-1] + arr[i] } fmt.Println(prefix) }
Attempts:
2 left
💡 Hint
Think about how each element in the prefix array sums all previous elements including itself.
✗ Incorrect
The prefix sum array stores cumulative sums. Each element is the sum of all elements up to that index in the original array.
🧠 Conceptual
intermediatePurpose of Prefix Sum Array
What is the main advantage of using a prefix sum array when querying sums of subarrays?
Attempts:
2 left
💡 Hint
Think about how prefix sums help avoid repeated addition.
✗ Incorrect
Prefix sum arrays let you find the sum of any subarray quickly by subtracting two prefix sums, avoiding repeated addition.
🔧 Debug
advancedIdentify the Bug in Prefix Sum Code
What error will this Go code produce when building a prefix sum array?
DSA Go
package main import "fmt" func main() { arr := []int{2, 4, 6} prefix := make([]int, len(arr)) for i := 1; i < len(arr); i++ { prefix[i] = prefix[i-1] + arr[i] } fmt.Println(prefix) }
Attempts:
2 left
💡 Hint
Check the loop boundaries and array indexing carefully.
✗ Incorrect
The loop uses i <= len(arr), which tries to access prefix[len(arr)] and arr[len(arr)], both out of range.
❓ Predict Output
advancedPrefix Sum Array with Negative Numbers
What is the output of the prefix sum array after running this Go code with negative numbers?
DSA Go
package main import "fmt" func main() { arr := []int{5, -2, 3, -1, 4} prefix := make([]int, len(arr)) prefix[0] = arr[0] for i := 1; i < len(arr); i++ { prefix[i] = prefix[i-1] + arr[i] } fmt.Println(prefix) }
Attempts:
2 left
💡 Hint
Add each element to the sum of all previous elements including negatives.
✗ Incorrect
Prefix sums include negative numbers, so sums can decrease or increase accordingly.
🚀 Application
expertUsing Prefix Sum to Count Subarrays with Sum Zero
Given an integer array, which approach uses prefix sums to count how many subarrays have a sum of zero?
Attempts:
2 left
💡 Hint
Think about how repeated prefix sums indicate zero-sum subarrays.
✗ Incorrect
If prefix sums repeat, the elements between those indices sum to zero. Counting repeats helps find zero-sum subarrays efficiently.
