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 exploring choices. Recursion handles these naturally.
Click to reveal answer
intermediate
Give an example of a problem that loops cannot express cleanly but recursion can.
Navigating a family tree or folder structure is hard with loops because the depth is unknown. Recursion can explore each branch easily by calling itself for each child.
Click to reveal answer
beginner
What is a base case in recursion and why is it important?
A base case is the simplest problem that recursion can solve directly without calling itself again. It stops the recursion from going forever.
Click to reveal answer
intermediate
How does recursion help in expressing problems with unknown or variable depth?
Recursion naturally handles unknown depth by calling itself for each smaller part until it reaches the base case, unlike loops which need fixed repetition counts.
Click to reveal answer
Which of these problems is best solved using recursion?
ACalculating the sum of numbers from 1 to 100
BPrinting numbers from 1 to 10
CTraversing a folder structure with unknown depth
DRepeating a task 5 times
✗ Incorrect
Traversing a folder structure with unknown depth is best handled by recursion because the depth and number of folders are not fixed.
What stops a recursive function from running forever?
AA base case that ends recursion
BCalling the function twice
CA loop inside the function
DUsing a counter variable
✗ Incorrect
A base case is a simple condition that stops the recursive calls, preventing infinite loops.
Why can't loops easily replace recursion for some problems?
ALoops are slower
BLoops can't handle problems with unknown or variable depth cleanly
CLoops use more memory
DLoops are harder to write
✗ Incorrect
Loops require a fixed number of repetitions, so they struggle with problems where the depth or size is unknown or changes.
Which of these is NOT a characteristic of recursion?
AFunction calls itself
BHas a base case
CBreaks problem into smaller parts
DAlways uses loops inside
✗ Incorrect
Recursion does not always use loops inside; it relies on self-calling and base cases.
What is a common real-life example to explain recursion?
ALooking at a mirror reflecting another mirror
BFilling a bucket with water
CWalking in a straight line
DCounting steps on a staircase
✗ Incorrect
Looking at mirrors reflecting each other shows a repeated pattern calling itself, similar to recursion.
Explain why recursion is useful for problems with unknown depth or branching, and why loops struggle with these.
Think about exploring folders or family trees.
You got /4 concepts.
Describe the role of the base case in recursion and what happens if it is missing.
What stops the function from calling itself forever?
You got /4 concepts.
Practice
(1/5)
1. Why do we use recursion instead of loops in some problems? function factorial(n) { if (n === 0) return 1; return n * factorial(n - 1); }
easy
A. Loops are always slower than recursion.
B. Recursion can express problems with self-similar structure more naturally.
C. Recursion uses less memory than loops.
D. Loops cannot perform repeated actions.
Solution
Step 1: Understand recursion's purpose
Recursion solves problems by breaking them into smaller similar problems, like factorial breaking n! into n * (n-1)!.
Step 2: Compare recursion and loops
Loops repeat actions but do not naturally express self-similar or nested structures, which recursion handles clearly.
Final Answer:
Recursion can express problems with self-similar structure more naturally. -> Option B
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
2. Which of the following is the correct syntax to define a recursive function in JavaScript?
easy
A. function recurse(n) { for(let i=0; i
B. function recurse(n) { while(n > 0) recurse(n - 1); }
C. function recurse(n) { if (n > 0) return n; }
D. function recurse(n) { if (n <= 0) return; recurse(n - 1); }
Solution
Step 1: Identify recursive call structure
function recurse(n) { if (n <= 0) return; recurse(n - 1); } calls itself with a smaller argument and has a base case to stop recursion.
Step 2: Check other options for recursion correctness
The options using for or while loops include recursive calls but lack base cases, leading to infinite recursion. The option that returns n without a recursive call is not recursive.
Final Answer:
function recurse(n) { if (n <= 0) return; recurse(n - 1); } -> Option D
Quick Check:
Correct recursion has base case and self-call [OK]
Hint: Look for base case and self-call in function [OK]
Common Mistakes:
Missing base case causing infinite recursion
Using loops instead of recursion
No recursive call inside function
3. What is the output of this recursive function?
function sumTo(n) { if (n === 0) return 0; return n + sumTo(n - 1); } console.log(sumTo(3));