Array initialization in Go - Time & Space Complexity
When we create an array and fill it with values, the computer does some work for each item.
We want to know how this work grows as the array gets bigger.
Analyze the time complexity of the following code snippet.
package main
func initializeArray(n int) []int {
arr := make([]int, n)
for i := 0; i < n; i++ {
arr[i] = i * 2
}
return arr
}
This code creates a slice of size n and fills each position with double its index.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop that sets each element in the slice. - How many times: Exactly
ntimes, once for each element.
As the slice size grows, the number of steps grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments |
| 100 | 100 assignments |
| 1000 | 1000 assignments |
Pattern observation: Doubling the slice size doubles the work needed.
Time Complexity: O(n)
This means the time to fill the slice grows directly with its size.
[X] Wrong: "Initializing an array is instant and does not depend on size."
[OK] Correct: Each element must be set, so bigger arrays take more time.
Understanding how array initialization scales helps you explain performance clearly in interviews.
"What if we used a built-in function that sets all elements to zero instantly? How would the time complexity change?"
Practice
Solution
Step 1: Understand Go array declaration syntax
In Go, arrays are declared with the syntax: var name [size]type.Step 2: Check each option
A: var arr int[5] wrong syntax (type before size). B: arr := []int{5} is a slice, not an array. C: var arr [5]int correct syntax. D: arr := [5]int{1, 2, 3, 4} too few initializers (4 < 5), compilation error.Final Answer:
var arr [5]int -> Option BQuick Check:
Array declaration = var arr [5]int [OK]
- Confusing slices with arrays
- Using wrong syntax like int[5]
- Missing var keyword for array declaration
Solution
Step 1: Identify correct array initialization syntax
Arrays use [size]type{values} syntax. arr := [3]string{"a", "b", "c"} matches this exactly.Step 2: Check other options for errors
arr := []string{"a", "b", "c"} is a slice, not an array. var arr [3]string = {"a", "b", "c"} uses invalid initialization with = and braces. arr := [3]string{"a", "b", c} misses quotes around "c" causing syntax error.Final Answer:
arr := [3]string{"a", "b", "c"} -> Option AQuick Check:
Array init with values = [3]string{"a", "b", "c"} [OK]
- Using slice syntax instead of array
- Missing quotes around string elements
- Incorrect use of = with array literals
package main
import "fmt"
func main() {
arr := [4]int{1, 2}
fmt.Println(arr)
}Solution
Step 1: Understand partial array initialization
When fewer elements are provided than the array size, Go fills remaining elements with zero values.Step 2: Apply to given array
Array size is 4, initialized with 1 and 2. Remaining two elements become 0 by default.Final Answer:
[1 2 0 0] -> Option AQuick Check:
Partial init fills rest with zero [OK]
- Assuming array length matches initializer length
- Expecting error for fewer initial values
- Confusing slices with arrays
var arr [3]int = [3]int{1, 2, 3, 4}Solution
Step 1: Check array size and initializer length
Array declared with size 3 but initializer has 4 elements.Step 2: Understand Go array rules
Go requires initializer length to match array size exactly, else compilation error occurs.Final Answer:
Array size mismatch with initializer -> Option DQuick Check:
Initializer length must equal array size [OK]
- Ignoring extra elements in initializer
- Assuming Go auto-expands array size
- Confusing slices with arrays
Solution
Step 1: Understand array initialization with explicit zeros
To ensure the last three elements are zeros explicitly, include them in the initializer.Step 2: Check each option
B: arr := [3]int{10, 20} fmt.Println(arr) wrong size 3, not 5. A: arr := [5]int{10, 20, 0, 0, 0} fmt.Println(arr) explicitly sets zeros. C: arr := [5]int{10, 20, 30} fmt.Println(arr) third element 30, not zero. D: arr := []int{10, 20} fmt.Println(arr) is a slice, not array.Step 3: Choose the best explicit solution
A clearly shows all elements matching the requirement exactly.Final Answer:
arr := [5]int{10, 20, 0, 0, 0} fmt.Println(arr) -> Option CQuick Check:
Explicit zeros in initializer = arr := [5]int{10, 20, 0, 0, 0} fmt.Println(arr) [OK]
- Assuming missing elements are always zero without clarity
- Using slices instead of arrays
- Adding unintended values in initializer
Ready to prove it?
Pass the test and this pattern moves unknown
