Challenge - 5 Problems
Array Rotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Left Rotation by 2 Positions
What is the output of the following Rust code that rotates an array to the left by 2 positions?
DSA Rust
fn main() {
let mut arr = [1, 2, 3, 4, 5];
arr.rotate_left(2);
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Think about moving the first two elements to the end of the array.
✗ Incorrect
The rotate_left(2) method moves the first two elements to the end, shifting the rest to the front.
❓ Predict Output
intermediateOutput of Right Rotation by 3 Positions
What is the output of this Rust code that rotates an array to the right by 3 positions?
DSA Rust
fn main() {
let mut arr = [10, 20, 30, 40, 50, 60];
arr.rotate_right(3);
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Right rotation by 3 moves last three elements to the front.
✗ Incorrect
rotate_right(3) moves the last three elements to the front, shifting others right.
🧠 Conceptual
advancedUnderstanding Time Complexity of Array Rotation
What is the time complexity of rotating an array of size n by k positions using Rust's built-in rotate_left or rotate_right methods?
Attempts:
2 left
💡 Hint
Consider how many elements need to be moved or swapped.
✗ Incorrect
Rust's rotate methods perform rotation in linear time proportional to array size n.
🔧 Debug
advancedIdentify the Error in Manual Array Rotation Code
What error will this Rust code produce when trying to rotate an array left by 1 position manually?
DSA Rust
fn main() {
let mut arr = [1, 2, 3, 4];
let first = arr[0];
for i in 0..arr.len() - 1 {
arr[i] = arr[i + 1];
}
arr[arr.len() - 1] = first;
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Check the loop range and array indexing carefully.
✗ Incorrect
The loop accesses arr[i + 1] when i reaches last index, causing out of bounds panic.
🚀 Application
expertResult of Multiple Rotations on an Array
Given the Rust code below, what is the final state of the array after all rotations?
DSA Rust
fn main() {
let mut arr = [7, 14, 21, 28, 35];
arr.rotate_left(1);
arr.rotate_right(2);
arr.rotate_left(3);
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Apply each rotation step by step on paper.
✗ Incorrect
After first rotate_left(1): [14, 21, 28, 35, 7]
Then rotate_right(2): [35, 7, 14, 21, 28]
Finally rotate_left(3): [21, 28, 35, 7, 14]
