This quiz will test your understanding of the Prefix Sum technique and how to efficiently compute cumulative sums and solve array-related problems using this powerful approach in data structures and algorithms.
Question 1
What is a prefix sum array?
An array where each element is the sum of all elements to the right of it.
An array that stores the product of elements up to the current index.
An array that stores the sum of elements from the start to the current index.
An array where each element is the sum of all elements to the left of it.
Question 2
In what scenarios is the prefix sum technique particularly useful?
When performing a merge sort
When the array has duplicate elements
When you need to compute the sum of elements in a range frequently
When you need to find the minimum element in an array
Question 3
What will be the output of the following program :
int main() {
int arr[] = {4, -1, 2, 1};
int prefix_sum[4];
prefix_sum[0] = arr[0];
for (int i = 1; i < arr.size(); i++) {
prefix_sum[i] = prefix_sum[i-1] + arr[i];
}
for (int i = 0; i < arr.size(); i++) {
cout << prefix_sum[i] << " ";
}
return 0;
}
int main() {
int arr[] = {4, -1, 2, 1};
int prefix_sum[4];
prefix_sum[0] = arr[0];
for (int i = 1; i < 4; i++) {
prefix_sum[i] = prefix_sum[i-1] + arr[i];
}
for (int i = 0; i < 4; i++) {
printf("%d ", prefix_sum[i]);
}
return 0;
}
public class Main {
public static void main(String[] args) {
int[] arr = {4, -1, 2, 1};
int[] prefix_sum = new int[arr.length];
prefix_sum[0] = arr[0];
for (int i = 1; i < arr.length; i++) {
prefix_sum[i] = prefix_sum[i - 1] + arr[i];
}
// Printing the prefix sum
for (int i = 0; i < arr.length; i++) {
System.out.println(prefix_sum[i]);
}
}
}
arr = [4, -1, 2, 1]
prefix_sum = [0] * len(arr)
prefix_sum[0] = arr[0]
for i in range(1, len(arr)):
prefix_sum[i] = prefix_sum[i-1] + arr[i]
for i in range(len(arr)):
print(prefix_sum[i], end=' ')
let arr = [4, -1, 2, 1];
let prefix_sum = new Array(arr.length);
prefix_sum[0] = arr[0];
for (let i = 1; i < arr.length; i++) {
prefix_sum[i] = prefix_sum[i - 1] + arr[i];
}
for (let i = 0; i < arr.length; i++) {
console.log(prefix_sum[i] + ' ');
}
4 3 5 6
4 3 7 8
3 2 1 5
5 3 7 9
Question 4
What is the time complexity of constructing a prefix sum array for an array of size n
?
O(n)
O(log n)
O(1)
O(n^2)
Question 5
What is the time complexity of querying the sum of elements in a subarray after constructing the prefix sum array?
O(n)
O(n^2)
O(log n)
O(1)
Question 6
What happens if we modify the values of the original array after constructing a prefix sum array?
The prefix sum array will automatically update
The prefix sum array will keep the old values
The prefix sum array will update only the first element
The prefix sum array becomes invalid and needs to be recalculated
Question 7
What would be the output if pass arr[][] = [[10, 20, 30], [5, 10, 20], [2, 4, 6]] to the following code:
int[][] prefixSum2D(int a[R][C])
{
int psa[R][C];
psa[0][0] = a[0][0];
for (int i = 1; i < C; i++)
{
psa[0][i] = psa[0][i - 1] + a[0][i];
}
for (int i = 1; i < R; i++)
{
psa[i][0] = psa[i - 1][0] + a[i][0];
}
for (int i = 1; i < R; i++)
{
for (int j = 1; j < C; j++)
{
psa[i][j] = psa[i - 1][j] + psa[i][j - 1] - psa[i - 1][j - 1] + a[i][j];
}
}
return psa;
}
int[][] prefixSum2D(int a[R][C]) {
int psa[R][C];
psa[0][0] = a[0][0];
for (int i = 1; i < C; i++) {
psa[0][i] = psa[0][i - 1] + a[0][i];
}
for (int i = 1; i < R; i++) {
psa[i][0] = psa[i - 1][0] + a[i][0];
}
for (int i = 1; i < R; i++) {
for (int j = 1; j < C; j++) {
psa[i][j] = psa[i - 1][j] + psa[i][j - 1] - psa[i - 1][j - 1] + a[i][j];
}
}
return psa;
}
public static int[][] prefixSum2D(int[][] a) {
int[][] psa = new int[R][C];
psa[0][0] = a[0][0];
for (int i = 1; i < C; i++) {
psa[0][i] = psa[0][i - 1] + a[0][i];
}
for (int i = 1; i < R; i++) {
psa[i][0] = psa[i - 1][0] + a[i][0];
}
for (int i = 1; i < R; i++) {
for (int j = 1; j < C; j++) {
psa[i][j] = psa[i - 1][j] + psa[i][j - 1] - psa[i - 1][j - 1] + a[i][j];
}
}
}
return psa;
}
def prefix_sum_2d(a):
psa = [[0] * C for _ in range(R)]
psa[0][0] = a[0][0]
for i in range(1, C):
psa[0][i] = psa[0][i - 1] + a[0][i]
for i in range(1, R):
psa[i][0] = psa[i - 1][0] + a[i][0]
for i in range(1, R):
for j in range(1, C):
psa[i][j] = psa[i - 1][j] + psa[i][j - 1] - psa[i - 1][j - 1] + a[i][j]
return psa;
function prefixSum2D(a) {
let psa = Array.from({ length: R }, () => Array(C).fill(0));
psa[0][0] = a[0][0];
for (let i = 1; i < C; i++) {
psa[0][i] = psa[0][i - 1] + a[0][i];
}
for (let i = 1; i < R; i++) {
psa[i][0] = psa[i - 1][0] + a[i][0];
}
for (let i = 1; i < R; i++) {
for (let j = 1; j < C; j++) {
psa[i][j] = psa[i - 1][j] + psa[i][j - 1] - psa[i - 1][j - 1] + a[i][j];
}
}
return psa;
}
[[10, 30, 60],[5, 15, 35],[2, 6, 10]]
[[10, 20, 30], [15, 30, 50], [17, 34, 56]]
[[10, 30, 60], [15, 45, 95], [17, 51, 107]]
None of the above
Question 8
How do you calculate the sum of elements in a subarray using the prefix sum array?
By directly summing the elements of the subarray
By multiplying the prefix sum values at the left and right boundaries
By subtracting the prefix sum value of the left boundary index from the prefix sum value of the right boundary index
By dividing the prefix sum values at the left and right boundaries
There are 8 questions to complete.