Slice and array relationship in Go - Time & Space Complexity
When working with slices and arrays in Go, it's important to understand how operations on them affect performance.
We want to know how the time to perform actions grows as the size of the slice or array changes.
Analyze the time complexity of the following code snippet.
package main
func sumSlice(s []int) int {
total := 0
for _, v := range s {
total += v
}
return total
}
This code sums all elements in a slice by looping through each item once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element of the slice.
- How many times: Exactly once per element, so as many times as the slice length.
As the slice gets bigger, the number of additions grows directly with its size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows in a straight line with the number of elements.
Time Complexity: O(n)
This means the time to sum the slice grows directly with the number of elements.
[X] Wrong: "Accessing elements in a slice is slow because it copies the whole array each time."
[OK] Correct: A slice points to the original array's data, so accessing elements is fast and does not copy the entire array.
Understanding how slices relate to arrays and how operations scale helps you write efficient Go code and explain your reasoning clearly.
"What if we changed the slice to an array parameter instead? How would the time complexity change?"
Practice
Solution
Step 1: Understand slice basics
A slice in Go points to an underlying array and does not copy data.Step 2: Relationship between slice and array
Changes to the slice affect the array and vice versa because they share the same data.Final Answer:
A slice is a view into an array and shares the same data. -> Option BQuick Check:
Slice shares array data = B [OK]
- Thinking slices copy array data
- Believing slices are independent arrays
- Assuming slices have fixed size
arr := [5]int{1,2,3,4,5} that includes elements 2, 3, and 4?Solution
Step 1: Identify slice indices
Slice syntax is arr[start:end], including start but excluding end index.Step 2: Select correct indices for elements 2,3,4
Elements 2,3,4 are at indices 1,2,3, so slice is arr[1:4].Final Answer:
slice := arr[1:4] -> Option AQuick Check:
Slice indices 1 to 4 exclude 4 = D [OK]
- Using wrong start or end index
- Trying to slice beyond array length
- Confusing inclusive/exclusive slice bounds
arr := [3]int{10, 20, 30}
slice := arr[:]
slice[1] = 99
fmt.Println(arr)Solution
Step 1: Understand slice referencing
slice := arr[:] creates a slice referencing the entire array.Step 2: Modify slice element
slice[1] = 99 changes the second element of the slice and underlying array.Step 3: Print array
Printing arr shows the updated array: [10 99 30].Final Answer:
[10 99 30] -> Option CQuick Check:
Slice change affects array = A [OK]
- Assuming array stays unchanged
- Confusing slice copy with reference
- Misreading slice syntax
arr := [4]int{1, 2, 3, 4}
slice := arr[1:3]
slice[2] = 10
fmt.Println(arr)Solution
Step 1: Check slice length
slice := arr[1:3] includes elements at indices 1 and 2, length 2.Step 2: Accessing slice[2]
slice[2] tries to access third element (index 2), which is out of range.Final Answer:
Index out of range error when assigning to slice[2] -> Option DQuick Check:
Slice length 2, index 2 invalid = C [OK]
- Assuming slice length equals array length
- Using invalid slice indices
- Ignoring runtime panics
arr := [5]int{1, 2, 3, 4, 5}, you create two slices:slice1 := arr[1:4]slice2 := arr[2:5]If you set
slice1[1] = 99, what will be the content of slice2?Solution
Step 1: Identify slice1 and slice2 elements
slice1 = arr[1:4] = elements at indices 1,2,3 -> [2,3,4]
slice2 = arr[2:5] = elements at indices 2,3,4 -> [3,4,5]Step 2: Modify slice1[1]
slice1[1] is element at arr index 2 (value 3), changed to 99.
So arr becomes [1,2,99,4,5].Step 3: Check slice2 after modification
slice2 references arr[2], arr[3], arr[4] -> [99,4,5].Final Answer:
[99 4 5] -> Option AQuick Check:
Shared array data means slice2 reflects change = B [OK]
- Mixing up slice indices and values
- Forgetting slices share underlying array
- Assuming slices are independent copies
Ready to prove it?
Pass the test and this pattern moves unknown
