Array Deletion at Beginning in DSA Go - Time & Space Complexity
We want to understand how long it takes to remove the first item from an array.
How does the time needed change as the array gets bigger?
Analyze the time complexity of the following code snippet.
func deleteFirst(arr []int) []int {
for i := 1; i < len(arr); i++ {
arr[i-1] = arr[i]
}
return arr[:len(arr)-1]
}
This code removes the first element by shifting all other elements one step left.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that moves each element one position left.
- How many times: It runs once for each element after the first, so (n - 1) times if n is array size.
As the array gets bigger, the number of moves grows roughly the same as the size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 9 moves |
| 100 | 99 moves |
| 1000 | 999 moves |
Pattern observation: The work grows linearly with the number of elements.
Time Complexity: O(n)
This means the time to delete the first element grows in direct proportion to the array size.
[X] Wrong: "Deleting the first element is always fast and takes constant time."
[OK] Correct: Because all other elements must be moved one step left, the time depends on how many elements are in the array.
Understanding this helps you explain why some data structures are better for certain operations, showing your clear thinking about efficiency.
"What if we changed the array to a linked list? How would the time complexity of deleting the first element change?"
Practice
[10, 20, 30, 40] after deleting the first element at the beginning?Solution
Step 1: Understand deletion at beginning
Deleting the first element removes the element at index 0, which is 10.Step 2: Shift remaining elements left
The remaining elements 20, 30, 40 shift left to fill the gap, resulting in [20, 30, 40].Final Answer:
[20, 30, 40] -> Option CQuick Check:
Remove first element = [20, 30, 40] [OK]
- Removing last element instead of first
- Not shifting elements left after deletion
- Leaving the first element in place
arr?Solution
Step 1: Understand slice slicing syntax
In Go,arr[1:]creates a slice from index 1 to end, effectively removing the first element.Step 2: Check other options
arr[:len(arr)-1]removes last element,append(arr, 0)adds element,arr[0:]returns full slice.Final Answer:
arr = arr[1:] -> Option BQuick Check:
Slice from index 1 removes first element [OK]
- Using arr[:len(arr)-1] deletes last element
- Appending instead of slicing
- Using arr[0:] which does not delete anything
arr := []int{5, 10, 15, 20}
arr = arr[1:]
fmt.Println(arr)Solution
Step 1: Initial array elements
arr starts as [5, 10, 15, 20].Step 2: Slice from index 1
arr = arr[1:] removes first element 5, resulting in [10, 15, 20].Final Answer:
[10 15 20] -> Option DQuick Check:
arr[1:] drops first element [OK]
- Printing original array without slicing
- Confusing arr[:1] with arr[1:]
- Expecting output with commas instead of spaces
arr. What is the error?
arr := []int{1, 2, 3, 4}
arr = arr[0:len(arr)]
fmt.Println(arr)Solution
Step 1: Analyze slicing range
arr[0:len(arr)] returns the full slice unchanged.Step 2: Check effect on array
Since no elements are removed, arr remains [1, 2, 3, 4].Final Answer:
No deletion happens; array remains unchanged -> Option AQuick Check:
Slicing full range keeps array same [OK]
- Assuming slicing deletes first element
- Expecting runtime error from slicing
- Confusing slicing syntax with deletion
arr := []int{7, 14, 21, 28, 35}, write the Go code to delete the first two elements and print the resulting slice.Solution
Step 1: Understand deletion of first two elements
To delete first two elements, slice from index 2 to end: arr[2:].Step 2: Verify other options
arr[:len(arr)-2] removes last two elements, arr[1:] removes only first element, append(arr[:2], arr[2:]...) reconstructs the original slice.Final Answer:
arr = arr[2:] fmt.Println(arr) -> Option AQuick Check:
Slice from index 2 removes first two elements [OK]
- Removing last elements instead of first
- Removing only one element instead of two
- Using append unnecessarily for simple slicing
Ready to prove it?
Pass the test and this pattern moves unknown
