It means adding a new element at the very start of an array, shifting all existing elements one position to the right.
Click to reveal answer
beginner
Why do we need to shift elements when inserting at the beginning of an array?
Because arrays have fixed positions, to insert at the start, all existing elements must move right to make space for the new element.
Click to reveal answer
intermediate
What is the time complexity of inserting an element at the beginning of an array?
The time complexity is O(n), where n is the number of elements, because all elements must be shifted one place to the right.
Click to reveal answer
beginner
Show the Go code snippet to insert an element at the beginning of an array (slice).
arr = append([]int{newElement}, arr...) // Creates a new slice with newElement first, then appends the old slice
Click to reveal answer
intermediate
What happens if the array is full and you try to insert at the beginning?
In Go slices, the underlying array may resize automatically. But in fixed-size arrays, you cannot insert without overwriting or resizing.
Click to reveal answer
What must happen to existing elements when inserting at the beginning of an array?
AThey get deleted
BThey shift one position to the left
CThey shift one position to the right
DThey stay in the same place
✗ Incorrect
To insert at the start, existing elements move right to make space.
What is the time complexity of inserting at the beginning of an array?
AO(n)
BO(log n)
CO(1)
DO(n^2)
✗ Incorrect
All elements must shift, so time grows linearly with array size.
In Go, which code inserts an element '5' at the beginning of slice 'arr'?
Aarr = append([]int{5}, arr...)
Barr = append(arr, 5)
Carr[0] = 5
Darr = arr[1:]
✗ Incorrect
Appending a new slice with 5 first, then the old slice inserts at beginning.
If an array is fixed size and full, what happens when inserting at the beginning?
AInsertion overwrites last element
BInsertion is not possible without resizing
CArray resizes automatically
DArray elements shift left
✗ Incorrect
Fixed arrays cannot resize; insertion requires resizing or overwriting.
Why is inserting at the beginning slower than at the end in arrays?
ABecause arrays are unordered
BBecause arrays are linked lists
CBecause memory is fragmented
DBecause shifting elements is needed
✗ Incorrect
Shifting all elements to the right takes time, making it slower.
Explain how to insert an element at the beginning of an array and what happens to existing elements.
Think about making space for the new element.
You got /3 concepts.
Describe the Go code approach to insert an element at the start of a slice.
Use append with two slices combined.
You got /3 concepts.
Practice
(1/5)
1. What happens when you insert an element at the beginning of an array in Go?
easy
A. The array size decreases by one.
B. The new element replaces the first element without shifting others.
C. All existing elements shift one position to the right to make space.
D. The new element is added at the end of the array.
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 C
Quick Check:
Insertion at start shifts elements right = A [OK]
Hint: Insertion at start shifts all elements right by one [OK]
Common Mistakes:
Thinking new element replaces first without shifting
Assuming array size changes automatically
Adding element at the end instead of beginning
2. Which of the following Go code snippets correctly inserts an element `x` at the beginning of a slice `s`?
easy
A. s = s[1:]
B. s = append(s, x)
C. s[0] = x
D. s = append([]int{x}, s...)
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 D
Quick Check:
Prepend with append([]int{x}, s...) = A [OK]
Hint: Use append with new slice and ... to insert at start [OK]
Common Mistakes:
Using append(s, x) adds at end, not start
Replacing element instead of inserting
Slicing removes elements instead of adding
3. What is the output of this Go code?
package main
import "fmt"
func main() {
s := []int{2, 3, 4}
s = append([]int{1}, s...)
fmt.Println(s)
}
medium
A. [1 2 3 4]
B. [2 3 4 1]
C. [1 3 4]
D. [2 3 4]
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 A
Quick Check:
Prepending 1 to [2 3 4] = [1 2 3 4] [OK]
Hint: append([]int{x}, s...) prepends x to slice s [OK]
Common Mistakes:
Thinking append adds at end by default
Ignoring ... operator to expand slice
Confusing slice contents after append
4. Identify the error in this Go code that tries to insert 0 at the beginning of slice `s`:
package main
import "fmt"
func main() {
s := []int{1, 2, 3}
s[0] = 0
fmt.Println(s)
}
medium
A. It replaces the first element instead of inserting a new one.
B. It causes a runtime panic due to index out of range.
C. It appends 0 at the end instead of the beginning.
D. It creates a new slice with 0 at the start.
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 A
Quick Check:
Assignment replaces element, does not insert [OK]
Hint: Assignment replaces element; use append to insert [OK]
Common Mistakes:
Thinking s[0] = 0 inserts element
Expecting slice length to increase
Confusing replace with insert
5. Given a slice `s := []int{3, 4, 5}`, write Go code to insert the numbers 1 and 2 at the beginning so that the final slice is `[1 2 3 4 5]`. Which code snippet achieves this correctly?
hard
A. s[0], s[1] = 1, 2
B. s = append([]int{1, 2}, s...)
C. s = append(s[:0], 1, 2)
D. s = append(s, 1, 2)
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 B
Quick Check:
Prepend multiple elements with append([]int{...}, s...) [OK]
Hint: Use append with slice of new elements and ... to prepend [OK]
Common Mistakes:
Appending instead of prepending
Overwriting elements without shifting
Misusing slice syntax causing data loss
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.