Array Access and Update at Index in DSA Rust - Time & Space Complexity
We want to understand how fast we can get or change a value in an array at a certain position.
The question is: how does the time to access or update an element change as the array grows?
Analyze the time complexity of the following code snippet.
fn access_and_update(arr: &mut [i32], index: usize, new_value: i32) {
let _ = arr[index]; // Access element at index
arr[index] = new_value; // Update element at index
}
This code accesses an element at a given index and then updates it with a new value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Direct access and update at a single index in the array.
- How many times: Exactly once each for access and update, no loops or recursion.
Accessing or updating an element at a specific index takes the same amount of time no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2 (1 access + 1 update) |
| 100 | 2 (1 access + 1 update) |
| 1000 | 2 (1 access + 1 update) |
Pattern observation: The number of operations stays the same regardless of array size.
Time Complexity: O(1)
This means accessing or updating an element at any index takes a constant amount of time, no matter how large the array is.
[X] Wrong: "Accessing an element takes longer if the array is bigger because it has to look through all elements."
[OK] Correct: Arrays allow direct access by index, so the computer jumps straight to the spot without checking others.
Knowing that array access and update are constant time helps you understand why arrays are great for quick lookups and changes by position.
"What if we had to search for a value instead of knowing the index? How would the time complexity change?"
Practice
arr[2] do in Rust when arr is an array?Solution
Step 1: Understand array indexing
In Rust, arrays use zero-based indexing, soarr[2]accesses the third element.Step 2: Identify the operation
The expression retrieves the value stored at index 2 without modifying the array.Final Answer:
Accesses the element at index 2 of the array -> Option CQuick Check:
arr[2] means element at index 2 [OK]
- Thinking indexing starts at 1
- Confusing access with modification
- Assuming it returns array length
arr to 10 in Rust?Solution
Step 1: Recall Rust array update syntax
In Rust, to update an element, use square brackets with the index and assign a new value.Step 2: Check each option
Onlyarr[1] = 10;uses correct square bracket indexing and assignment syntax.Final Answer:
arr[1] = 10; -> Option AQuick Check:
Use square brackets for indexing and assignment [OK]
- Using parentheses () instead of brackets
- Using curly braces {} for indexing
- Using arrow operator -> incorrectly
fn main() {
let mut arr = [5, 10, 15, 20];
arr[2] = 25;
println!("{:?}", arr);
}Solution
Step 1: Analyze array initialization and update
The arrayarrstarts as [5, 10, 15, 20]. The statementarr[2] = 25;changes the element at index 2 (third element) from 15 to 25.Step 2: Understand the print output
Theprintln!("{:?}", arr);prints the entire array showing the updated value.Final Answer:
[5, 10, 25, 20] -> Option DQuick Check:
Updated index 2 to 25, array prints updated [OK]
- Thinking index 2 is second element
- Assuming original array prints without update
- Expecting compilation error due to mutability
fn main() {
let arr = [1, 2, 3];
arr[1] = 5;
println!("{:?}", arr);
}Solution
Step 1: Check array mutability
The arrayarris declared withoutmut, so it is immutable and cannot be changed.Step 2: Identify assignment error
The linearr[1] = 5;tries to modify an immutable array element, causing a compile-time error.Final Answer:
Cannot assign to element of immutable array -> Option AQuick Check:
Immutable arrays cannot be updated [OK]
- Ignoring mut keyword for mutability
- Assuming index out of bounds error
- Thinking syntax error causes failure
arr of length 4 initialized as [2, 4, 6, 8], which code snippet correctly doubles the value at index 3 and then prints the updated array?Solution
Step 1: Identify correct index and operation
Index 3 is the fourth element (value 8). Doubling means multiplying by 2, soarr[3] = arr[3] * 2;is correct.Step 2: Check syntax and print statement
arr[3] = arr[3] * 2;println!("{:?}", arr);uses correct syntax with semicolon and prints the updated array properly.Final Answer:
arr[3] = arr[3] * 2; println!("{:?}", arr); -> Option BQuick Check:
Multiply element at index 3 by 2, then print [OK]
- Using index 4 which is out of bounds
- Forgetting semicolon after assignment
- Adding 2 instead of multiplying by 2
Ready to prove it?
Pass the test and this pattern moves unknown
