Challenge - 5 Problems
Recursion Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Sum of Array Elements
What is the output of the following Rust code that recursively sums elements of an array?
DSA Rust
fn recursive_sum(arr: &[i32], index: usize) -> i32 {
if index == arr.len() {
0
} else {
arr[index] + recursive_sum(arr, index + 1)
}
}
fn main() {
let numbers = [1, 2, 3, 4];
let result = recursive_sum(&numbers, 0);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Think about how the function adds each element starting from index 0 until the end.
✗ Incorrect
The function recursively adds each element starting from index 0. The sum is 1+2+3+4 = 10.
❓ Predict Output
intermediateOutput of Recursive String Reverse
What is the output of this Rust code that recursively reverses a string slice?
DSA Rust
fn reverse_string(s: &str) -> String { if s.is_empty() { String::new() } else { let (first, rest) = s.split_at(1); format!("{}{}", reverse_string(rest), first) } } fn main() { let text = "rust"; let reversed = reverse_string(text); println!("{}", reversed); }
Attempts:
2 left
💡 Hint
The function moves the first character to the end recursively.
✗ Incorrect
The function takes the first character and appends it after reversing the rest, resulting in the reversed string.
🔧 Debug
advancedIdentify the Runtime Error in Recursive Array Search
What runtime error will occur when running this Rust code that searches for a value recursively in an array?
DSA Rust
fn recursive_search(arr: &[i32], target: i32, index: usize) -> bool { if arr[index] == target { true } else { recursive_search(arr, target, index + 1) } } fn main() { let data = [5, 3, 7]; let found = recursive_search(&data, 9, 0); println!("{}", found); }
Attempts:
2 left
💡 Hint
Consider what happens when the index goes beyond the array length.
✗ Incorrect
The function does not check if index is within bounds before accessing arr[index], causing panic when index exceeds array length.
🧠 Conceptual
advancedNumber of Recursive Calls in String Palindrome Check
Given a recursive function that checks if a string is a palindrome by comparing first and last characters and recursing on the substring, how many recursive calls are made for the string "level"?
DSA Rust
fn is_palindrome(s: &str) -> bool { if s.len() <= 1 { true } else if s.chars().next() != s.chars().rev().next() { false } else { is_palindrome(&s[1..s.len()-1]) } } fn main() { let word = "level"; let result = is_palindrome(word); println!("{}", result); }
Attempts:
2 left
💡 Hint
Count how many times the function calls itself before reaching the base case.
✗ Incorrect
The function calls itself 3 times: on "level", then "eve", then "v" (base case).
🚀 Application
expertOutput of Recursive Flattening of Nested Integer Vectors
What is the output of this Rust code that recursively flattens a nested vector of integers into a single vector?
DSA Rust
enum NestedInt {
Int(i32),
List(Vec<NestedInt>),
}
fn flatten(nested: &NestedInt, result: &mut Vec<i32>) {
match nested {
NestedInt::Int(ref value) => result.push(*value),
NestedInt::List(ref list) => {
for item in list {
flatten(item, result);
}
}
}
}
fn main() {
let nested = NestedInt::List(vec![
NestedInt::Int(1),
NestedInt::List(vec![NestedInt::Int(2), NestedInt::Int(3)]),
NestedInt::Int(4),
]);
let mut flat = Vec::new();
flatten(&nested, &mut flat);
println!("{:?}", flat);
}Attempts:
2 left
💡 Hint
The function visits each integer in order and adds it to the result vector.
✗ Incorrect
The function recursively visits each integer in the nested structure and appends it to the result vector in order.
