What is the main idea behind the greedy algorithm?
A greedy algorithm 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 algorithms?
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 choose a greedy algorithm over DP?
Choose greedy when the problem has the 'greedy choice property' and 'optimal substructure', meaning local best choices lead to a global best solution.
Click to reveal answer
intermediate
What is 'optimal substructure' in the context of DP and greedy algorithms?
Optimal substructure means the best solution to a problem can be built from the best solutions of its smaller parts.
Click to reveal answer
advanced
How can you test if a greedy algorithm will work for a problem?
Try to prove or check if making the best local choice at each step always leads to the global best solution. If not, DP might be needed.
Click to reveal answer
Which property must a problem have for a greedy algorithm to work correctly?
AGreedy choice property
BOverlapping subproblems
CMemoization
DBacktracking
✗ Incorrect
The greedy choice property means local optimal choices lead to a global optimum, which is essential for greedy algorithms.
What does Dynamic Programming use to avoid repeated work?
AGreedy choices
BRandom guessing
CMemoization or tabulation
DDivide and conquer without storing results
✗ Incorrect
DP stores results of subproblems using memoization or tabulation to avoid solving the same problem multiple times.
If a problem has overlapping subproblems but no greedy choice property, which approach is better?
AGreedy algorithm
BDivide and conquer without memoization
CBrute force
DDynamic Programming
✗ Incorrect
DP is suitable when subproblems overlap and greedy choices do not guarantee an optimal solution.
Which of these is an example of a problem typically solved by a greedy algorithm?
ACoin change with unlimited coins
BActivity selection problem
CShortest path in a graph with negative edges
DFibonacci sequence
✗ Incorrect
The activity selection problem can be solved optimally by choosing the earliest finishing activity greedily.
What is a sign that a problem might require Dynamic Programming?
ASubproblems overlap and optimal substructure exists
BGreedy choice property holds
CNo overlapping subproblems
DProblem size is very small
✗ Incorrect
DP is useful when subproblems overlap and the problem has optimal substructure.
Explain how to decide between using a greedy algorithm or dynamic programming for a problem.
Think about problem properties and how solutions build from smaller parts.
You got /5 concepts.
Describe the difference between greedy algorithms and dynamic programming in simple terms.
Focus on how each method approaches problem solving.
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 needs to store results of subproblems
B. When the problem requires checking all possible solutions
C. When overlapping subproblems are present
D. When the problem has the greedy choice property and optimal substructure
Solution
Step 1: Understand greedy choice property
A greedy algorithm works when choosing the best option at each step leads to a global optimum, known as the greedy choice property.
Step 2: Compare with dynamic programming conditions
Dynamic programming is used when problems have overlapping subproblems and optimal substructure, but greedy algorithms require only the greedy choice property and optimal substructure.
Final Answer:
When the problem has the greedy choice property and optimal substructure -> Option D
Quick Check:
Greedy choice property = Use greedy [OK]
Hint: Use greedy if local best leads to global best [OK]
Common Mistakes:
Confusing overlapping subproblems as greedy condition
Using dynamic programming when greedy suffices
Ignoring greedy choice property
2. Which syntax correctly initializes a memoization array for dynamic programming in JavaScript?
easy
A. const memo = new Array(n).fill(-1);
B. const memo = Array(n).map(() => 0);
C. const memo = new Array(n).map(() => 0);
D. const memo = Array.fill(n, -1);
Solution
Step 1: Understand Array initialization
Using new Array(n).fill(-1) creates an array of length n filled with -1, suitable for memoization since -1 indicates uncomputed.
Step 2: Check other options
A and C fill with 0, which conflicts if 0 is a valid result; map is also unnecessary. D is invalid syntax.
Final Answer:
const memo = new Array(n).fill(-1); -> Option A
Quick Check:
Use new Array(n).fill(value) for memo [OK]
Hint: Use new Array(n).fill(value) to initialize arrays [OK]
Common Mistakes:
Using map on uninitialized array
Wrong order of fill arguments
Using Array.fill which is invalid
3. What is the output of the following JavaScript code?
function maxSum(arr) {
let incl = arr[0];
let excl = 0;
for (let i = 1; i < arr.length; i++) {
let new_excl = Math.max(incl, excl);
incl = excl + arr[i];
excl = new_excl;
}
return Math.max(incl, excl);
}
console.log(maxSum([3, 2, 7, 10]));