Recursion is when a function calls itself to solve smaller parts of a problem until it reaches a simple case it can solve directly.
Click to reveal answer
beginner
Why do we need recursion when we have loops?
Loops repeat actions but can't easily handle problems that break down into smaller similar problems, like tree traversals or factorial calculations. Recursion naturally fits these cases.
Click to reveal answer
intermediate
Give an example of a problem that loops cannot express cleanly but recursion can.
Calculating factorial of a number or traversing a tree structure are easier with recursion because they involve repeating the same process on smaller parts.
Click to reveal answer
beginner
What is a base case in recursion?
A base case is the simplest part of the problem that can be solved directly without further recursion. It stops the recursive calls.
Click to reveal answer
intermediate
How does recursion help in expressing problems with nested or hierarchical data?
Recursion naturally follows the structure of nested or hierarchical data by calling itself on smaller parts, making the code simpler and clearer than loops.
Click to reveal answer
What stops a recursive function from calling itself forever?
AA timer in the computer
BA loop inside the function
CA base case that ends recursion
DA variable that counts loops
✗ Incorrect
A base case is a condition where the function stops calling itself, preventing infinite recursion.
Which problem is easier to solve with recursion than with loops?
ASumming numbers from 1 to 10
BFinding factorial of a number
CPrinting numbers 1 to 10
DAdding two numbers
✗ Incorrect
Factorial calculation naturally breaks down into smaller factorial problems, making recursion a clean solution.
Loops are best for problems that require:
ABreaking down problems into smaller similar parts
BCalling functions multiple times
CHandling nested data structures
DRepeating a fixed set of instructions
✗ Incorrect
Loops repeat instructions a fixed number of times or until a condition is met, but don't handle nested structures well.
What is a key difference between recursion and loops?
ARecursion uses function calls; loops use repeated statements
BLoops can call themselves; recursion cannot
CRecursion is faster than loops always
DLoops need base cases; recursion does not
✗ Incorrect
Recursion solves problems by calling the same function again, while loops repeat code blocks without function calls.
Which of these is a natural use case for recursion?
ATraversing a file system directory tree
BIterating over a list of numbers
CCalculating the sum of two numbers
DPrinting a message multiple times
✗ Incorrect
Traversing hierarchical structures like directory trees fits recursion well because each folder can contain subfolders.
Explain why recursion is useful for problems that loops cannot express cleanly.
Think about how recursion handles smaller similar problems inside a big problem.
You got /5 concepts.
Describe the role of the base case in recursion and what happens if it is missing.
Base case is like the 'stop sign' for recursive calls.
You got /5 concepts.
Practice
(1/5)
1. Why do we use recursion instead of loops in some problems? fn factorial(n: u32) -> u32 { if n == 0 { 1 } else { n * factorial(n - 1) } }
easy
A. Recursion uses less memory than loops.
B. Loops are always slower than recursion.
C. Recursion can express problems with self-similar structure more naturally.
D. Loops cannot perform repeated actions.
Solution
Step 1: Understand recursion's purpose
Recursion is useful when a problem can be broken down into smaller similar problems, like factorial.
Step 2: Compare recursion and loops
Loops repeat actions but do not naturally express self-similar or nested problems as clearly as recursion.
Final Answer:
Recursion can express problems with self-similar structure more naturally. -> Option C
Quick Check:
Recursion = natural for self-similar problems [OK]
Hint: Recursion fits problems breaking into smaller similar parts [OK]
Common Mistakes:
Thinking loops are always slower
Believing recursion uses less memory
Assuming loops cannot repeat actions
2. Which of the following is the correct Rust syntax for a recursive function that sums numbers from 1 to n?
easy
A. fn sum(n: u32) -> u32 { if n == 0 { 0 } else { n + sum(n - 1) } }
B. fn sum(n: u32) -> u32 { loop { n + sum(n - 1) } }
C. fn sum(n: u32) -> u32 { while n > 0 { n + sum(n - 1) } }
D. fn sum(n: u32) -> u32 { if n == 0 { 1 } else { n * sum(n - 1) } }
Solution
Step 1: Identify correct base case and recursion
fn sum(n: u32) -> u32 { if n == 0 { 0 } else { n + sum(n - 1) } } correctly returns 0 when n is 0 and sums recursively.
Step 2: Check syntax and logic
Options B and C misuse loops inside recursion, causing infinite loops or errors. fn sum(n: u32) -> u32 { if n == 0 { 1 } else { n * sum(n - 1) } } multiplies instead of sums.
Final Answer:
fn sum(n: u32) -> u32 { if n == 0 { 0 } else { n + sum(n - 1) } } -> Option A
Quick Check:
Correct recursion syntax = fn sum(n: u32) -> u32 { if n == 0 { 0 } else { n + sum(n - 1) } } [OK]
Hint: Look for correct base case and recursive call pattern [OK]