Challenge - 5 Problems
Prefix Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Prefix Sum Array Construction
What is the output of the prefix sum array after processing the input array?
DSA Java
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]; } System.out.println(java.util.Arrays.toString(prefixSum));
Attempts:
2 left
💡 Hint
Remember each element in prefix sum array is sum of all elements up to that index.
✗ Incorrect
The prefix sum array stores cumulative sums. Starting with 2, then 2+4=6, then 6+6=12, then 12+8=20.
❓ Predict Output
intermediatePrefix Sum Array Query Result
Given the prefix sum array, what is the sum of elements from index 1 to 3 (inclusive) in the original array?
DSA Java
int[] prefixSum = {3, 7, 12, 18, 25}; int left = 1, right = 3; int result = prefixSum[right] - prefixSum[left - 1]; System.out.println(result);
Attempts:
2 left
💡 Hint
Use prefixSum[right] - prefixSum[left - 1] to get sum between left and right.
✗ Incorrect
Sum from index 1 to 3 is prefixSum[3] - prefixSum[0] = 18 - 3 = 15.
🧠 Conceptual
advancedUnderstanding Prefix Sum Array Initialization
Why do we initialize the first element of the prefix sum array with the first element of the input array?
Attempts:
2 left
💡 Hint
Think about what prefix sum means at the start of the array.
✗ Incorrect
Prefix sum at index 0 equals the first element because it sums all elements up to index 0, which is just that element.
❓ Predict Output
advancedOutput of Prefix Sum with Negative Numbers
What is the prefix sum array output for the input array containing negative numbers?
DSA Java
int[] arr = {5, -3, 7, -2, 4}; 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]; } System.out.println(java.util.Arrays.toString(prefixSum));
Attempts:
2 left
💡 Hint
Add each element to the sum of all previous elements.
✗ Incorrect
Prefix sums: 5, 5+(-3)=2, 2+7=9, 9+(-2)=7, 7+4=11.
🚀 Application
expertUsing Prefix Sum to Count Subarrays with Given Sum
Given an integer array and a target sum, which approach correctly uses prefix sums to count the number of subarrays that sum to the target?
DSA Java
int countSubarraysWithSum(int[] arr, int target) { int count = 0; int[] prefixSum = new int[arr.length + 1]; prefixSum[0] = 0; for (int i = 1; i <= arr.length; i++) { prefixSum[i] = prefixSum[i - 1] + arr[i - 1]; } java.util.Map<Integer, Integer> map = new java.util.HashMap<>(); for (int sum : prefixSum) { if (map.containsKey(sum - target)) { count += map.get(sum - target); } map.put(sum, map.getOrDefault(sum, 0) + 1); } return count; }
Attempts:
2 left
💡 Hint
Think about how prefix sums and hashmap can find subarrays efficiently.
✗ Incorrect
The hashmap stores counts of prefix sums. For each prefix sum, checking if (current sum - target) exists counts subarrays summing to target.
