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 Go
prefixSum[0] = [1][0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name like 'nums' or 'input'.
Trying to assign prefixSum[0] to 0 instead of the first element.
✗ Incorrect
The prefix sum array starts with the first element of the input array named 'arr'.
2fill in blank
mediumComplete the code to calculate the prefix sum for the current index i.
DSA Go
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 for prefix sums.
✗ Incorrect
Each prefix sum element is the sum of the previous prefix sum and the current element.
3fill in blank
hardFix the error in the loop condition to iterate over the array correctly.
DSA Go
for i := 1; i [1] len(arr); i++ {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes index out of range error.
Using '>' or '>=' causes the loop to never run.
✗ Incorrect
The loop should run while i is less than the length of the array to avoid out-of-range errors.
4fill in blank
hardFill both blanks to complete the function that returns the prefix sum array.
DSA Go
func prefixSumArray(arr []int) []int {
prefixSum := make([]int, len(arr))
prefixSum[0] = arr[0]
for i := 1; i [1] len(arr); i++ {
prefixSum[i] = prefixSum[i-1] [2] arr[i]
}
return prefixSum
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' in loop condition causing index errors.
Using subtraction instead of addition for prefix sums.
✗ Incorrect
The loop runs while i < len(arr), and prefixSum[i] is the sum of prefixSum[i-1] and arr[i].
5fill in blank
hardFill all three blanks to complete the code that computes prefix sums and prints the result.
DSA Go
func main() {
arr := []int{1, 2, 3, 4, 5}
prefixSum := make([]int, len(arr))
prefixSum[0] = arr[[1]]
for i := 1; i [2] len(arr); i++ {
prefixSum[i] = prefixSum[i-1] [3] arr[i]
}
fmt.Println(prefixSum)
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting with arr[1] instead of arr[0].
Using '<=' in loop causing index errors.
Using '-' instead of '+' for prefix sums.
✗ Incorrect
The first element is arr[0], loop runs while i < len(arr), and prefixSum[i] is sum of prefixSum[i-1] and arr[i].
