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 index 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, 6 (2+4), 12 (6+6), 20 (12+8)].
Click to reveal answer
intermediate
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
Write the formula to find the sum of elements from index i to j using a prefix sum array.
Sum(i to j) = prefixSum[j] - prefixSum[i - 1] (if i > 0), else prefixSum[j] if i = 0.
Click to reveal answer
beginner
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 the current element to the previous prefix sum once.
Click to reveal answer
What does the element at index i in a prefix sum array represent?
ASum of elements from index i to the end of the array
BSum of elements from index 0 to i in the original array
CValue of the element at index i in the original array
DDifference between elements at i and i-1
✗ Incorrect
Each prefix sum element stores the total sum from the start up to that index.
How do you find the sum of elements between indices 2 and 4 using a prefix sum array?
AprefixSum[4] - prefixSum[1]
BprefixSum[4] + prefixSum[2]
CprefixSum[2] - prefixSum[4]
DprefixSum[4] - prefixSum[2]
✗ Incorrect
Sum from 2 to 4 = prefixSum[4] - prefixSum[1], subtracting sum before index 2.
What is the time complexity to get the sum of any subarray using a prefix sum array?
AO(n^2)
BO(n)
CO(1)
DO(log n)
✗ Incorrect
Using prefix sums, subarray sums can be found instantly with subtraction.
If the original array is empty, what is the prefix sum array?
AEmpty array
B[0]
CArray with one element 1
DUndefined
✗ Incorrect
An empty original array results in an empty prefix sum array.
Which of these is NOT a benefit of using prefix sum arrays?
AHelps in solving range sum problems
BFaster subarray sum queries
CSimple to build in O(n) time
DLess memory usage than original array
✗ Incorrect
Prefix sum arrays use extra memory equal to the original array size.
Explain how to build a prefix sum array from a given array and how it helps in finding subarray sums.
Think about adding elements one by one and how subtraction helps.
You got /4 concepts.
Describe the formula to get the sum of elements between two indices i and j using a prefix sum array and why it works.
Consider what prefixSum stores and how to exclude elements before i.
You got /4 concepts.
Practice
(1/5)
1. What does a prefix sum array represent for a given array arr?
easy
A. An array where each element is the sum of all elements from the start up to that index in arr.
B. An array where each element is the product of all elements from the start up to that index in arr.
C. An array where each element is the difference between consecutive elements in arr.
D. An array where each element is the maximum value found in arr up to that index.
Solution
Step 1: Understand prefix sum array definition
A prefix sum array stores sums of elements from the start of the original array up to each index.
Step 2: Compare options with definition
Only An array where each element is the sum of all elements from the start up to that index in arr. correctly describes this sum accumulation behavior.
Final Answer:
An array where each element is the sum of all elements from the start up to that index in arr. -> Option A
Quick Check:
Prefix sum = cumulative sum [OK]
Hint: Prefix sum means cumulative addition from start [OK]
Common Mistakes:
Confusing prefix sum with product or max
Thinking prefix sum stores differences
Assuming prefix sum stores original values
2. Which of the following JavaScript code snippets correctly initializes a prefix sum array prefix for an input array arr?
A. prefix array is not initialized with first element, causing prefix[-1] to be undefined.
B. The loop should start from i = 1 instead of 0.
C. The code uses addition instead of multiplication.
D. The array arr is empty, so prefix will be empty.
Solution
Step 1: Analyze prefix[-1] usage at i=0
At i=0, prefix[i-1] is prefix[-1], which is undefined, causing NaN result.
Step 2: Fix initialization to avoid undefined
Initialize prefix[0] = arr[0] before loop or handle i=0 separately.
Final Answer:
prefix array is not initialized with first element, causing prefix[-1] to be undefined. -> Option A
Quick Check:
Accessing prefix[-1] causes undefined [OK]
Hint: Initialize prefix[0] before loop to avoid undefined [OK]
Common Mistakes:
Starting loop at 0 without initializing prefix[0]
Ignoring undefined prefix[-1] access
Assuming prefix[-1] is zero automatically
5. Given an array arr = [3, 1, 4, 1, 5], how can you use a prefix sum array to efficiently find the sum of elements between indices 1 and 3 (inclusive)?
hard
A. Calculate prefix sums, then subtract prefix[0] from prefix[4].
B. Calculate prefix sums, then subtract prefix[0] from prefix[3].
C. Calculate prefix sums, then subtract prefix[0] from prefix[2].
D. Calculate prefix sums, then subtract prefix[1] from prefix[3].
Solution
Step 1: Build prefix sum array for arr
prefix = [3, 4, 8, 9, 14] where prefix[i] = sum of arr[0..i].