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 already have loops?
Loops repeat actions but cannot easily handle problems that need to break down into smaller similar problems, like tree traversals or exploring choices. Recursion naturally fits these cases.
Click to reveal answer
intermediate
Give an example of a problem that loops cannot express cleanly but recursion can.
Exploring all paths in a maze or traversing a family tree is hard with loops alone because the number of steps is not fixed. Recursion handles this by calling itself for each branch.
Click to reveal answer
beginner
What is a base case in recursion?
A base case is the simplest situation where the problem can be solved directly without further recursion. It stops the function from calling itself forever.
Click to reveal answer
intermediate
How does recursion help in expressing problems with unknown or variable steps?
Recursion breaks the problem into smaller parts and solves each part by calling itself. This works well when the number of steps or branches is not known in advance, unlike loops that need fixed repetition.
Click to reveal answer
What stops a recursive function from running forever?
AA base case that ends recursion
BA loop inside the function
CUsing a counter variable
DCalling another function
✗ Incorrect
A base case is a simple condition where the function returns without calling itself again, stopping infinite recursion.
Which problem is easier to solve with recursion than with loops?
ATraversing a tree structure
BSumming numbers from 1 to 10
CPrinting numbers 1 to 5
DRepeating a fixed action 3 times
✗ Incorrect
Tree traversal involves branching paths and unknown depth, which recursion handles naturally, unlike simple loops.
Why can't loops express some problems cleanly?
ALoops are slower
BLoops cannot call functions
CLoops use more memory
DLoops require fixed repetition counts
✗ Incorrect
Loops need a known number of repetitions, but some problems have unknown or variable steps that recursion can handle better.
What is the main idea behind recursion?
AFunction uses loops to repeat
BFunction calls itself to solve smaller problems
CFunction calls another function
DFunction runs only once
✗ Incorrect
Recursion means a function solves a problem by calling itself with simpler inputs.
Which of these is NOT a good use case for recursion?
ACalculating factorial
BWalking through a file system
CAdding two numbers
DSolving puzzles with choices
✗ Incorrect
Adding two numbers is simple and does not need recursion; recursion is better for problems with repeated or branching steps.
Explain why recursion is useful when loops cannot express a problem cleanly.
Think about problems with branching or unknown depth.
You got /4 concepts.
Describe what a base case is and why it is important in recursion.
Without it, recursion never ends.
You got /4 concepts.
Practice
(1/5)
1. Why do we use recursion instead of loops in some problems?
easy
A. Because recursion can handle problems with self-similar subproblems naturally
B. Because loops are slower than recursion in all cases
C. Because recursion uses less memory than loops always
D. Because loops cannot repeat tasks
Solution
Step 1: Understand recursion's nature
Recursion breaks a problem into smaller similar problems, calling itself to solve each part.
Step 2: Compare with loops
Loops repeat tasks but do not naturally express problems that divide into smaller similar parts.
Final Answer:
Because recursion can handle problems with self-similar subproblems naturally -> Option A
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
Assuming recursion always uses less memory
Believing loops cannot repeat tasks
2. Which of the following is the correct syntax to define a recursive function in Python?
easy
A. def recurse(n): while n > 0: return n * recurse(n-1)
B. def recurse(n): if n == 0: return 1 else: return n * recurse(n-1)
C. def recurse(n): for i in range(n): return recurse(i)
D. def recurse(n): return n * recurse(n+1)
Solution
Step 1: Check base case and recursive call
def recurse(n): if n == 0: return 1 else: return n * recurse(n-1) has a base case (n == 0) and calls itself with smaller n (n-1), correct recursion pattern.
Step 2: Verify other options
Options A and C misuse loops inside recursion; D calls recurse with n+1 causing infinite recursion.
Final Answer:
def recurse(n): if n == 0: return 1 else: return n * recurse(n-1) -> Option B
Quick Check:
Base case + smaller recursive call = correct recursion [OK]
Hint: Look for base case and smaller recursive call [OK]
Common Mistakes:
Missing base case causing infinite recursion
Using loops incorrectly inside recursion
Calling recursion with increasing argument
3. What is the output of this code?
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(4))