Challenge - 5 Problems
Array Access and Update Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Array Update at Index
What is the output of the following Rust code that updates an array element at a specific index?
DSA Rust
fn main() {
let mut arr = [10, 20, 30, 40, 50];
arr[2] = 99;
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Remember that arrays in Rust are fixed size and mutable if declared with 'mut'.
✗ Incorrect
The code changes the element at index 2 (third element) from 30 to 99. The rest remain unchanged.
❓ Predict Output
intermediateResult of Out-of-Bounds Array Access
What happens when the following Rust code tries to access an array element out of bounds?
DSA Rust
fn main() {
let arr = [1, 2, 3];
println!("{}", arr[5]);
}Attempts:
2 left
💡 Hint
Rust checks array bounds at runtime and panics if index is invalid.
✗ Incorrect
Accessing arr[5] is out of bounds since arr has length 3. Rust panics at runtime with an index out of bounds error.
❓ Predict Output
advancedOutput After Multiple Array Updates
What is the output of this Rust program after multiple updates to the array elements?
DSA Rust
fn main() {
let mut arr = [5, 10, 15, 20];
arr[0] += 5;
arr[3] -= 10;
arr[1] = arr[2] * 2;
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Apply each update step by step and track changes carefully.
✗ Incorrect
arr[0] becomes 5+5=10, arr[3] becomes 20-10=10, arr[1] becomes arr[2]*2=15*2=30.
🔧 Debug
advancedIdentify the Error in Array Update
What error does this Rust code produce when trying to update an array element?
DSA Rust
fn main() {
let arr = [1, 2, 3];
arr[1] = 10;
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Check if the array is declared mutable before updating.
✗ Incorrect
The array is declared without 'mut', so it cannot be changed. Rust compiler prevents this with an error.
🧠 Conceptual
expertUnderstanding Rust Array Indexing and Mutability
Which statement about Rust arrays and indexing is TRUE?
Attempts:
2 left
💡 Hint
Think about mutability and safety in Rust arrays.
✗ Incorrect
Rust arrays have fixed size and require 'mut' to update elements. Out-of-bounds access causes runtime panic, not compile error. Negative indices are not allowed.
