Challenge - 5 Problems
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting element in the middle of an array
What is the output of the following Go code that inserts the value 99 into the middle of the array?
DSA Go
package main import "fmt" func main() { arr := []int{10, 20, 30, 40, 50} mid := len(arr) / 2 arr = append(arr[:mid], append([]int{99}, arr[mid:]...)...) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Remember that the middle index is calculated as length divided by 2, and insertion shifts elements to the right.
✗ Incorrect
The middle index of the array with length 5 is 2. Inserting 99 at index 2 shifts elements from index 2 onwards to the right. The resulting array is [10 20 99 30 40 50].
❓ Predict Output
intermediateResult of inserting element in even-length array middle
What will be printed after inserting 77 in the middle of this even-length array?
DSA Go
package main import "fmt" func main() { arr := []int{1, 2, 3, 4, 5, 6} mid := len(arr) / 2 arr = append(arr[:mid], append([]int{77}, arr[mid:]...)...) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
For even length, middle index is length/2, so insertion happens before the element at that index.
✗ Incorrect
The array length is 6, so mid = 3. Inserting 77 at index 3 shifts elements from index 3 onwards to the right. Result: [1 2 3 77 4 5 6].
🔧 Debug
advancedIdentify the error in array insertion code
What error does this Go code produce when trying to insert 10 in the middle of the array?
DSA Go
package main import "fmt" func main() { arr := []int{5, 6, 7} mid := len(arr) / 2 arr = append(arr[:mid], 10, arr[mid:]...) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
The syntax append(slice[:mid], elem, slice[mid:]...) is the standard way to insert a single element in Go.
✗ Incorrect
There is no error. The code correctly inserts 10 at index 1 (mid=1 for length 3) using append(arr[:mid], 10, arr[mid:]...), which unpacks arr[mid:] into individual elements. It prints [5 10 6 7].
❓ Predict Output
advancedOutput after multiple middle insertions in array
What is the final array after inserting 100 and then 200 at the middle index sequentially?
DSA Go
package main import "fmt" func main() { arr := []int{1, 2, 3, 4} mid := len(arr) / 2 arr = append(arr[:mid], append([]int{100}, arr[mid:]...)...) mid = len(arr) / 2 arr = append(arr[:mid], append([]int{200}, arr[mid:]...)...) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Remember to recalculate the middle index after the first insertion.
✗ Incorrect
After first insertion (mid=2), array is [1 2 100 3 4]. Length 5, mid=2. Inserting 200 at index 2: [1 2] + [200] + [100 3 4] = [1 2 200 100 3 4].
🧠 Conceptual
expertEffect of middle insertion on array length and indices
If you insert an element at the middle index of an array of length n, which of the following statements is always true?
Attempts:
2 left
💡 Hint
Think about what happens when you insert an element in the middle of a list.
✗ Incorrect
Inserting an element adds one new element, increasing length by 1. Elements at and after the insertion index move one position to the right.
