Array Deletion at End in DSA Rust - Time & Space Complexity
We want to understand how fast it is to remove an item from the end of an array.
This helps us know how the work grows when the array gets bigger.
Analyze the time complexity of the following code snippet.
fn delete_last_element(arr: &mut Vec<i32>) {
if !arr.is_empty() {
arr.pop();
}
}
This code removes the last element from a dynamic array if it is not empty.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Removing the last element using
pop(). - How many times: This operation happens once per call.
Removing the last element takes the same amount of work no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The work stays constant as the array grows.
Time Complexity: O(1)
This means removing the last item takes the same small amount of time no matter how big the array is.
[X] Wrong: "Removing the last element takes longer if the array is bigger because it has to shift all elements."
[OK] Correct: When deleting at the end, no elements need to move. Only the last slot is removed, so the time stays the same.
Knowing that deleting at the end is very fast helps you choose the right data structure for tasks where you add and remove items often.
"What if we changed to deleting the first element instead? How would the time complexity change?"
Practice
Vec using pop()?Solution
Step 1: Understand
Thepop()behaviorpop()method removes the last element from the vector and returns it wrapped inSome(value)if the vector is not empty.Step 2: Confirm it does not affect other elements
Only the last element is removed; the rest of the vector remains unchanged.Final Answer:
The last element is removed and returned asSome(value). -> Option AQuick Check:
pop()removes last element [OK]
- Thinking pop() removes the first element
- Assuming pop() clears the entire vector
- Believing pop() makes vector immutable
v in Rust?Solution
Step 1: Recall Rust vector methods
The standard method to remove the last element from a vector ispop().Step 2: Check method names
Methods likeremove_last(),delete_end(), anderase_last()do not exist in Rust'sVecAPI.Final Answer:
v.pop(); -> Option DQuick Check:
Correct method to remove last element is pop() [OK]
- Using non-existent methods like remove_last()
- Confusing pop() with remove() which removes by index
- Trying to use erase_last() or delete_end() which are invalid
let mut v = vec![10, 20, 30];
let x = v.pop();
println!("{:?}", v);
println!("{:?}", x);Solution
Step 1: Understand
Thepop()effect on vectorpop()removes the last element, which is 30, and returnsSome(30).Step 2: Check vector after pop
After removal, the vector contains [10, 20].Final Answer:
[10, 20] Some(30) -> Option CQuick Check:
pop() removes last element 30, vector left with [10, 20] [OK]
- Expecting pop() to remove first element
- Assuming pop() returns None when vector is not empty
- Confusing vector contents after pop
let v = vec![1, 2, 3]; v.pop();
Solution
Step 1: Check vector mutability
The vectorvis declared immutable withlet. To modify it, it must be mutable.Step 2: Understand pop() requirements
Thepop()method requires a mutable vector to remove the last element.Final Answer:
Cannot call pop() on an immutable vector. -> Option BQuick Check:
pop() needs mutable vector [OK]
- Trying to pop from immutable vector
- Thinking pop() removes first element
- Assuming pop() works on empty vector without error
v with elements [5, 10, 15, 20], write Rust code to remove the last two elements and print the resulting vector. What will be printed?Solution
Step 1: Remove last element twice using pop()
Callingpop()twice removes 20 then 15, leaving [5, 10].Step 2: Print the vector after removals
Printingvnow shows [5, 10].Final Answer:
[5, 10] -> Option AQuick Check:
Two pops remove last two elements [OK]
- Removing only one element instead of two
- Confusing pop() with remove(index)
- Expecting vector to be empty after two pops
Ready to prove it?
Pass the test and this pattern moves unknown
