Challenge - 5 Problems
Prefix Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of the prefix sum array after processing?
Given the input array
[2, 4, 6, 8], what will be the prefix sum array printed by the code below?DSA C#
int[] arr = {2, 4, 6, 8}; int[] prefixSum = new int[arr.Length]; prefixSum[0] = arr[0]; for (int i = 1; i < arr.Length; i++) { prefixSum[i] = prefixSum[i - 1] + arr[i]; } for (int i = 0; i < prefixSum.Length; i++) { Console.Write(prefixSum[i] + " "); }
Attempts:
2 left
💡 Hint
Remember, each prefix sum element is the sum of all elements up to that index.
✗ Incorrect
The prefix sum array is built by adding the current element to the sum of all previous elements. So, prefixSum[0] = 2, prefixSum[1] = 2+4=6, prefixSum[2] = 6+6=12, prefixSum[3] = 12+8=20.
🧠 Conceptual
intermediateWhat does the prefix sum array help to calculate efficiently?
Which of the following is the main use of a prefix sum array?
Attempts:
2 left
💡 Hint
Think about how sums of subarrays can be found faster using prefix sums.
✗ Incorrect
Prefix sum arrays store cumulative sums so that the sum of any subarray can be found in constant time by subtracting prefix sums.
❓ Predict Output
advancedWhat is the output after updating the prefix sum array with a new element?
Given the prefix sum array
[3, 7, 12, 18] for the original array, what will be the prefix sum array after adding a new element 5 to the original array and updating the prefix sums accordingly?DSA C#
int[] prefixSum = {3, 7, 12, 18}; int newElement = 5; int[] newPrefixSum = new int[prefixSum.Length + 1]; for (int i = 0; i < prefixSum.Length; i++) { newPrefixSum[i] = prefixSum[i]; } newPrefixSum[newPrefixSum.Length - 1] = newPrefixSum[newPrefixSum.Length - 2] + newElement; for (int i = 0; i < newPrefixSum.Length; i++) { Console.Write(newPrefixSum[i] + " "); }
Attempts:
2 left
💡 Hint
The new prefix sum is the last prefix sum plus the new element.
✗ Incorrect
The last prefix sum is 18, adding 5 gives 23 as the new last element in the prefix sum array.
🔧 Debug
advancedWhat error does this prefix sum code produce?
Consider the following code snippet to create a prefix sum array. What error will it produce when run?
DSA C#
int[] arr = {1, 2, 3}; int[] prefixSum = new int[arr.Length]; for (int i = 1; i < arr.Length; i++) { prefixSum[i] = prefixSum[i - 1] + arr[i]; } Console.WriteLine(string.Join(",", prefixSum));
Attempts:
2 left
💡 Hint
Check the loop boundary conditions and array indexing.
✗ Incorrect
The loop runs with i <= arr.Length, which tries to access prefixSum[arr.Length], an invalid index causing IndexOutOfRangeException.
🚀 Application
expertHow many subarray sums can be calculated in O(1) time using prefix sums for an array of length 5?
Using a prefix sum array, how many different subarray sums can be calculated in constant time for an array of length 5?
Attempts:
2 left
💡 Hint
Count all possible subarrays in an array of length n.
✗ Incorrect
Number of subarrays in an array of length n is n*(n+1)/2. For n=5, it's 5*6/2=15.
