Array Insertion at Beginning in DSA Go - Time & Space Complexity
We want to understand how long it takes to add an item at the start of an array.
How does the time needed change as the array gets bigger?
Analyze the time complexity of the following code snippet.
package main
func insertAtBeginning(arr []int, val int) []int {
arr = append(arr, 0) // increase slice size by 1
for i := len(arr) - 1; i > 0; i-- {
arr[i] = arr[i-1] // shift elements right
}
arr[0] = val // insert new value at start
return arr
}
This code adds a new value at the start by shifting all existing elements one step to the right.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that shifts elements one by one to the right.
- How many times: It runs once for each element already in the array (n times).
As the array grows, the number of shifts grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 shifts |
| 100 | About 100 shifts |
| 1000 | About 1000 shifts |
Pattern observation: The work grows directly with the number of elements; double the elements, double the work.
Time Complexity: O(n)
This means the time to insert at the beginning grows linearly with the array size.
[X] Wrong: "Inserting at the start is always fast because we just add one item."
[OK] Correct: We must move all existing items to make space, so the bigger the array, the more moves needed.
Knowing this helps you explain why some data structures are better for front insertions than arrays, showing your understanding of efficiency.
"What if we used a linked list instead of an array? How would the time complexity change?"
Practice
Solution
Step 1: Understand array behavior in Go
Arrays have fixed size, so to insert at the beginning, elements must shift right.Step 2: Effect of insertion at beginning
The first element moves to second position, second to third, and so on, making space at index 0.Final Answer:
All existing elements shift one position to the right to make space. -> Option CQuick Check:
Insertion at start shifts elements right = A [OK]
- Thinking new element replaces first without shifting
- Assuming array size changes automatically
- Adding element at the end instead of beginning
Solution
Step 1: Understand slice append syntax
Appending a slice to another uses append(slice1, slice2...), so to add at start, prepend with new slice.Step 2: Analyze each option
s = append([]int{x}, s...) prepends x correctly; B appends at end; C replaces first element; D slices off first element.Final Answer:
s = append([]int{x}, s...) -> Option DQuick Check:
Prepend with append([]int{x}, s...) = A [OK]
- Using append(s, x) adds at end, not start
- Replacing element instead of inserting
- Slicing removes elements instead of adding
package main
import "fmt"
func main() {
s := []int{2, 3, 4}
s = append([]int{1}, s...)
fmt.Println(s)
}Solution
Step 1: Initial slice and append operation
Initial slice s is [2 3 4]. append([]int{1}, s...) adds 1 at start.Step 2: Resulting slice after append
New slice becomes [1 2 3 4] because 1 is prepended before all elements.Final Answer:
[1 2 3 4] -> Option AQuick Check:
Prepending 1 to [2 3 4] = [1 2 3 4] [OK]
- Thinking append adds at end by default
- Ignoring ... operator to expand slice
- Confusing slice contents after append
package main
import "fmt"
func main() {
s := []int{1, 2, 3}
s[0] = 0
fmt.Println(s)
}Solution
Step 1: Analyze the assignment s[0] = 0
This replaces the value at index 0 with 0, not inserting a new element.Step 2: Effect on slice length and contents
Slice length stays same; no shifting occurs; original first element lost.Final Answer:
It replaces the first element instead of inserting a new one. -> Option AQuick Check:
Assignment replaces element, does not insert [OK]
- Thinking s[0] = 0 inserts element
- Expecting slice length to increase
- Confusing replace with insert
Solution
Step 1: Understand slice prepending with append
To add multiple elements at start, create a new slice with them and append original slice after.Step 2: Analyze options
s = append([]int{1, 2}, s...) prepends [1, 2] correctly; A replaces first two elements; B appends at end; C overwrites start without preserving rest.Final Answer:
s = append([]int{1, 2}, s...) -> Option BQuick Check:
Prepend multiple elements with append([]int{...}, s...) [OK]
- Appending instead of prepending
- Overwriting elements without shifting
- Misusing slice syntax causing data loss
Ready to prove it?
Pass the test and this pattern moves unknown
