Zero values in Go - Time & Space Complexity
We want to see how the time to run code changes when using zero values in Go.
Does initializing variables with zero values affect how long the program takes?
Analyze the time complexity of the following code snippet.
package main
func initializeSlice(n int) []int {
s := make([]int, n) // slice with zero values
for i := 0; i < n; i++ {
s[i] = 0 // explicitly setting zero value
}
return s
}
This code creates a slice of size n and sets each element to zero explicitly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for loop that sets each element to zero.
- How many times: It runs n times, once for each element in the slice.
As the slice size n grows, the number of operations grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: Doubling the input doubles the work because each element is set once.
Time Complexity: O(n)
This means the time to run grows directly with the size of the slice.
[X] Wrong: "Since the slice is filled with zero values by default, the loop does nothing extra and takes no time."
[OK] Correct: Even if zero values exist by default, the loop still runs n times doing an assignment, which takes time proportional to n.
Understanding how loops over zero values affect time helps you explain simple but important performance ideas clearly.
"What if we removed the loop that sets zero values explicitly? How would the time complexity change?"
Practice
int variable in Go?Solution
Step 1: Understand zero values for basic types
In Go, each type has a default zero value when not initialized.Step 2: Identify zero value for int type
The zero value forintis the number 0.Final Answer:
0 -> Option CQuick Check:
int zero value = 0 [OK]
- Confusing int zero value with nil
- Thinking zero value is empty string
- Mixing boolean zero value with int
Solution
Step 1: Recall Go variable declaration syntax
Declaring a variable without initialization sets it to its zero value.Step 2: Check each option for correctness
var x int declaresxasintwithout initialization, so it gets zero value 0. Other options assign invalid values for int.Final Answer:
var x int -> Option DQuick Check:
Declare without init = zero value [OK]
- Assigning nil to int
- Assigning string to int
- Assigning boolean to int
package main
import "fmt"
func main() {
var b bool
fmt.Println(b)
}Solution
Step 1: Identify the variable type and initialization
Variablebis declared asboolwithout explicit initialization.Step 2: Recall zero value for bool type
The zero value forboolisfalse, sobis false by default.Final Answer:
false -> Option AQuick Check:
bool zero value = false [OK]
- Expecting true as default
- Thinking nil is valid bool
- Assuming compilation error
package main
func main() {
var s string = nil
}Solution
Step 1: Understand zero value for string type
Zero value for string is an empty string "", not nil.Step 2: Check assignment validity
Assigningnilto a string variable causes a compile-time error becausenilis not a valid string value.Final Answer:
Assigning nil to string causes a compile error -> Option AQuick Check:
string zero value = "" not nil [OK]
- Using nil for string
- Confusing zero value with nil
- Ignoring compile errors
var m map[string]intWhat is the zero value of
m and what must you do before adding elements?Solution
Step 1: Identify zero value of map type
Maps declared without initialization have zero valuenil.Step 2: Understand map usage requirements
You cannot add elements to a nil map; you must initialize it usingmake(map[string]int)before adding keys and values.Final Answer:
mis nil; you must initialize it withmakebefore adding elements -> Option BQuick Check:
Maps zero value = nil; initialize with make [OK]
- Trying to add to nil map
- Assuming map is empty but usable
- Confusing zero value with empty map
Ready to prove it?
Pass the test and this pattern moves unknown
