📖 Scenario: You are managing a list of daily tasks. Sometimes, you need to add a new task right in the middle of your current list to keep things balanced.
🎯 Goal: Build a program that inserts a new task into the middle of an existing list of tasks.
📋 What You'll Learn
Create a slice of strings called tasks with exactly these values: "Laundry", "Grocery Shopping", "Homework", "Call Mom"
Create a string variable called newTask and set it to "Exercise"
Insert newTask into the middle index of tasks
Print the updated tasks slice
💡 Why This Matters
🌍 Real World
In real life, task lists or schedules often need updates where new items are added in the middle to keep priorities balanced.
💼 Career
Understanding how to manipulate slices and insert elements is essential for software developers working with dynamic data collections.
Progress0 / 4 steps
1
Create the initial tasks list
Create a slice of strings called tasks with these exact values: "Laundry", "Grocery Shopping", "Homework", "Call Mom"
DSA Go
Hint
Use tasks := []string{...} to create the slice with the exact tasks.
2
Add the new task variable
Create a string variable called newTask and set it to "Exercise"
DSA Go
Hint
Use newTask := "Exercise" to create the new task.
3
Insert the new task in the middle
Insert newTask into the middle index of tasks. Use the middle index calculated as len(tasks) / 2. Update tasks to include newTask at this position.
DSA Go
Hint
Calculate middle index with len(tasks) / 2. Use append to insert newTask at that index.
4
Print the updated tasks list
Print the tasks slice to show the updated list after insertion.
DSA Go
Hint
Use fmt.Println(tasks) to print the updated list.
Practice
(1/5)
1. What is the middle index of an array with 7 elements when inserting a new element?
easy
A. 3
B. 4
C. 2
D. 5
Solution
Step 1: Calculate middle index for odd length array
For an array of length 7, middle index = length / 2 = 7 / 2 = 3 (integer division).
Step 2: Confirm insertion position
The new element should be inserted at index 3 to be in the middle.
Final Answer:
3 -> Option A
Quick Check:
Middle index = 7/2 = 3 [OK]
Hint: Middle index = length divided by 2 (integer division) [OK]
Common Mistakes:
Using length/2 + 1 for middle index
Confusing zero-based index with count
Using length/2 as float instead of integer
2. Which Go code snippet correctly inserts an element `x` at the middle index of slice `arr`?
easy
A. arr = append(arr, x)
B. arr = append(arr[:len(arr)/2], append([]int{x}, arr[len(arr)/2:]...)...)
C. arr = append(arr[:len(arr)/2], x, arr[len(arr)/2:])
D. arr = append(arr[:len(arr)/2+1], append([]int{x}, arr[len(arr)/2:]...)...)
Solution
Step 1: Understand slice insertion in Go
To insert at middle, split slice at middle index, append new element slice, then append remaining slice.
Step 2: Check syntax correctness
arr = append(arr[:len(arr)/2], append([]int{x}, arr[len(arr)/2:]...)...) correctly uses append with slices and variadic syntax to insert element at middle.
Final Answer:
arr = append(arr[:len(arr)/2], append([]int{x}, arr[len(arr)/2:]...)...) -> Option B