Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the middle index of the array.
DSA Go
arr := []int{1, 2, 3, 4, 5}
middle := len(arr) [1] 2
fmt.Println(middle)Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '/' causes incorrect index calculation.
Using '*' or '-' will not give the middle index.
✗ Incorrect
The middle index is found by dividing the length of the array by 2 using the '/' operator.
2fill in blank
mediumComplete the code to insert a new element at the middle index of the array.
DSA Go
arr := []int{1, 2, 4, 5}
middle := len(arr) / 2
arr = append(arr[:[1]], append([]int{3}, arr[middle:]...)...)
fmt.Println(arr)Attempts:
3 left
💡 Hint
Common Mistakes
Using len(arr) slices the whole array, causing duplication.
Using 0 inserts at the start, not the middle.
Using middle + 1 inserts after the middle.
✗ Incorrect
We slice the array up to the middle index to insert the new element at that position.
3fill in blank
hardFix the error in the code to correctly insert an element at the middle index.
DSA Go
arr := []int{10, 20, 40, 50}
middle := len(arr) / 2
arr = append(arr[:middle], append([]int{30}, arr[[1]:]...)...)
fmt.Println(arr)Attempts:
3 left
💡 Hint
Common Mistakes
Using middle + 1 causes the new element to be inserted after the middle.
Using 0 or len(arr) slices the wrong part of the array.
✗ Incorrect
The slice should start from the middle index to insert the new element correctly.
4fill in blank
hardFill both blanks to create a function that inserts a value at the middle index of an integer slice.
DSA Go
func insertMiddle(arr []int, val int) []int {
middle := len(arr) [1] 2
return append(arr[:middle], append([]int{val}, arr[[2]:]...)...)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '/' for middle calculation.
Slicing from len(arr) instead of middle.
✗ Incorrect
Divide length by 2 to find middle, then slice from middle to insert the value.
5fill in blank
hardFill both blanks to insert a value at the middle index and print the updated array.
DSA Go
arr := []int{100, 200, 400, 500}
middle := len(arr) [1] 2
arr = append(arr[:middle], append([]int[2], arr[middle:]...)...)
fmt.Println(arr)Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '/' for middle calculation.
Inserting value without curly braces causing type errors.
Using '+' instead of ':' for slicing.
✗ Incorrect
Divide length by 2 for middle, insert {300} at middle, slice arr from middle onwards using ':'.
