📖 Scenario: Imagine you have a list of daily sales numbers for a small shop. You want to quickly find the total sales between any two days without adding each day every time.
🎯 Goal: You will build a prefix sum array in Go. This array helps you find the sum of sales between any two days quickly.
📋 What You'll Learn
Create a slice called sales with exact daily sales values
Create a slice called prefixSums to store prefix sums
Calculate prefix sums using a for loop
Print the prefixSums slice to show the result
💡 Why This Matters
🌍 Real World
Prefix sums are used in finance, data analysis, and gaming to quickly calculate sums over ranges without repeated addition.
💼 Career
Understanding prefix sums helps in coding interviews and real-world tasks where performance matters for large data.
Progress0 / 4 steps
1
Create the sales data slice
Create a slice called sales with these exact daily sales values: 5, 8, 6, 3, 7.
DSA Go
Hint
Use sales := []int{5, 8, 6, 3, 7} to create the slice.
2
Create the prefix sums slice
Create a slice called prefixSums with the same length as sales and initialize all values to zero.
DSA Go
Hint
Use make([]int, len(sales)) to create the slice.
3
Calculate prefix sums
Use a for loop with variable i from 0 to len(sales)-1 to fill prefixSums. Set prefixSums[0] to sales[0]. For other indices, set prefixSums[i] to prefixSums[i-1] + sales[i].
DSA Go
Hint
Remember to set the first prefix sum before the loop.
4
Print the prefix sums slice
Use fmt.Println(prefixSums) to print the prefixSums slice.
DSA Go
Hint
Use fmt.Println(prefixSums) to display the result.
Practice
(1/5)
1. What is the main purpose of a prefix sum array in algorithms?
easy
A. To find the maximum element in the array
B. To sort the array elements in ascending order
C. To reverse the array elements efficiently
D. To quickly find the sum of elements in any subarray
Solution
Step 1: Understand prefix sum array concept
A prefix sum array stores cumulative sums of elements from the start to each position.
Step 2: Use prefix sums for subarray sums
Using prefix sums, sum of any subarray can be found quickly by subtracting two prefix sums.
Final Answer:
To quickly find the sum of elements in any subarray -> Option D
Quick Check:
Prefix sum array helps fast subarray sums [OK]
Hint: Prefix sums store cumulative sums for fast subarray queries [OK]
Common Mistakes:
Confusing prefix sum with sorting
Thinking prefix sum reverses array
Assuming prefix sum finds max element
2. Which of the following Go code snippets correctly initializes a prefix sum array prefix from an integer slice arr?
easy
A. prefix := make([]int, len(arr))
for i := 0; i < len(arr); i++ {
prefix[i] = prefix[i-1] + arr[i]
}
B. prefix := make([]int, len(arr)+1)
for i := 1; i <= len(arr); i++ {
prefix[i] = prefix[i-1] + arr[i-1]
}
C. prefix := make([]int, len(arr)+1)
for i := 0; i < len(arr); i++ {
prefix[i] = prefix[i-1] + arr[i]
}
D. prefix := make([]int, len(arr))
for i := 1; i < len(arr); i++ {
prefix[i] = prefix[i-1] + arr[i]
}
Solution
Step 1: Check prefix array size and indexing
Prefix sum array usually has length one more than original to handle zero prefix sum at start.
Step 2: Verify loop and indexing logic
Loop from 1 to len(arr), prefix[i] = prefix[i-1] + arr[i-1] correctly accumulates sums.
Final Answer:
prefix := make([]int, len(arr)+1)
for i := 1; i <= len(arr); i++ {
prefix[i] = prefix[i-1] + arr[i-1]
} -> Option B
Quick Check:
Correct prefix sum initialization uses offset indexing [OK]
Hint: Prefix array length = original length + 1, loop starts at 1 [OK]
Common Mistakes:
Using prefix array same length as original
Starting loop at 0 and accessing prefix[-1]
Incorrect indexing of arr inside loop
3. What is the output of the following Go code?
arr := []int{2, 4, 6, 8}
prefix := make([]int, len(arr)+1)
for i := 1; i <= len(arr); i++ {
prefix[i] = prefix[i-1] + arr[i-1]
}
fmt.Println(prefix)