Array declaration in Go - Time & Space Complexity
When we declare an array in Go, it is important to understand how this affects the program's speed as the array size changes.
We want to know how the time to create an array grows when the array gets bigger.
Analyze the time complexity of the following code snippet.
package main
func main() {
var numbers [100]int
for i := 0; i < len(numbers); i++ {
numbers[i] = i * 2
}
}
This code declares an array of 100 integers and fills it with values by looping through each position.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that assigns values to each element in the array.
- How many times: Exactly once for each element, so 100 times in this example.
As the array size grows, the number of assignments grows at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments |
| 100 | 100 assignments |
| 1000 | 1000 assignments |
Pattern observation: The work grows directly with the size of the array; doubling the array doubles the work.
Time Complexity: O(n)
This means the time to fill the array grows in a straight line with the number of elements.
[X] Wrong: "Declaring an array is instant and does not depend on its size."
[OK] Correct: While declaration reserves space quickly, initializing or filling the array requires time proportional to its size.
Understanding how array operations scale helps you explain your code choices clearly and shows you know how programs handle data efficiently.
"What if we replaced the array with a slice that grows dynamically? How would the time complexity change?"
Practice
var arr [3]intSolution
Step 1: Understand the syntax
This declares a fixed-size array namedvar arr [3]intarrwith 3 elements, each of typeint.Step 2: Recognize default initialization
In Go, arrays are initialized with zero values for their type, so all 3 integers start as 0.Final Answer:
An array of 3 integers initialized with zero values -> Option BQuick Check:
Array declaration = fixed size + type + zero values [OK]
- Confusing array with slice declaration
- Thinking it creates a pointer or map
- Assuming uninitialized values are random
Solution
Step 1: Check Go array declaration syntax
Arrays are declared withvar name [size]type. Sovar arr [4]stringis correct.Step 2: Analyze other options
var arr []string{4} uses slice syntax incorrectly. arr := [4]string{} is valid short declaration syntax in Go. var arr string[4] reverses size and type, which is invalid.Final Answer:
var arr [4]string -> Option AQuick Check:
Array syntax = var name [size]type [OK]
- Using slice syntax instead of array syntax
- Placing size after type
- Missing var keyword or wrong initialization
package main
import "fmt"
func main() {
var nums = [3]int{10, 20, 30}
fmt.Println(nums[1])
}Solution
Step 1: Understand array initialization
The arraynumshas 3 integers: 10 at index 0, 20 at index 1, and 30 at index 2.Step 2: Access element at index 1
Printingnums[1]outputs the second element, which is 20.Final Answer:
20 -> Option DQuick Check:
Array index starts at 0, nums[1] = 20 [OK]
- Confusing index 1 with index 0
- Expecting output 10 or 30 instead of 20
- Thinking array is zero-indexed but printing wrong element
var arr [3]int = {1, 2, 3}Solution
Step 1: Review array initialization syntax
In Go, when initializing an array with values, the correct syntax isvar arr = [3]int{1, 2, 3}orarr := [3]int{1, 2, 3}.Step 2: Identify the error
The code usesvar arr [3]int = {1, 2, 3}which misses the array type on the right side before the braces. This causes a syntax error.Final Answer:
Array initialization syntax is incorrect -> Option AQuick Check:
Array init needs type before braces [OK]
- Omitting array type on right side during initialization
- Using wrong braces or brackets
- Trying to assign without proper syntax
Solution
Step 1: Understand 2D array declaration
A 2D array with 2 rows and 3 columns is declared as[2][3]int. Both dimensions must be specified.Step 2: Check initialization syntax
matrix := [2][3]int{{1,2,3},{4,5,6}} uses short declaration with:=and correctly initializes the 2D array with two rows of three integers each.Step 3: Analyze other options
var matrix [2][3]int = [2][3]int{{1,2,3},{4,5,6}} is correct syntax but uses explicitvarwith type on both sides (valid but less common). var matrix [][]int = {{1,2,3},{4,5,6}} is a slice of slices, not an array. var matrix [3][2]int = {{1,2},{3,4},{5,6}} has dimensions reversed (3 rows, 2 columns).Final Answer:
matrix := [2][3]int{{1,2,3},{4,5,6}} -> Option CQuick Check:
2D array = [rows][cols]type with nested braces [OK]
- Confusing slices with arrays
- Swapping row and column sizes
- Missing nested braces for 2D arrays
Ready to prove it?
Pass the test and this pattern moves unknown
