Challenge - 5 Problems
Array Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of reversing an array slice in Rust
What is the output of this Rust code that reverses a slice of an array?
DSA Rust
fn main() {
let arr = [1, 2, 3, 4, 5];
let slice = &arr[1..4];
let reversed: Vec<_> = slice.iter().rev().cloned().collect();
println!("{:?}", reversed);
}Attempts:
2 left
💡 Hint
Remember that slice [1..4] includes elements at indices 1, 2, and 3.
✗ Incorrect
The slice includes elements 2, 3, and 4. Reversing them results in 4, 3, 2.
🧠 Conceptual
intermediateUnderstanding in-place array reversal
Which of the following best describes what happens during an in-place reversal of an array?
Attempts:
2 left
💡 Hint
In-place means no extra array is created.
✗ Incorrect
In-place reversal swaps elements from the ends moving inward, using constant extra space.
🔧 Debug
advancedIdentify the error in this Rust array reversal code
What error does this Rust code produce when trying to reverse an array in place?
DSA Rust
fn main() {
let arr = [1, 2, 3, 4, 5];
let mut i = 0;
let mut j = arr.len() - 1;
while i < j {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i += 1;
j -= 1;
}
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Check the mutability of the array variable.
✗ Incorrect
The array `arr` is immutable by default; trying to assign to its elements causes a compile-time error.
❓ Predict Output
advancedOutput of reversing a vector using built-in method
What is the output of this Rust code that reverses a vector using the built-in reverse method?
DSA Rust
fn main() {
let mut v = vec![10, 20, 30, 40];
v.reverse();
println!("{:?}", v);
}Attempts:
2 left
💡 Hint
The reverse method changes the vector in place.
✗ Incorrect
The reverse method reverses the vector elements in place, so the output is the vector reversed.
🚀 Application
expertResult of reversing a linked list represented as a vector of nodes
Given a singly linked list represented as a vector of nodes where each node is a tuple (value, next_index), what is the resulting list after reversing it? The list is:
[(1, 1), (2, 2), (3, -1)]
where -1 means end of list. Reversing means changing next_index pointers to reverse the list order.
Attempts:
2 left
💡 Hint
Reversing changes the direction of next pointers so the last node points to the previous one.
✗ Incorrect
Reversing the list changes next indices so node with value 3 points to node 2, node 2 points to node 1, and node 1 points to -1 (end).
