Recursion is a method where a function calls itself to solve smaller parts of a problem until it reaches a simple base case.
Click to reveal answer
intermediate
What does DP (Dynamic Programming) do differently from simple recursion?
DP stores results of solved subproblems to avoid repeating work, making it faster than simple recursion for overlapping subproblems.
Click to reveal answer
beginner
When is a greedy algorithm the right choice?
A greedy algorithm works well when choosing the best option at each step leads to a global best solution, without needing to reconsider choices.
Click to reveal answer
intermediate
What kind of problems are best solved by DP instead of recursion or greedy?
Problems with overlapping subproblems and optimal substructure, where subproblems repeat and solutions build on smaller solutions, are best for DP.
Click to reveal answer
beginner
Why might recursion be inefficient without DP?
Recursion can repeat the same calculations many times, causing slow performance and high memory use, which DP avoids by saving results.
Click to reveal answer
Which approach saves results of subproblems to avoid repeated work?
ABrute Force
BSimple Recursion
CGreedy Algorithm
DDynamic Programming
✗ Incorrect
Dynamic Programming stores results of subproblems to reuse them, improving efficiency.
When is a greedy algorithm NOT a good choice?
AWhen recursion is possible
BWhen local choices don't lead to a global best solution
CWhen subproblems overlap
DWhen the problem is simple
✗ Incorrect
Greedy fails if choosing the best local option doesn't guarantee the best overall solution.
What is a base case in recursion?
AA greedy choice
BThe largest input size
CThe simplest problem that stops recursion
DA repeated subproblem
✗ Incorrect
Base case stops recursion by providing a direct answer without further calls.
Which method is best when subproblems overlap and solutions build on smaller solutions?
ADynamic Programming
BGreedy Algorithm
CSimple Recursion
DSorting
✗ Incorrect
Dynamic Programming handles overlapping subproblems efficiently by storing results.
What is a key disadvantage of simple recursion without optimization?
ARepeats calculations causing inefficiency
BAlways uses greedy choices
CCannot solve problems with base cases
DRequires sorting input
✗ Incorrect
Simple recursion can repeat the same work many times, making it slow.
Explain how to decide between using recursion, dynamic programming, or a greedy algorithm for a problem.
Think about problem structure and if repeated work happens.
You got /5 concepts.
Describe the main difference between dynamic programming and greedy algorithms.
Focus on how choices are made and stored.
You got /4 concepts.
Practice
(1/5)
1. Which approach is best suited when a problem has overlapping subproblems and optimal substructure?
easy
A. Dynamic Programming
B. Greedy Algorithm
C. Simple Recursion
D. Brute Force
Solution
Step 1: Understand problem properties
Overlapping subproblems means the same smaller problems repeat. Optimal substructure means the best solution can be built from best solutions of smaller parts.
Step 2: Match approach to properties
Dynamic Programming stores results of subproblems to avoid repeated work, ideal for these properties.
Hint: Use DP when subproblems repeat and build optimal solutions [OK]
Common Mistakes:
Choosing greedy when subproblems overlap
Using simple recursion without memoization
Confusing optimal substructure with greedy choice property
2. Which of the following Java method signatures correctly represents a recursive function to compute factorial?
easy
A. public static int factorial(int n) { if (n == 1) return 0; else return n + factorial(n - 1); }
B. public static int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); }
C. public static int factorial(int n) { for (int i = 1; i <= n; i++) return i; }
D. public static int factorial(int n) { while (n > 0) n = n - 1; return n; }
Solution
Step 1: Check base case correctness
Factorial base case is 0! = 1, so if (n == 0) return 1 is correct.
Step 2: Check recursive call and calculation
Factorial(n) = n * factorial(n-1) is correct recursive formula.
Final Answer:
public static int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } -> Option B
Quick Check:
Correct base case and recursive multiplication = public static int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } [OK]
Hint: Factorial recursion multiplies n by factorial(n-1) with base n=0 [OK]
Common Mistakes:
Wrong base case (e.g., returning 0 instead of 1)
Using loops instead of recursion
Returning addition instead of multiplication
3. What will be the output of the following Java code snippet?
public class Test {
public static int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
public static void main(String[] args) {
System.out.println(fib(4));
}
}
Hint: Sort coins descending for greedy coin change to work correctly [OK]
Common Mistakes:
Not sorting coins before greedy selection
Using recursion unnecessarily
Resetting count inside loop causing wrong count
5. You want to find the minimum number of jumps to reach the end of an array where each element represents max jump length at that position. Which approach is best and why?
int[] arr = {2,3,1,1,4};
hard
A. Use Dynamic Programming to store minimum jumps for each index
B. Use simple recursion to try all jump combinations
C. Use Brute Force to check every path
D. Use Greedy approach to jump to the farthest reachable index
Solution
Step 1: Analyze problem nature
Minimum jumps problem can be solved efficiently by greedy because at each step, jumping to the farthest reachable index reduces total jumps.
Step 2: Compare approaches
Recursion or DP works but are slower. Greedy gives optimal solution in linear time for this problem.
Final Answer:
Use Greedy approach to jump to the farthest reachable index -> Option D
Quick Check:
Minimum jumps = Greedy farthest jump [OK]
Hint: Jump farthest reachable index greedily for minimum jumps [OK]
Common Mistakes:
Using recursion causing timeouts
Overcomplicating with DP when greedy suffices
Ignoring problem constraints for greedy validity
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.