Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the prefix sum array with the first element of the input array.
DSA Javascript
const prefixSum = [[1]];Attempts:
3 left
💡 Hint
Common Mistakes
Using arr[1] instead of arr[0]
Starting with 0 instead of the first element
✗ Incorrect
The prefix sum array starts with the first element of the input array to accumulate sums correctly.
2fill in blank
mediumComplete the code to calculate the prefix sum for the current index inside the loop.
DSA Javascript
for (let i = 1; i < arr.length; i++) { prefixSum[i] = prefixSum[i - 1] [1] arr[i]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition
Using multiplication or division which are incorrect here
✗ Incorrect
Each prefix sum is the sum of the previous prefix sum and the current element.
3fill in blank
hardFix the error in the code to correctly compute the prefix sum array.
DSA Javascript
const prefixSum = []; prefixSum[0] = arr[0]; for (let i = 1; [1]; i++) { prefixSum[i] = prefixSum[i - 1] + arr[i]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= arr.length causes out-of-bounds error
Using < arr.length - 1 skips last element
✗ Incorrect
The loop should run while i is less than arr.length to avoid accessing undefined elements.
4fill in blank
hardFill both blanks to create a function that returns the prefix sum array for a given input array.
DSA Javascript
function prefixSumArray(arr) {
const prefixSum = [arr[0]];
for (let i = 1; i [1] arr.length; i++) {
prefixSum[i] = prefixSum[i - 1] [2] arr[i];
}
return prefixSum;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in loop condition causing errors
Using subtraction instead of addition
✗ Incorrect
The loop runs while i is less than arr.length, and sums previous prefix sum with current element.
5fill in blank
hardFill all three blanks to create a function that returns the prefix sum array using map and reduce methods.
DSA Javascript
function prefixSumArray(arr) {
let sum = 0;
return arr.map(([1]) => {
sum = sum [2] [3];
return sum;
});
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter name
Using wrong operator
Using index instead of element
✗ Incorrect
The map function uses 'num' as parameter, adds it to sum, and returns the cumulative sum.
