Challenge - 5 Problems
Array Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of nested loop traversal over array
What is the output of this Rust code that traverses a 2D array with nested loops?
DSA Rust
fn main() {
let arr = [[1, 2], [3, 4]];
for i in 0..arr.len() {
for j in 0..arr[i].len() {
print!("{} ", arr[i][j]);
}
}
}Attempts:
2 left
💡 Hint
Think about how nested loops iterate row by row, then column by column.
✗ Incorrect
The outer loop goes over each row, and the inner loop goes over each element in that row, printing elements in order: 1, 2, then 3, 4.
❓ Predict Output
intermediateOutput of reverse traversal using iterators
What is the output of this Rust code that traverses an array in reverse using iterators?
DSA Rust
fn main() {
let arr = [10, 20, 30, 40];
for &val in arr.iter().rev() {
print!("{} ", val);
}
}Attempts:
2 left
💡 Hint
The rev() method reverses the iterator order.
✗ Incorrect
The iterator goes from the last element to the first, printing 40 30 20 10.
🔧 Debug
advancedIdentify the runtime error in array traversal
What runtime error does this Rust code produce when traversing an array?
DSA Rust
fn main() {
let arr = [5, 10, 15];
for i in 0..=arr.len() {
println!("{}", arr[i]);
}
}Attempts:
2 left
💡 Hint
Check the range used for indexing the array.
✗ Incorrect
The loop uses 0..=arr.len(), which includes arr.len() as an index, causing an out of bounds panic at runtime.
🧠 Conceptual
advancedUnderstanding stride in array traversal
If you want to traverse an array skipping every other element, which Rust iterator method is best to use?
Attempts:
2 left
💡 Hint
Think about how to jump indices by a fixed step.
✗ Incorrect
step_by(2) makes the iterator jump two elements at a time, effectively skipping every other element.
🚀 Application
expertFind the sum of elements at even indices
Given an array of integers, which Rust code correctly calculates the sum of elements at even indices (0, 2, 4, ...)?
Attempts:
2 left
💡 Hint
You need to check indices, not values, to select elements.
✗ Incorrect
Option C uses enumerate to get indices, filters even indices, then sums values. Option C is valid and works correctly for arrays, but may be less clear in some contexts. Option C is the most explicit and safe approach.
