Greedy approach makes the best choice at each step, hoping to find the overall best solution without reconsidering previous choices.
Click to reveal answer
beginner
What does Dynamic Programming (DP) do differently from Greedy?
DP solves problems by breaking them into smaller overlapping subproblems, solving each once, and storing results to avoid repeated work.
Click to reveal answer
intermediate
When should you use Greedy over DP?
Use Greedy when the problem has the greedy-choice property and optimal substructure, meaning local best choices lead to global best solution.
Click to reveal answer
beginner
What is the optimal substructure property?
A problem has optimal substructure if an optimal solution can be built from optimal solutions of its subproblems.
Click to reveal answer
intermediate
How can you tell if a problem needs DP instead of Greedy?
If the problem has overlapping subproblems and no greedy-choice property, DP is needed to explore all options and store results.
Click to reveal answer
Which property must a problem have for Greedy to work correctly?
AMemoization
BOverlapping subproblems
CGreedy-choice property
DBacktracking
✗ Incorrect
Greedy-choice property means local best choices lead to global best solution, essential for Greedy to work.
What does Dynamic Programming use to avoid repeated work?
AMemoization or tabulation
BGreedy choices
CRandom guessing
DDivide and conquer without storage
✗ Incorrect
DP stores results of subproblems using memoization or tabulation to avoid recalculating.
If a problem has overlapping subproblems but also a greedy-choice property, which approach is better?
ABacktracking
BGreedy
CBrute force
DDynamic Programming
✗ Incorrect
Greedy is preferred if greedy-choice property holds, even if subproblems overlap.
Which of these problems is typically solved by Dynamic Programming?
AFibonacci sequence calculation
BFinding minimum spanning tree
CSorting an array
DBinary search
✗ Incorrect
Fibonacci has overlapping subproblems and optimal substructure, ideal for DP.
What is NOT a sign that Greedy might fail?
ALocal choices don't lead to global optimum
BOverlapping subproblems
CNo greedy-choice property
DOptimal substructure
✗ Incorrect
Optimal substructure alone does not mean Greedy fails; it is necessary but not sufficient.
Explain how to decide between using Greedy or Dynamic Programming for a problem.
Think about whether making the best choice now guarantees the best overall solution.
You got /4 concepts.
Describe the key differences between Greedy and Dynamic Programming approaches.
Consider how each approach handles problem parts and solution building.
You got /4 concepts.
Practice
(1/5)
1. Which of the following best describes when to use a greedy algorithm instead of dynamic programming?
easy
A. When the problem has the greedy choice property and optimal substructure
B. When the problem requires checking all possible combinations
C. When the problem has overlapping subproblems but no greedy choice
D. When the problem is about sorting large datasets
Solution
Step 1: Understand greedy choice property
A greedy algorithm works when choosing the best option at each step leads to a global optimum.
Step 2: Compare with dynamic programming conditions
Dynamic programming is used when subproblems overlap and greedy choice property does not hold.
Final Answer:
When the problem has the greedy choice property and optimal substructure -> Option A
Quick Check:
Greedy choice property = Use greedy [OK]
Hint: Use greedy if local best leads to global best [OK]
Common Mistakes:
Confusing overlapping subproblems with greedy choice
Using greedy when problem needs full exploration
Assuming sorting problems always need greedy
2. Which of the following is the correct syntax to declare a memoization array for dynamic programming in C?
easy
A. int memo[1000] = {0};
B. int memo[] = 0;
C. int memo(1000) = {0};
D. int memo = new int[1000];
Solution
Step 1: Recall C array declaration syntax
In C, arrays are declared with fixed size and optional initialization using braces.
Step 2: Check each option
int memo[1000] = {0}; correctly declares an int array of size 1000 initialized to zero. Others are invalid C syntax.
Final Answer:
int memo[1000] = {0}; -> Option A
Quick Check:
C arrays use [] with size [OK]
Hint: Use int arr[size] = {0} for zero-initialized arrays [OK]
Common Mistakes:
Using parentheses instead of brackets for arrays
Trying to assign 0 directly to array
Using new keyword in C (it's C++ syntax)
3. What is the output of the following C code snippet that uses a greedy approach to select coins?
int coins[] = {1, 5, 10, 25};
int amount = 30;
int count = 0;
for (int i = 3; i >= 0; i--) {
while (amount >= coins[i]) {
amount -= coins[i];
count++;
}
}
printf("%d\n", count);
medium
A. 3
B. 4
C. 2
D. 5
Solution
Step 1: Trace the greedy coin selection
Start with coin 25: amount=30, 30>=25, subtract 25, amount=5, count=1.
Step 2: Continue with smaller coins
Next coin 10: amount=5<10, skip. Next coin 5: amount=5>=5, subtract 5, amount=0, count=2.
Step 3: No more coins needed
Loop ends, total coins used = 2.
Final Answer:
2 -> Option C
Quick Check:
25 + 5 = 30 using 2 coins [OK]
Hint: Subtract largest coin repeatedly until amount zero [OK]
Common Mistakes:
Counting coins incorrectly in loops
Not updating amount after subtraction
Confusing coin order in loop
4. The following code attempts to solve the Fibonacci sequence using a greedy approach. What is the error?
int fib(int n) {
if (n <= 1) return n;
int a = 0, b = 1, c = 0;
for (int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return c;
}
medium
A. Loop should start from i = 1 instead of 2
B. No error, code correctly computes Fibonacci
C. Variable c is not initialized properly
D. Greedy approach is incorrect for Fibonacci calculation
Solution
Step 1: Understand Fibonacci calculation
Fibonacci requires summing previous two numbers, which is a classic dynamic programming problem.
Step 2: Identify greedy approach misuse
The code uses iteration correctly but calling it greedy is wrong because Fibonacci does not have greedy choice property.
Final Answer:
Greedy approach is incorrect for Fibonacci calculation -> Option D
Quick Check:
Fibonacci needs DP, not greedy [OK]
Hint: Fibonacci needs DP, greedy won't work [OK]
Common Mistakes:
Thinking iterative is greedy
Ignoring problem properties
Confusing initialization with error
5. You want to find the minimum number of coins to make change for a given amount using coins of denominations {1, 3, 4}. Which approach is best and why?
hard
A. Greedy, because always picking the largest coin works here
B. Dynamic Programming, because greedy fails for some amounts
C. Brute force, because both greedy and DP are too complex
D. Sorting coins and picking smallest first
Solution
Step 1: Test greedy approach on example
For amount 6, greedy picks 4 + 1 + 1 = 3 coins, but optimal is 3 + 3 = 2 coins.
Step 2: Recognize overlapping subproblems
Dynamic programming stores solutions for smaller amounts to build up answer correctly.
Step 3: Conclude best approach
Greedy fails due to lack of greedy choice property; DP handles all cases optimally.
Final Answer:
Dynamic Programming, because greedy fails for some amounts -> Option B
Quick Check:
Greedy fails, DP works [OK]
Hint: Test greedy on small cases; if fails, use DP [OK]
Common Mistakes:
Assuming greedy always works for coin change
Ignoring counterexamples
Choosing brute force unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.