Challenge - 5 Problems
Array Insertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting at the beginning?
Consider the Rust code below that inserts elements at the beginning of a vector. What will be printed?
DSA Rust
fn main() {
let mut v = vec![2, 3, 4];
v.insert(0, 1);
println!("{:?}", v);
}Attempts:
2 left
💡 Hint
Remember that insert at index 0 adds the element at the start, shifting others right.
✗ Incorrect
The insert(0, 1) call places 1 at the start, pushing existing elements right. So the vector becomes [1, 2, 3, 4].
❓ Predict Output
intermediateWhat is the vector after multiple insertions at the beginning?
Given the Rust code below, what is the final vector printed?
DSA Rust
fn main() {
let mut v = vec![3, 4];
v.insert(0, 2);
v.insert(0, 1);
println!("{:?}", v);
}Attempts:
2 left
💡 Hint
Each insert at index 0 adds the element at the front, pushing others right.
✗ Incorrect
First insert(0, 2) makes [2, 3, 4], then insert(0, 1) makes [1, 2, 3, 4].
🔧 Debug
advancedWhy does this Rust code panic when inserting at the beginning?
Look at the Rust code below. It panics at runtime. What is the cause?
DSA Rust
fn main() {
let mut v = Vec::new();
v.insert(1, 10);
println!("{:?}", v);
}Attempts:
2 left
💡 Hint
Check the valid index range for insert in an empty vector.
✗ Incorrect
Inserting at index 1 in an empty vector is invalid because valid indices are only 0 (start). This causes a panic.
🧠 Conceptual
advancedWhat is the time complexity of inserting at the beginning of a Rust vector?
When inserting an element at the start (index 0) of a Rust vector, what is the time complexity and why?
Attempts:
2 left
💡 Hint
Think about what happens to existing elements when inserting at the front.
✗ Incorrect
Inserting at index 0 requires shifting all elements one position to the right, which takes linear time proportional to the vector size.
🚀 Application
expertAfter these insertions, what is the vector's state?
Given the Rust code below, what is the final vector printed?
DSA Rust
fn main() {
let mut v = vec![5, 6];
v.insert(0, 3);
v.insert(1, 4);
v.insert(0, 2);
println!("{:?}", v);
}Attempts:
2 left
💡 Hint
Track each insertion step carefully by index and vector state.
✗ Incorrect
Start: [5,6]
insert(0,3) -> [3,5,6]
insert(1,4) -> [3,4,5,6]
insert(0,2) -> [2,3,4,5,6]
