Slicing operations in Go - Time & Space Complexity
When working with slices in Go, it's important to know how the time to create or manipulate slices changes as the slice size grows.
We want to understand how slicing operations affect the program's speed as the input size increases.
Analyze the time complexity of the following code snippet.
func sliceExample(arr []int) []int {
newSlice := arr[2:5]
return newSlice
}
This code creates a new slice from an existing slice by selecting elements from index 2 up to 4.
- Primary operation: Creating a new slice header pointing to part of the original array.
- How many times: No loops or element copying happens here; it happens once.
Creating a slice header is a simple operation that does not copy elements, so the time stays almost the same no matter how big the original slice is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Constant time, just setting pointers |
| 100 | Still constant time, no element copying |
| 1000 | Still constant time, no element copying |
Pattern observation: The time to create a slice header does not grow with the size of the slice.
Time Complexity: O(1)
This means creating a slice header is very fast and takes the same time regardless of the slice size.
[X] Wrong: "Creating a slice always copies all elements, so it takes longer for bigger slices."
[OK] Correct: In Go, slicing just creates a new view (header) on the existing array without copying elements, so it is very fast.
Understanding how slicing works helps you write efficient code and answer questions about data handling speed in Go.
"What if we copied the slice elements into a new slice instead of just slicing? How would the time complexity change?"
Practice
arr[2:5] do?Solution
Step 1: Understand slice indexing in Go
Go slices include the start index but exclude the end index.Step 2: Apply to
This means elements at indexes 2, 3, and 4 are included, but not index 5.arr[2:5]Final Answer:
Extracts elements from index 2 up to but not including index 5 -> Option BQuick Check:
Slice = arr[2:5] includes indexes 2,3,4 [OK]
- Including the end index element
- Confusing start and end indexes
- Thinking slice includes elements before start
arr from the beginning up to index 4 (excluding 4), omitting the start index?Solution
Step 1: Recall slice syntax for start omitted
Omitting the start index means slicing from the beginning (index 0).Step 2: Check the options
arr[:4]omits the start index (defaults to 0), slicing up to but excluding index 4.arr[0:4]explicitly includes the start index.Final Answer:
arr[:4] -> Option AQuick Check:
Start omitted means from 0 [OK]
- Using arr[1:4] which skips the first element
- Using arr[4:] which slices from index 4 to end
- Confusing start and end indexes
arr := []int{10, 20, 30, 40, 50}
slice := arr[1:4]
fmt.Println(slice)Solution
Step 1: Identify slice indexes
The slicearr[1:4]includes elements at indexes 1, 2, and 3.Step 2: Extract elements from arr
arr[1] = 20, arr[2] = 30, arr[3] = 40, so the slice is [20 30 40].Final Answer:
[20 30 40] -> Option DQuick Check:
Slice arr[1:4] = [20 30 40] [OK]
- Including arr[4] which is 50
- Starting slice at index 0 instead of 1
- Printing the whole array instead of slice
arr := []int{1, 2, 3, 4, 5}
slice := arr[3:2]
fmt.Println(slice)
What is the issue here?Solution
Step 1: Check slice indexes
The slicearr[3:2]has start index 3 and end index 2.Step 2: Understand Go slice rules
In Go, the start index must be less than or equal to the end index; otherwise, it causes a runtime panic.Final Answer:
The start index is greater than the end index, causing a runtime panic -> Option CQuick Check:
Start > end in slice causes panic [OK]
- Assuming slice is empty without error
- Thinking slice syntax is invalid at compile time
- Confusing start and end indexes
nums := []int{5, 10, 15, 20, 25, 30}, which expression produces a new slice containing only the last three elements?Solution
Step 1: Identify last three elements indexes
The last three elements are at indexes 3, 4, and 5 (20, 25, 30).Step 2: Use slice syntax to get from index 3 to end
Usingnums[3:]slices from index 3 to the end, giving [20 25 30].Final Answer:
nums[3:] -> Option AQuick Check:
nums[3:] = last three elements [OK]
- Using nums[:3] which gives first three elements
- Using nums[2:5] which excludes last element
- Using nums[4:] which gives only last two elements
Ready to prove it?
Pass the test and this pattern moves unknown
