What is the main idea behind the greedy algorithm approach?
A greedy algorithm makes the best choice at each step, hoping to find the overall best solution by building it piece by piece.
Click to reveal answer
intermediate
When does a greedy algorithm guarantee an optimal solution?
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 the greedy-choice property?
It means that a locally optimal choice can be extended to a globally optimal solution without reconsidering previous choices.
Click to reveal answer
intermediate
Give an example of a problem where greedy algorithms fail to find the optimal solution.
The coin change problem with arbitrary coin denominations can fail with greedy algorithms because picking the largest coin first may not lead to the fewest coins overall.
Click to reveal answer
intermediate
Why does the greedy algorithm fail in some problems?
Because the problem lacks the greedy-choice property or optimal substructure, so local best choices do not lead to the global best solution.
Click to reveal answer
Which property must a problem have for a greedy algorithm to always work?
ARandomness
BGreedy-choice property
CBrute force
DBacktracking
✗ Incorrect
The greedy-choice property ensures that local optimal choices lead to a global optimal solution.
What does optimal substructure mean in the context of greedy algorithms?
AProblem has no solution
BProblem requires random guessing
CProblem can be divided into smaller subproblems with optimal solutions
DProblem needs backtracking
✗ Incorrect
Optimal substructure means the optimal solution can be built from optimal solutions of subproblems.
Why might greedy algorithms fail on the coin change problem with arbitrary coins?
ABecause picking the largest coin first may not minimize total coins
BBecause coins are unlimited
CBecause greedy algorithms always work on coin change
DBecause coins have the same value
✗ Incorrect
Greedy choice of largest coin first can lead to more coins than necessary.
Which of these is NOT a characteristic of problems solved by greedy algorithms?
ARequires checking all combinations
BOptimal substructure
CLocal choices lead to global optimum
DGreedy-choice property
✗ Incorrect
Greedy algorithms do not check all combinations; they rely on local choices.
What is a key difference between greedy algorithms and dynamic programming?
AGreedy uses recursion; dynamic programming does not
BGreedy algorithms always fail
CDynamic programming is always faster
DGreedy makes local choices; dynamic programming considers all subproblems
✗ Incorrect
Dynamic programming solves all subproblems and combines solutions, while greedy makes local choices.
Explain why greedy algorithms work for some problems but fail for others.
Think about how local best choices relate to the overall best solution.
You got /4 concepts.
Describe the greedy-choice property and give an example of a problem that has it.
Focus on how choosing the best option at each step leads to the best total result.
You got /3 concepts.
Practice
(1/5)
1. What is the main reason a greedy algorithm works correctly for some problems?
easy
A. Because it makes the best choice at each step hoping for a global optimum
B. Because it tries all possible solutions and picks the best one
C. Because it uses recursion to explore all options
D. Because it sorts the input data before processing
Solution
Step 1: Understand greedy choice property
A greedy algorithm picks the best option available at the current step without reconsidering previous choices.
Step 2: Connect greedy choice to global optimum
For some problems, making the best local choice at each step leads to the best overall solution.
Final Answer:
Because it makes the best choice at each step hoping for a global optimum -> Option A
Quick Check:
Greedy choice property = Because it makes the best choice at each step hoping for a global optimum [OK]
Hint: Greedy picks best local choice hoping for best overall [OK]
Common Mistakes:
Thinking greedy tries all solutions
Confusing greedy with recursion or backtracking
Assuming sorting alone makes greedy correct
2. Which of the following is the correct syntax to pick the next item greedily from a sorted array arr in C?
easy
A. int next = arr[--i];
B. int next = arr[i]; i++;
C. int next = arr[++i];
D. int next = arr[i]; i--;
Solution
Step 1: Understand post-increment vs pre-increment
Post-increment i++ returns current value then increments; pre-increment ++i increments then returns.
Step 2: Choose syntax that picks current item then moves forward
int next = arr[i]; i++; picks current element and then increments index for next pick.
Final Answer:
int next = arr[i]; i++; -> Option B
Quick Check:
Pick current then increment index = int next = arr[i]; i++; [OK]
Hint: Pick current element then increment index separately [OK]
Common Mistakes:
Using pre-increment which skips first element
Decrementing index instead of incrementing
Confusing post-increment inside array index
3. Consider the following C code that tries to select intervals greedily by earliest finish time:
typedef struct {
int start;
int end;
} Interval;
int main() {
Interval arr[] = {{1,4}, {3,5}, {0,6}, {5,7}, {8,9}};
int n = 5, count = 0, last_end = 0;
for (int i = 0; i < n; i++) {
if (arr[i].start >= last_end) {
count++;
last_end = arr[i].end;
}
}
printf("%d\n", count);
return 0;
}