Bird
Raised Fist0
Goprogramming~20 mins

Slice length and capacity 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
Understanding slice length and capacity after slicing
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    s := []int{10, 20, 30, 40, 50}
    t := s[1:3]
    fmt.Println(len(t), cap(t))
}
A2 3
B3 4
C2 4
D3 5
Attempts:
2 left
💡 Hint
Remember: length is the number of elements in the slice, capacity is from the start of the slice to the end of the underlying array.
❓ Predict Output
intermediate
2:00remaining
Effect of append on slice capacity
What will be printed by this Go program?
Go
package main
import "fmt"
func main() {
    s := make([]int, 2, 4)
    s[0], s[1] = 1, 2
    s = append(s, 3)
    fmt.Println(len(s), cap(s))
}
A3 3
B3 4
C4 4
D2 4
Attempts:
2 left
💡 Hint
Appending increases length by 1; capacity stays the same if there is room.
❓ Predict Output
advanced
2:00remaining
Slice capacity after reslicing
What is the output of this Go code?
Go
package main
import "fmt"
func main() {
    s := []int{1, 2, 3, 4, 5}
    t := s[1:4]
    u := t[1:3]
    fmt.Println(len(u), cap(u))
}
A3 4
B2 4
C3 3
D2 3
Attempts:
2 left
💡 Hint
Capacity of a slice is from its start index to the end of the underlying array.
❓ Predict Output
advanced
2:00remaining
Appending beyond capacity creates new array
What will this Go program print?
Go
package main
import "fmt"
func main() {
    s := []int{1, 2, 3}
    t := s[:2]
    t = append(t, 4, 5)
    fmt.Println(len(t), cap(t))
}
A4 6
B4 4
C5 6
D5 4
Attempts:
2 left
💡 Hint
Appending more elements than capacity causes a new underlying array with larger capacity.
❓ Predict Output
expert
2:00remaining
Slice capacity and length with nil slice and append
What is the output of this Go program?
Go
package main
import "fmt"
func main() {
    var s []int
    fmt.Println(len(s), cap(s))
    s = append(s, 1)
    fmt.Println(len(s), cap(s))
}
A
0 0
1 1
B
0 0
1 0
C
0 1
1 1
D
0 0
1 2
Attempts:
2 left
💡 Hint
A nil slice has zero length and capacity. Append allocates new underlying array.

Practice

(1/5)
1. In Go, what does the len() function return when used on a slice?
easy
A. The maximum number of elements the slice can hold
B. The total memory size of the slice in bytes
C. The index of the last element in the slice
D. The number of elements currently in the slice

Solution

  1. Step 1: Understand the purpose of len() on slices

    The len() function returns how many elements are currently stored in the slice.
  2. Step 2: Differentiate from capacity

    Capacity is about how many elements the slice can hold before resizing, not the current count.
  3. Final Answer:

    The number of elements currently in the slice -> Option D
  4. Quick Check:

    len(slice) = current element count [OK]
Hint: Remember: len = current items, cap = max before grow [OK]
Common Mistakes:
  • Confusing length with capacity
  • Thinking len returns memory size
  • Assuming len gives last index
2. Which of the following is the correct way to create a slice with length 3 and capacity 5 in Go?
easy
A. make([]int, 5, 3)
B. make([]int, 3, 5)
C. []int{3, 5}
D. make([]int, 3)

Solution

  1. Step 1: Recall the make function signature for slices

    make([]T, length, capacity) creates a slice with given length and capacity.
  2. Step 2: Match length and capacity values

    To get length 3 and capacity 5, use make([]int, 3, 5).
  3. Final Answer:

    make([]int, 3, 5) -> Option B
  4. Quick Check:

    make([]int, length, capacity) [OK]
Hint: make([]T, len, cap) sets length then capacity [OK]
Common Mistakes:
  • Swapping length and capacity arguments
  • Using slice literal instead of make
  • Omitting capacity when needed
3. What will be the output of this Go code?
nums := make([]int, 2, 4)
nums[0] = 10
nums[1] = 20
fmt.Println(len(nums), cap(nums))
medium
A. 2 2
B. 0 4
C. 2 4
D. 4 4

Solution

  1. Step 1: Analyze slice creation

    make([]int, 2, 4) creates a slice with length 2 and capacity 4.
  2. Step 2: Check length and capacity after assignments

    Assigning values to existing indices does not change length or capacity.
  3. Final Answer:

    2 4 -> Option C
  4. Quick Check:

    len = 2, cap = 4 [OK]
Hint: Length stays same unless you append beyond capacity [OK]
Common Mistakes:
  • Confusing length with capacity
  • Assuming capacity equals length after assignment
  • Thinking length changes when assigning values
4. Consider this code snippet:
arr := []int{1, 2, 3, 4, 5}
slice := arr[1:3]
fmt.Println(len(slice), cap(slice))

What is the correct output?
medium
A. 2 4
B. 2 2
C. 3 4
D. 3 5

Solution

  1. Step 1: Determine length of the slice

    The slice is from index 1 to 3 (exclusive), so length = 3 - 1 = 2.
  2. Step 2: Calculate capacity of the slice

    Capacity is from start index to end of original array: 5 - 1 = 4.
  3. Final Answer:

    2 4 -> Option A
  4. Quick Check:

    len = 2, cap = 4 from slice indices [OK]
Hint: Capacity = original array length - slice start index [OK]
Common Mistakes:
  • Using slice length as capacity
  • Confusing slice end index with capacity
  • Assuming capacity equals original array length
5. You have a slice s := make([]int, 3, 5). You append two elements to it. What will be the length and capacity of s after these appends?
hard
A. Length 5, Capacity 5
B. Length 5, Capacity 10
C. Length 3, Capacity 5
D. Length 5, Capacity 6

Solution

  1. Step 1: Understand initial slice size

    Slice s starts with length 3 and capacity 5.
  2. Step 2: Append two elements within capacity

    Appending two elements increases length to 5, capacity remains 5 because it is not exceeded.
  3. Final Answer:

    Length 5, Capacity 5 -> Option A
  4. Quick Check:

    Appending within capacity keeps capacity same [OK]
Hint: Capacity grows only when length exceeds capacity [OK]
Common Mistakes:
  • Assuming capacity doubles immediately after append
  • Confusing length with capacity after append
  • Thinking capacity increases by 1 per append
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