What is the main idea behind the greedy algorithm approach?
Greedy algorithms make the best choice at each step, hoping that these local best choices lead to a global best solution.
Click to reveal answer
beginner
Why does a greedy algorithm sometimes fail to find the best solution?
Because greedy algorithms do not reconsider previous choices, they can get stuck in a local optimum that is not the best overall solution.
Click to reveal answer
intermediate
What property must a problem have for a greedy algorithm to always work correctly?
The problem must have the greedy-choice property and optimal substructure, meaning local choices lead to a global optimum and subproblems can be solved optimally.
Click to reveal answer
beginner
Give an example of a problem where greedy algorithms work well.
The activity selection problem, where choosing the earliest finishing activity first leads to the maximum number of non-overlapping activities.
Click to reveal answer
intermediate
Give an example of a problem where greedy algorithms fail to find the best solution.
The coin change problem with arbitrary coin denominations, where greedy choice of largest coin first may not give the minimum number of coins.
Click to reveal answer
What does a greedy algorithm do at each step?
ABacktracks to previous steps
BTries all possible choices
CMakes the best local choice
DRandomly picks a choice
✗ Incorrect
Greedy algorithms always pick the best local choice at each step without reconsidering previous decisions.
Which property ensures a greedy algorithm finds the global optimum?
ABacktracking
BDivide and conquer
CBrute force
DGreedy-choice property
✗ Incorrect
The greedy-choice property means local optimal choices lead to a global optimum.
When can greedy algorithms fail?
AWhen local choices do not lead to global optimum
BWhen problem size is small
CWhen all choices are equal
DWhen problem has optimal substructure
✗ Incorrect
Greedy algorithms fail when the best local choice does not lead to the best overall solution.
Which problem is a classic example where greedy works?
ATraveling salesman
BActivity selection
CKnapsack 0/1
DCoin change with arbitrary coins
✗ Incorrect
Activity selection problem can be solved optimally by greedy choice of earliest finishing activity.
Why does greedy fail in the coin change problem with arbitrary coins?
ALargest coin first may not minimize total coins
BCoins are unlimited
CCoins have same value
DGreedy always works for coin change
✗ Incorrect
Choosing the largest coin first can lead to more coins than necessary in some coin sets.
Explain why greedy algorithms work for some problems but fail for others.
Think about how local choices relate to the overall best solution.
You got /4 concepts.
Describe the key properties a problem must have for a greedy algorithm to guarantee an optimal solution.
Focus on problem structure and solution building.
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 sorts the input data before processing
B. Because it tries all possible solutions and picks the best one
C. Because it makes the best choice at each step hoping for a global optimum
D. Because it uses recursion to explore all paths
Solution
Step 1: Understand greedy choice property
A greedy algorithm makes the best local choice at each step, aiming for a global best solution.
Step 2: Differentiate from exhaustive search
Unlike trying all solutions or recursion, greedy does not explore all paths but relies on local optimality.
Final Answer:
Because it makes the best choice at each step hoping for a global optimum -> Option C
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, not all possibilities [OK]
Common Mistakes:
Confusing greedy with exhaustive search
Thinking greedy always tries all solutions
Assuming sorting alone makes greedy correct
2. Which of the following Go code snippets correctly implements a greedy step to pick the largest number from a slice?
easy
A. max := nums[0]
for _, v := range nums {
if v > max {
max = v
}
}
B. max := 0
for i := 1; i < len(nums); i++ {
if nums[i] < max {
max = nums[i]
}
}
C. max := nums[0]
for i := 0; i < len(nums); i++ {
if nums[i] < max {
max = nums[i]
}
}
D. max := nums[0]
for _, v := range nums {
if v < max {
max = v
}
}
Solution
Step 1: Identify correct max initialization and comparison
Initialize max with first element and update if current value is greater.
Step 2: Check loop and condition correctness
max := nums[0]
for _, v := range nums {
if v > max {
max = v
}
} uses correct range loop and updates max when v > max.
Final Answer:
Option A correctly finds the largest number -> Option A
Quick Check:
Correct max update condition = max := nums[0]
for _, v := range nums {
if v > max {
max = v
}
} [OK]
Hint: Update max only if current value is greater [OK]
Common Mistakes:
Using '<' instead of '>' to find max
Initializing max to zero instead of first element
Comparing incorrectly inside loop
3. What will be the output of the following Go code that tries to select intervals greedily by earliest finish time?
package main
import "fmt"
func main() {
intervals := [][]int{{1,4},{3,5},{0,6},{5,7},{8,9},{5,9}}
selected := [][]int{}
lastEnd := 0
for _, interval := range intervals {
if interval[0] >= lastEnd {
selected = append(selected, interval)
lastEnd = interval[1]
}
}
fmt.Println(selected)
}
medium
A. [[1 4] [5 7] [8 9]]
B. [[1 4] [3 5] [5 7] [8 9]]
C. [[0 6] [8 9]]
D. [[1 4] [5 9]]
Solution
Step 1: Understand greedy selection by earliest finish time
Intervals are checked in given order; select if start >= last selected end.
A. The coins slice is not sorted in ascending order
B. The greedy approach fails for some coin sets, but code logic is correct
C. The code does not update amount correctly inside the loop
D. The count variable should be reset inside the loop
Solution
Step 1: Analyze greedy coin change logic
The code picks largest coins first, counting how many fit, updating amount correctly.
Step 2: Understand greedy failure cases
Greedy works for US coins but fails for some coin sets; code logic is correct but approach can fail.
Final Answer:
The greedy approach fails for some coin sets, but code logic is correct -> Option B
Quick Check:
Greedy correctness depends on coin set = The greedy approach fails for some coin sets, but code logic is correct [OK]
Hint: Greedy coin change fails on non-canonical coin sets [OK]
Common Mistakes:
Thinking code has syntax or logic error
Assuming coin order must be ascending
Resetting count inside loop incorrectly
5. You want to schedule the maximum number of non-overlapping meetings from a list of start and end times. Which greedy strategy guarantees the optimal solution?
hard
A. Always pick the meeting with the latest start time
B. Always pick the meeting with the shortest duration
C. Always pick the meeting with the earliest start time
D. Always pick the meeting with the earliest finish time