Bird
Raised Fist0
Goprogramming~20 mins

Common slice operations in Go - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Slice Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this slice append operation?
Consider the following Go code snippet. What will be printed?
Go
package main
import "fmt"
func main() {
    s := []int{1, 2, 3}
    s = append(s, 4, 5)
    fmt.Println(s)
}
A[4 5]
B[1 2 3]
C[1 2 3 4 5]
DCompilation error
Attempts:
2 left
💡 Hint
Remember that append adds elements to the end of the slice.
❓ Predict Output
intermediate
2:00remaining
What does this slice slicing produce?
What will be the output of this Go program?
Go
package main
import "fmt"
func main() {
    s := []string{"a", "b", "c", "d", "e"}
    fmt.Println(s[1:4])
}
Aruntime error: index out of range
B["a" "b" "c"]
C["c" "d" "e"]
D["b" "c" "d"]
Attempts:
2 left
💡 Hint
Remember slice[start:end] includes start but excludes end.
❓ Predict Output
advanced
2:00remaining
What is the output of this slice capacity and length code?
What will this Go program print?
Go
package main
import "fmt"
func main() {
    s := make([]int, 3, 5)
    fmt.Println(len(s), cap(s))
    s = s[:5]
    fmt.Println(len(s), cap(s))
}
A
3 5
3 5
B
3 5
5 5
C
3 3
5 5
Druntime error: slice bounds out of range
Attempts:
2 left
💡 Hint
Length is the number of elements, capacity is the underlying array size.
❓ Predict Output
advanced
2:00remaining
What happens when you remove an element from a slice?
What will be printed by this Go program?
Go
package main
import "fmt"
func main() {
    s := []int{10, 20, 30, 40, 50}
    i := 2
    s = append(s[:i], s[i+1:]...)
    fmt.Println(s)
}
A[10 20 40 50]
Bruntime error: slice bounds out of range
C[10 20 30 50]
D[10 20 30 40 50]
Attempts:
2 left
💡 Hint
Appending slices can be used to remove elements.
🧠 Conceptual
expert
2:00remaining
What error does this slice operation cause?
What error will this Go code produce when run?
Go
package main
func main() {
    s := []int{1, 2, 3}
    s = s[:4]
}
Aruntime error: slice bounds out of range
Bcompilation error: invalid slice index
Cno error, slice extended with zero values
Dpanic: index out of range
Attempts:
2 left
💡 Hint
You cannot extend a slice beyond its capacity.

Practice

(1/5)
1. What does the slice expression mySlice[2:5] do in Go?
easy
A. It returns a new slice containing elements from index 2 up to, but not including, index 5.
B. It returns elements from index 2 to index 5, including index 5.
C. It removes elements from index 2 to 5 from the original slice.
D. It appends elements from index 2 to 5 to the original slice.

Solution

  1. Step 1: Understand slice syntax

    The slice expression mySlice[2:5] extracts elements starting at index 2 up to, but not including, index 5. Go slices use half-open intervals, so the element at index 5 is not included in the new slice.
  2. Final Answer:

    It returns a new slice containing elements from index 2 up to, but not including, index 5. -> Option A
  3. Quick Check:

    Slice[start:end] excludes end index [OK]
Hint: Remember slice end index is excluded in Go slices [OK]
Common Mistakes:
  • Including the end index element in the slice
  • Thinking slice removes elements from original
  • Confusing append with slicing
2. Which of the following is the correct way to append an element 5 to a slice nums in Go?
easy
A. append(nums, 5)
B. nums += 5
C. nums.append(5)
D. nums = append(nums, 5)

Solution

  1. Step 1: Recall append function usage

    In Go, append returns a new slice with the element added, so you must assign it back. nums = append(nums, 5) correctly assigns the result back to nums. append(nums, 5) ignores the returned slice. nums.append(5) uses invalid method syntax. nums += 5 is invalid for slices.
  2. Final Answer:

    nums = append(nums, 5) -> Option D
  3. Quick Check:

    append returns new slice, assign it [OK]
Hint: Always assign append result back to slice variable [OK]
Common Mistakes:
  • Not assigning append result back
  • Using method syntax like nums.append()
  • Trying to add with += operator
3. What is the output of this Go code?
nums := []int{1, 2, 3, 4, 5}
slice := nums[1:4]
slice[0] = 10
fmt.Println(nums)
medium
A. [1 10 3 4 5]
B. [1 2 10 4 5]
C. [10 2 3 4 5]
D. [1 2 3 4 5]

Solution

  1. Step 1: Understand slice referencing

    The slice slice := nums[1:4] references the original array elements at indexes 1, 2, and 3. Changing slice[0] to 10 changes nums[1] because slices share the underlying array. Output: [1 10 3 4 5]
  2. Final Answer:

    [1 10 3 4 5] -> Option A
  3. Quick Check:

    Slices share array, modifying slice changes original [OK]
Hint: Changing slice elements changes original array too [OK]
Common Mistakes:
  • Thinking slice is a copy, not a reference
  • Modifying wrong index in slice
  • Confusing slice length with capacity
4. Identify the error in this Go code snippet:
nums := []int{1, 2, 3}
newSlice := nums[1:4]
fmt.Println(newSlice)
medium
A. Cannot print a slice directly with fmt.Println.
B. Syntax error in slice declaration.
C. Slicing beyond the slice length causes a runtime panic.
D. Missing import for fmt package.

Solution

  1. Step 1: Check slice bounds

    The original slice nums has length 3, so valid indices are 0 to 2. Attempting nums[1:4] tries to access index 3 which is out of range, causing a runtime panic.
  2. Final Answer:

    Slicing beyond the slice length causes a runtime panic. -> Option C
  3. Quick Check:

    Slice end index must be ≤ length [OK]
Hint: Slice end index cannot exceed slice length [OK]
Common Mistakes:
  • Assuming slice auto-expands
  • Confusing compile error with runtime panic
  • Ignoring slice length when slicing
5. Given a slice data := []int{2, 4, 6, 8, 10, 12}, how can you create a new slice containing only the even numbers greater than 6 using slicing and append?
hard
A. newSlice := data[2:5]
B. newSlice := append([]int{}, data[3:]...)
C. newSlice := append(data[:3], data[4:]...)
D. newSlice := data[3:len(data)]

Solution

  1. Step 1: Identify elements > 6

    Elements greater than 6 are 8, 10, 12 at indexes 3, 4, 5. Using data[3:] gets these elements. To create a new slice (not referencing original), append to empty slice: newSlice := append([]int{}, data[3:]...).
  2. Final Answer:

    newSlice := append([]int{}, data[3:]...) -> Option B
  3. Quick Check:

    Slice and append to copy subset [OK]
Hint: Use append to copy slice subset safely [OK]
Common Mistakes:
  • Using slice without append copies reference
  • Incorrect slice indexes for desired elements
  • Trying to remove elements by slicing incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard