Challenge - 5 Problems
Recursion Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive vs Loop Factorial
What is the output of this Rust code that calculates factorial using recursion and a loop?
DSA Rust
fn factorial_recursive(n: u32) -> u32 {
if n == 0 { 1 } else { n * factorial_recursive(n - 1) }
}
fn factorial_loop(n: u32) -> u32 {
let mut result = 1;
for i in 1..=n {
result *= i;
}
result
}
fn main() {
let rec = factorial_recursive(5);
let looped = factorial_loop(5);
println!("Recursive: {} Loop: {}", rec, looped);
}Attempts:
2 left
💡 Hint
Both methods calculate factorial of 5, which is 120.
✗ Incorrect
Both recursive and loop methods correctly compute 5! = 120.
🧠 Conceptual
intermediateWhy Recursion Can Express Problems Loops Cannot Easily
Which of these problem types is best expressed using recursion rather than loops?
Attempts:
2 left
💡 Hint
Think about problems with nested or branching structures.
✗ Incorrect
Tree traversal naturally fits recursion because each node can have multiple child nodes, which loops cannot easily handle without extra data structures.
🔧 Debug
advancedIdentify the Error in Recursive Fibonacci
What error will this Rust code produce when calculating fibonacci(5)?
DSA Rust
fn fibonacci(n: u32) -> u32 {
if n == 0 {
0
} else if n == 1 {
1
} else {
fibonacci(n - 1) + fibonacci(n - 2)
}
}
fn main() {
println!("{}", fibonacci(5));
}Attempts:
2 left
💡 Hint
Trace fibonacci(5) calls carefully.
✗ Incorrect
The code correctly computes fibonacci(5) = 8 by summing fibonacci(4) and fibonacci(3).
❓ Predict Output
advancedOutput of Recursive Directory Size Calculation
Given this simplified Rust code to calculate total size of files in nested directories recursively, what is the output?
DSA Rust
struct File {
size: u32,
}
struct Directory {
files: Vec<File>,
subdirs: Vec<Directory>,
}
impl Directory {
fn total_size(&self) -> u32 {
let files_size: u32 = self.files.iter().map(|f| f.size).sum();
let subdirs_size: u32 = self.subdirs.iter().map(|d| d.total_size()).sum();
files_size + subdirs_size
}
}
fn main() {
let dir = Directory {
files: vec![File { size: 10 }, File { size: 20 }],
subdirs: vec![
Directory {
files: vec![File { size: 5 }],
subdirs: vec![],
},
Directory {
files: vec![],
subdirs: vec![
Directory {
files: vec![File { size: 15 }],
subdirs: vec![],
}
],
},
],
};
println!("Total size: {}", dir.total_size());
}Attempts:
2 left
💡 Hint
Add sizes of all files including nested directories.
✗ Incorrect
Sum of files: 10 + 20 + 5 + 15 = 50.
🧠 Conceptual
expertWhy Recursion is Essential for Some Algorithms
Which statement best explains why recursion is essential for some algorithms and cannot be replaced by simple loops without extra data structures?
Attempts:
2 left
💡 Hint
Think about how recursion manages function calls and problem breakdown.
✗ Incorrect
Recursion uses the call stack to manage self-similar subproblems naturally, while loops require explicit data structures to mimic this behavior.
