What is the main difference between recursion and dynamic programming?
Recursion solves problems by breaking them into smaller subproblems and calling itself, but may repeat work. Dynamic programming improves recursion by storing results of subproblems to avoid repeated work.
Click to reveal answer
beginner
When is a greedy algorithm the right choice?
When the problem has the greedy choice property, meaning a local optimal choice leads to a global optimal solution without needing to reconsider previous choices.
Click to reveal answer
intermediate
What problem characteristic suggests using dynamic programming?
When the problem has overlapping subproblems and optimal substructure, meaning subproblems repeat and the overall solution depends on solutions to subproblems.
Click to reveal answer
beginner
Why might pure recursion be inefficient for some problems?
Because it may solve the same subproblems multiple times, leading to exponential time complexity without storing intermediate results.
Click to reveal answer
intermediate
Give an example where greedy algorithms fail but dynamic programming works.
The coin change problem with arbitrary coin denominations: greedy may not find minimum coins, but dynamic programming finds the optimal solution.
Click to reveal answer
Which approach stores results of subproblems to avoid repeated work?
ADynamic Programming
BGreedy Algorithm
CPure Recursion
DBrute Force
✗ Incorrect
Dynamic Programming saves results of subproblems to avoid recalculating them.
When is greedy algorithm guaranteed to give an optimal solution?
AWhen problem has overlapping subproblems
BWhen problem has greedy choice property
CWhen problem has no subproblems
DAlways
✗ Incorrect
Greedy algorithms work optimally when the problem has the greedy choice property.
What is a key sign that recursion alone may be inefficient?
ANo subproblems
BNo base case
CGreedy choice property
DOverlapping subproblems
✗ Incorrect
Overlapping subproblems cause recursion to repeat work, making it inefficient.
Which method is best when subproblems do not overlap?
ARecursion with memoization
BDynamic Programming
CGreedy Algorithm
DBrute Force
✗ Incorrect
Greedy algorithms are efficient when subproblems do not overlap and local choices lead to global optimum.
Which problem is a classic example where greedy fails but DP works?
ACoin change with arbitrary coins
BSorting numbers
CFinding max in array
DBinary search
✗ Incorrect
Coin change with arbitrary denominations requires DP for optimal solution; greedy may fail.
Explain how to decide between using recursion, dynamic programming, or greedy algorithms for a problem.
Think about problem structure and if subproblems repeat or if local choices are enough.
You got /5 concepts.
Describe a real-life example where greedy algorithm works well and one where dynamic programming is better.
Consider simple daily decisions vs complex planning with repeated choices.
You got /3 concepts.
Practice
(1/5)
1. Which approach is best suited when a problem has overlapping subproblems and optimal substructure?
easy
A. Brute Force
B. Greedy Algorithm
C. Dynamic Programming
D. Simple Recursion without memoization
Solution
Step 1: Understand problem properties
Overlapping subproblems mean 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: Trace recursion bottom-up for max sum non-adjacent [OK]
Common Mistakes:
Adding adjacent elements mistakenly
Confusing indices in recursion
Ignoring base cases
4. The following code tries to solve the coin change problem using recursion but has an error. What is the error?
def coin_change(coins, amount):
if amount == 0:
return 1
if amount < 0:
return 0
count = 0
for coin in coins:
count += coin_change(coins, amount - coin)
return count
print(coin_change([1, 2], 3))
medium
A. It does not handle negative amounts correctly
B. It counts duplicate combinations multiple times
C. It misses base case for amount == 1
D. It uses iteration instead of recursion
Solution
Step 1: Analyze recursion logic
The function counts ways to make amount by subtracting coins recursively.
Step 2: Identify counting duplicates
Because it tries all coins at each step without order control, it counts same combinations in different orders multiple times.
Final Answer:
It counts duplicate combinations multiple times -> Option B
Quick Check:
Duplicates counted due to unordered recursion [OK]
Hint: Unordered recursion counts duplicates; use order or DP to fix [OK]
Common Mistakes:
Assuming base cases are missing
Confusing negative amount handling
Thinking iteration replaces recursion
5. You want to find the minimum number of coins needed to make a given amount using coin denominations [1, 3, 4]. Which approach is best and why?
hard
A. Brute force trying all combinations without optimization
B. Simple recursion without memoization for clarity
C. Greedy algorithm because always picking largest coin works
D. Dynamic Programming to store and reuse subproblem results
Solution
Step 1: Understand problem requirements
We want minimum coins to make amount, which requires checking all combinations to find optimal.
Step 2: Evaluate approaches
Greedy fails here because picking largest coin first may not yield minimum coins (e.g., amount=6, greedy picks 4+1+1=3 coins, but 3+3=2 coins is better). Simple recursion repeats subproblems causing inefficiency. DP stores results to avoid repeated work and finds optimal solution efficiently.
Final Answer:
Dynamic Programming to store and reuse subproblem results -> Option D
Quick Check:
Minimum coins with overlapping subproblems = DP [OK]
Hint: Use DP for minimum coins; greedy can fail with some denominations [OK]
Common Mistakes:
Assuming greedy always works for coin change
Ignoring efficiency of memoization
Using brute force causing timeouts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.