Array length in Go - Time & Space Complexity
We want to understand how long it takes to find the length of an array in Go.
Does the time change if the array is bigger or smaller?
Analyze the time complexity of the following code snippet.
package main
import "fmt"
func main() {
arr := [5]int{1, 2, 3, 4, 5}
length := len(arr)
fmt.Println(length)
}
This code creates an array and gets its length using the built-in len function.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the array length property.
- How many times: Exactly once, no loops or traversals.
Getting the length does not depend on how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The operation count stays the same no matter the array size.
Time Complexity: O(1)
This means getting the length of an array takes the same amount of time no matter how big the array is.
[X] Wrong: "Getting the length of an array takes longer if the array is bigger because it counts each item."
[OK] Correct: The length is stored as a property, so Go does not count items each time. It just reads the stored value instantly.
Knowing that array length access is very fast helps you understand what operations are costly and which are not. This skill helps you explain your code clearly in interviews.
"What if we used a slice instead of an array? Would getting the length still be as fast?"
Practice
len() function return when used with an array in Go?Solution
Step 1: Understand the purpose of len() in Go
Thelen()function returns the count of elements in arrays, slices, maps, and strings.Step 2: Apply len() to an array
When used on an array,len()returns the fixed number of elements it holds.Final Answer:
The number of elements in the array -> Option BQuick Check:
len(array) = number of elements [OK]
- Confusing len() with capacity
- Thinking len() returns memory size
- Assuming len() returns last element
arr in Go?Solution
Step 1: Recall Go syntax for length
In Go, the built-in functionlen()is used to get the length of arrays and slices.Step 2: Check each option
Onlylen(arr)is valid syntax; others are invalid or do not exist in Go.Final Answer:
len(arr) -> Option AQuick Check:
Use len() function for array length [OK]
- Using dot notation like arr.length
- Using non-existent functions like length() or size()
- Confusing with other languages' syntax
package main
import "fmt"
func main() {
arr := [4]int{10, 20, 30, 40}
fmt.Println(len(arr))
}Solution
Step 1: Analyze the array declaration
The arrayarris declared with 4 elements: 10, 20, 30, 40.Step 2: Understand len(arr)
Thelen()function returns the number of elements, which is 4.Final Answer:
4 -> Option CQuick Check:
len([4]int{...}) = 4 [OK]
- Counting elements incorrectly
- Thinking len() returns last index (3)
- Expecting runtime error
package main
import "fmt"
func main() {
arr := [3]int{1, 2, 3}
fmt.Println(len(arr))
fmt.Println(arr[3])
}Solution
Step 1: Check array length and indexing
The arrayarrhas length 3, so valid indices are 0, 1, and 2.Step 2: Identify invalid index access
Accessingarr[3]is out of range and causes a runtime panic.Final Answer:
Accessing arr[3] causes an index out of range error -> Option DQuick Check:
Array indices go from 0 to len-1 [OK]
- Assuming len(arr) is invalid
- Thinking fmt.Println can't print arrays
- Confusing array length with capacity
arr1 := [5]int{1, 2, 3, 4, 5}
arr2 := [3]int{10, 20, 30}
Which expression correctly checks if arr2 has fewer elements than arr1?Solution
Step 1: Identify lengths of both arrays
arr1has length 5,arr2has length 3.Step 2: Write correct comparison syntax
Uselen()to get lengths. To check ifarr2is smaller, comparelen(arr2) < len(arr1).Final Answer:
len(arr2) < len(arr1) -> Option AQuick Check:
len(arr2) = 3 < 5 = len(arr1) [OK]
- Using wrong function names like length or size
- Reversing comparison order
- Using dot notation for length
Ready to prove it?
Pass the test and this pattern moves unknown
