Array Insertion at Beginning in DSA Rust - Time & Space Complexity
We want to understand how long it takes to add an item at the start of an array.
Specifically, how the time grows as the array gets bigger.
Analyze the time complexity of the following code snippet.
fn insert_at_beginning(arr: &mut Vec<i32>, value: i32) {
arr.insert(0, value);
}
This code inserts a new value at the start of a dynamic array (vector).
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Shifting all existing elements one position to the right.
- How many times: Once for each element already in the array.
When the array has more items, more elements must move to make space at the start.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 moves |
| 100 | About 100 moves |
| 1000 | About 1000 moves |
Pattern observation: The number of moves grows directly with the array size.
Time Complexity: O(n)
This means the time to insert at the start grows linearly with the number of elements.
[X] Wrong: "Inserting at the beginning is always fast, like adding at the end."
[OK] Correct: Because all existing elements must move to make space, unlike adding at the end which is usually quick.
Knowing this helps you explain why some data structures are better for front insertions, showing your understanding of efficiency.
"What if we used a linked list instead of an array? How would the time complexity change?"
Practice
insert(0, value)?Solution
Step 1: Understand
In Rust, callinginsert(0, value)behaviorinsert(0, value)on a vector adds the value at index 0.Step 2: Effect on existing elements
All existing elements shift one position to the right to make space for the new element.Final Answer:
The element is added at the start, and all other elements shift right by one position. -> Option BQuick Check:
Insert at index 0 shifts elements right [OK]
- Thinking insert replaces the first element
- Assuming insert adds at the end
- Believing array size stays the same
vec?Solution
Step 1: Identify correct method for insertion at index
Rust vectors useinsert(index, value)to add elements at a specific position.Step 2: Check each option
vec.insert(0, 10); usesinsert(0, 10), which inserts 10 at the start. vec.push(10); adds at the end. vec[0] = 10; replaces the first element without insertion. vec.add(0, 10); is invalid syntax.Final Answer:
vec.insert(0, 10); -> Option AQuick Check:
Useinsert(0, value)to add at start [OK]
- Using push() adds at end, not beginning
- Assigning vec[0] replaces element, no insertion
- Using non-existent method add()
let mut vec = vec![2, 3, 4];
vec.insert(0, 1);
println!("{:?}", vec);Solution
Step 1: Initial vector state
The vector starts as [2, 3, 4].Step 2: Insert 1 at index 0
Usingvec.insert(0, 1)adds 1 at the start, shifting others right.Final Answer:
[1, 2, 3, 4] -> Option DQuick Check:
Insert at start shifts elements right [OK]
- Expecting element added at end
- Thinking insert replaces element
- Ignoring vector shift after insert
let mut vec = vec![1, 2, 3]; vec.insert(5, 0);
Solution
Step 1: Analyze the insert index
The vector has length 3, valid insert indices are 0 to 3. Index 5 is out of bounds, causing runtime panic.Step 2: Confirm syntax and mutability
Parameters follow correct order insert(index, value); vector is declared mutable.Final Answer:
The index 5 is out of bounds for the vector of length 3. -> Option AQuick Check:
Insert index must be within vector bounds [OK]
- Reversing insert parameters
- Using out-of-range index
- Forgetting to declare vector mutable
vec = vec![10, 20, 30]. You want to insert multiple values [1, 2, 3] at the beginning efficiently. Which approach is best in Rust?Solution
Step 1: Analyze inserting multiple elements at start
Inserting one by one at index 0 shifts elements repeatedly, inefficient for multiple inserts.Step 2: Efficient approach
Create a new vector with new elements, then extend it with the original vector to combine efficiently.Final Answer:
Create a new vector with the new elements, then extend it with the original vector. -> Option CQuick Check:
Prepending by new vector + extend is efficient [OK]
- Inserting elements one by one at index 0 (slow)
- Using push then reversing (extra steps)
- Replacing original vector loses data
Ready to prove it?
Pass the test and this pattern moves unknown
