Array Insertion at End in DSA Rust - Time & Space Complexity
We want to understand how long it takes to add an item at the end of an array.
How does the time needed change as the array grows bigger?
Analyze the time complexity of the following code snippet.
fn insert_at_end(arr: &mut Vec<i32>, value: i32) {
arr.push(value);
}
This code adds a new value at the end of a dynamic array (vector).
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding one element at the end of the array.
- How many times: Once per insertion call.
Adding one element usually takes the same short time, no matter the array size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 (simple add) |
| 100 | 1 (simple add) |
| 1000 | 1 (simple add) |
Pattern observation: Most insertions take constant time, but sometimes resizing the array takes longer.
Time Complexity: O(1) amortized
This means adding an element at the end usually takes the same short time, regardless of array size.
[X] Wrong: "Adding at the end always takes longer as the array grows."
[OK] Correct: Most insertions are quick because arrays reserve extra space. Only sometimes, when full, the array resizes, which takes longer.
Knowing this helps you explain why dynamic arrays are efficient for adding items at the end, a common operation in many programs.
"What if we changed the array to a linked list? How would the time complexity of insertion at the end change?"
Practice
Solution
Step 1: Understand array structure
An array is a list of items stored in order, starting from the first to the last.Step 2: Define insertion at the end
Inserting at the end means placing a new item right after the last item, extending the array by one.Final Answer:
Adding a new item after the last existing item in the array -> Option DQuick Check:
Insert at end = Add after last [OK]
- Confusing insertion at end with removal
- Thinking insertion replaces an item
- Mixing insertion with sorting
vec?Solution
Step 1: Identify correct method to add at end
In Rust,pushadds an element to the end of a vector.Step 2: Check other options
insert(0, 10)adds at start,vec[vec.len()] = 10causes error (index out of bounds),append(10)is invalid syntax.Final Answer:
vec.push(10); -> Option AQuick Check:
Use push() to add at end [OK]
- Using insert with wrong index
- Trying to assign to vec[vec.len()] directly
- Using append with a single value
let mut numbers = vec![1, 2, 3];
numbers.push(4);
println!("{:?}", numbers);Solution
Step 1: Understand initial vector
The vectornumbersstarts as [1, 2, 3].Step 2: Apply push operation
numbers.push(4)adds 4 at the end, making it [1, 2, 3, 4].Final Answer:
[1, 2, 3, 4] -> Option CQuick Check:
push(4) adds 4 at end [OK]
- Thinking push adds at start
- Expecting original vector unchanged
- Confusing output format
let numbers = vec![1, 2, 3]; numbers.push(5);
Solution
Step 1: Check vector mutability
The vectornumbersis declared withoutmut, so it is immutable.Step 2: Understand push requirements
pushrequires the vector to be mutable to add elements.Final Answer:
Cannot push because vector is immutable -> Option AQuick Check:
push needs mutable vector [OK]
- Forgetting to add mut keyword
- Assuming push works on immutable vectors
- Misreading error as syntax problem
vec with elements [2, 4, 6], write Rust code to insert the number 8 at the end and then remove the first element. What will be the final vector?Solution
Step 1: Insert 8 at the end
Usingvec.push(8), vector becomes [2, 4, 6, 8].Step 2: Remove the first element
Usingvec.remove(0), removes 2, vector becomes [4, 6, 8].Final Answer:
[4, 6, 8] -> Option BQuick Check:
push then remove first = [4,6,8] [OK]
- Removing last instead of first element
- Not updating vector after push
- Confusing remove index
Ready to prove it?
Pass the test and this pattern moves unknown
