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 C#
int[] prefixSum = new int[arr.Length]; prefixSum[0] = arr[[1]];
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or curly braces instead of square brackets for indexing.
Trying to assign the whole array instead of a single element.
✗ Incorrect
We use square brackets [ ] to access array elements by index in C#.
2fill in blank
mediumComplete the for loop header to iterate from the second element to the end of the array.
DSA C#
for (int i = [1]; i < arr.Length; i++) { prefixSum[i] = prefixSum[i - 1] + arr[i]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting the loop at 0 which overwrites the first prefix sum.
Starting at arr.Length which is out of bounds.
✗ Incorrect
We start from index 1 because prefixSum[0] is already set.
3fill in blank
hardFix the error in the prefix sum calculation inside the loop.
DSA C#
prefixSum[i] = prefixSum[i [1] 1] + arr[i];
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of - which causes index out of range or wrong sums.
Using * or / which are not correct for indexing.
✗ Incorrect
We subtract 1 to get the previous prefix sum element.
4fill in blank
hardFill both blanks to compute the sum of elements from index left to right using the prefix sum array.
DSA C#
int rangeSum = prefixSum[[1]] - prefixSum[[2]];
Attempts:
3 left
💡 Hint
Common Mistakes
Using left instead of left - 1 for the second index.
Using right - 1 instead of right for the first index.
✗ Incorrect
Sum from left to right is prefixSum[right] - prefixSum[left - 1].
5fill in blank
hardFill all three blanks to safely compute the sum from left to right, handling the case when left is zero.
DSA C#
int rangeSum = (left == 0) ? prefixSum[[1]] : prefixSum[[2]] - prefixSum[[3]];
Attempts:
3 left
💡 Hint
Common Mistakes
Not handling left == 0 case causing index errors.
Using left instead of left - 1 in subtraction.
✗ Incorrect
If left is zero, sum is prefixSum[right]. Otherwise, subtract prefixSum[left - 1].
