Accessing array elements in Go - Time & Space Complexity
When we access elements in an array, we want to know how long it takes as the array grows.
We ask: Does it take longer to get an element if the array is bigger?
Analyze the time complexity of the following code snippet.
package main
import "fmt"
func main() {
arr := [5]int{10, 20, 30, 40, 50}
fmt.Println(arr[2])
}
This code creates an array and prints the element at index 2.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing a single element by index.
- How many times: Exactly once in this example.
Accessing an element by index takes the same time no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same even if the array grows larger.
Time Complexity: O(1)
This means accessing any element takes a fixed amount of time, no matter the array size.
[X] Wrong: "Accessing an element takes longer if the array is bigger."
[OK] Correct: Arrays store elements in contiguous memory, so the computer can jump directly to any element quickly.
Knowing that array access is fast helps you choose the right data structure and explain your choices clearly in interviews.
"What if we used a linked list instead of an array? How would the time complexity of accessing an element change?"
Practice
Solution
Step 1: Understand Go array indexing
In Go, arrays start indexing from 0, meaning the first element is at index 0.Step 2: Confirm the first element's index
The first element is always accessed with index 0, not 1 or any other number.Final Answer:
0 -> Option DQuick Check:
First element index = 0 [OK]
- Thinking indexing starts at 1
- Assuming negative indexes are valid
- Believing index depends on array size
arr in Go?Solution
Step 1: Recall Go array indexing syntax
Go uses square brackets with zero-based indexing to access elements, so the third element is at index 2.Step 2: Identify correct syntax
arr[2] accesses the third element; arr[3] accesses the fourth, arr(2) and arr{2} are invalid syntax.Final Answer:
arr[2] -> Option BQuick Check:
Third element = arr[2] [OK]
- Using parentheses instead of brackets
- Using 1-based index
- Using curly braces for access
arr := [4]int{10, 20, 30, 40}
fmt.Println(arr[1])Solution
Step 1: Identify the array elements and their indexes
The array is [10, 20, 30, 40], indexed 0 to 3. Index 1 corresponds to the second element, 20.Step 2: Determine the printed value
arr[1] accesses the element 20, so fmt.Println prints 20.Final Answer:
20 -> Option CQuick Check:
arr[1] = 20 [OK]
- Printing arr[0] instead
- Confusing index 1 with 2
- Expecting output as 30 or 40
arr := [3]int{1, 2, 3}
fmt.Println(arr[3])Solution
Step 1: Check array size and valid indexes
The array has size 3, so valid indexes are 0, 1, and 2 only.Step 2: Identify the error in accessing arr[3]
Accessing arr[3] tries to get the fourth element, which does not exist, causing an out-of-bounds runtime error.Final Answer:
Index 3 is out of bounds for the array -> Option AQuick Check:
Valid indexes: 0 to 2 only [OK]
- Using index equal to array length
- Thinking fmt.Println can't print elements
- Assuming array size auto-expands
arr := [5]int{5, 10, 15, 20, 25}, which code snippet correctly sums the first three elements?Solution
Step 1: Identify the first three elements
The first three elements are at indexes 0, 1, and 2, which are 5, 10, and 15 respectively.Step 2: Check which option sums these elements
sum := arr[0] + arr[1] + arr[2] sums arr[0], arr[1], and arr[2], which is correct. Other options include elements beyond the first three.Final Answer:
sum := arr[0] + arr[1] + arr[2] -> Option AQuick Check:
Sum first three = arr[0]+arr[1]+arr[2] [OK]
- Starting sum at index 1
- Skipping elements
- Including elements beyond first three
Ready to prove it?
Pass the test and this pattern moves unknown
