Challenge - 5 Problems
Array Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after deleting the last element from the array?
Consider the Rust code below that deletes the last element from a vector. What will be printed?
DSA Rust
fn main() {
let mut arr = vec![10, 20, 30, 40, 50];
arr.pop();
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
The pop() method removes the last element from the vector.
✗ Incorrect
The pop() method removes the last element (50) from the vector, so the remaining elements are [10, 20, 30, 40].
❓ Predict Output
intermediateWhat happens if pop() is called on an empty vector?
What will be the output of the following Rust code?
DSA Rust
fn main() {
let mut arr: Vec<i32> = Vec::new();
let result = arr.pop();
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
pop() returns an Option type indicating if an element was removed.
✗ Incorrect
Since the vector is empty, pop() returns None indicating no element was removed.
🔧 Debug
advancedWhy does this code cause a compile error when deleting the last element?
Identify the error in the Rust code below that tries to delete the last element from an array and fix it.
DSA Rust
fn main() {
let arr = [1, 2, 3, 4];
arr.pop();
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
pop() works only on vectors, not fixed-size arrays.
✗ Incorrect
Arrays in Rust have fixed size and do not support pop(). Only vectors support pop() to remove the last element.
🚀 Application
advancedHow to delete the last element and get the updated length?
Given a vector in Rust, which code snippet correctly deletes the last element and prints the updated length?
Attempts:
2 left
💡 Hint
pop() removes last element; len() returns current length.
✗ Incorrect
pop() removes the last element; len() returns the updated number of elements. remove(arr.len()) causes panic because index is out of bounds.
🧠 Conceptual
expertWhat is the time complexity of deleting the last element from a Rust vector?
Consider a Rust vector with n elements. What is the time complexity of deleting the last element using pop()?
Attempts:
2 left
💡 Hint
pop() removes the last element without shifting others.
✗ Incorrect
pop() removes the last element in constant time because no elements need to be shifted.
