Array Declaration and Initialization in DSA Go - Time & Space Complexity
We want to understand how long it takes to create and fill an array when we declare it.
How does the time needed change as the array size grows?
Analyze the time complexity of the following code snippet.
package main
func createArray(n int) []int {
arr := make([]int, n)
for i := 0; i < n; i++ {
arr[i] = 0
}
return arr
}
This code creates a slice of size n and sets each element to zero.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that sets each element to zero.
- How many times: Exactly n times, once for each element.
As the array size n grows, the number of steps grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps to set elements |
| 100 | 100 steps to set elements |
| 1000 | 1000 steps to set elements |
Pattern observation: The work grows in a straight line with the size of the array.
Time Complexity: O(n)
This means the time to create and fill the array grows directly with the number of elements.
[X] Wrong: "Creating an array is instant and does not depend on size."
[OK] Correct: Even though memory is allocated quickly, setting each element takes time proportional to the array size.
Understanding how array initialization scales helps you reason about performance when working with large data sets in real projects.
"What if we used a built-in function that initializes the array with a default value instead of a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand array declaration syntax in Go
Arrays in Go are declared with a fixed size inside square brackets, followed by the type, e.g.,var arr [5]int.Step 2: Analyze each option
var arr [5]intcorrectly declares an array of 5 integers.arr := [5]intuses shorthand but lacks the array literal {}, causing a syntax error.var arr []int = make([]int, 5)declares a slice, not an array.arr := []int{5}declares a slice with one element 5, not an array of size 5.Final Answer:
var arr [5]int -> Option BQuick Check:
Array declaration = var arr [5]int [OK]
- Confusing slices with arrays
- Using shorthand without initialization
- Incorrect size placement
Solution
Step 1: Review array initialization syntax
In Go, arrays are initialized with curly braces and values inside, e.g.,[3]int{1, 2, 3}.Step 2: Check each option
var arr = [3]int{1, 2, 3}uses correct syntax.arr := [3]int(1, 2, 3)uses parentheses which is invalid.var arr [3]int = {1, 2, 3}misses the type keyword before braces.arr := []int{1, 2, 3}declares a slice, not an array.Final Answer:
var arr = [3]int{1, 2, 3} -> Option DQuick Check:
Array init with braces = var arr = [3]int{1, 2, 3} [OK]
- Using parentheses instead of braces
- Confusing slices with arrays
- Omitting type in declaration
package main
import "fmt"
func main() {
var arr = [4]int{10, 20}
fmt.Println(arr)
}Solution
Step 1: Understand partial array initialization
When an array is partially initialized, the remaining elements are set to zero values (0 for int).Step 2: Analyze the given array
The array size is 4, but only first two elements are initialized as 10 and 20. The last two elements default to 0.Final Answer:
[10 20 0 0] -> Option AQuick Check:
Partial init fills rest with zeros = [10 20 0 0] [OK]
- Expecting only initialized elements in output
- Thinking partial init causes error
- Confusing slices with arrays
package main
func main() {
var arr [3]int = [3]int{1, 2}
}Solution
Step 1: Check array size and initialization
The array is declared with size 3 but initialized with 2 values. Go allows this and fills the last element with zero.Step 2: Verify syntax and imports
No syntax error exists. Import is not mandatory if no external packages are used. The code compiles fine.Final Answer:
No error, code is correct -> Option AQuick Check:
Partial init allowed, no import needed [OK]
- Assuming size mismatch causes error
- Expecting import for main package
- Confusing slices with arrays
Solution
Step 1: Understand indexed initialization in arrays
Go allows initializing specific elements by index using syntax like{index: value}. Indexing starts at 0.Step 2: Analyze each option
var arr = [5]int{4: 100}initializes the element at index 4 (the last element in a 5-element array) to 100, others default to 0.var arr = [5]int{5: 100}uses index 5 which is out of range.var arr = [5]int{100}initializes only the first element.var arr = [5]int{0: 100}initializes index 0 to 100, not the last.Final Answer:
var arr = [5]int{4: 100} -> Option CQuick Check:
Use index 4 for last element in 5-size array [OK]
- Using out-of-range index
- Initializing first element instead of last
- Omitting index for specific element
Ready to prove it?
Pass the test and this pattern moves unknown
