Recall & Review
beginner
What is a Prefix Sum Array?
A Prefix Sum Array is a new array where each element at index i is the sum of all elements from the start up to i in the original array. It helps to quickly find the sum of any subarray.
Click to reveal answer
beginner
How do you calculate the prefix sum array for [2, 4, 6, 8]?
Start with the first element: 2. Then add each next element to the sum so far: [2, 2+4=6, 6+6=12, 12+8=20]. So the prefix sum array is [2, 6, 12, 20].
Click to reveal answer
beginner
Why is a prefix sum array useful?
It allows you to find the sum of any part of the array quickly by subtracting two prefix sums instead of adding many elements each time.
Click to reveal answer
intermediate
What is the time complexity to build a prefix sum array of size n?
The time complexity is O(n) because you calculate each prefix sum by adding one element to the previous sum in a single pass.
Click to reveal answer
intermediate
How do you find the sum of elements from index i to j using a prefix sum array?
Sum from i to j = prefix_sum[j] - prefix_sum[i-1] if i > 0, else prefix_sum[j] if i == 0.
Click to reveal answer
What does the element at index 3 in a prefix sum array represent?
✗ Incorrect
Each prefix sum element at index i is the sum of all original elements from 0 to i.
What is the time complexity to find the sum of any subarray using a prefix sum array?
✗ Incorrect
Using prefix sums, subarray sums can be found in constant time O(1) by subtraction.
If prefix_sum = [3, 7, 12, 18], what is the sum of elements from index 1 to 3?
✗ Incorrect
Sum = prefix_sum[3] - prefix_sum[0] = 18 - 3 = 15.
Which of the following is NOT true about prefix sum arrays?
✗ Incorrect
Prefix sum arrays store sums, not products.
What is the prefix sum array of [1, 1, 1, 1]?
✗ Incorrect
Each element is the sum of all previous elements: 1, 1+1=2, 2+1=3, 3+1=4.
Explain how to build a prefix sum array from a given array and why it is useful.
Think about adding elements one by one and how it helps in fast queries.
You got /3 concepts.
Describe how to find the sum of elements between two indices i and j using a prefix sum array.
Use subtraction of prefix sums to get the subarray sum.
You got /3 concepts.