Making locally optimal choices that guarantee global optimal outcomes
432 problems·42 Easy·295 Medium·95 Hard
Pattern Study Guide & Cheat Sheet· O(n) single pass or O(n log n) with sorting · O(1) auxiliary space
▼
Make the locally optimal choice at each decision step with the mathematical guarantee that it leads to the globally optimal solution without backtracking.
Core Invariant: Greedy Choice Property: Can you prove by exchange argument that swapping a greedy decision with any alternative never produces a superior outcome? If yes, greedy holds.
Recognize it (Keywords & Signals)
Maximize profit / minimize cost with independent sequential choices
Jump game (maximum reachable index)
Gas station circuit / circular route
Task scheduler / candy distribution
Minimum arrows to burst balloons
When NOT to use
Local decisions constrain future options in conflicting ways (e.g. 0/1 Knapsack, Coin Change with arbitrary denominations; must use DP!).
How to solve (Step-by-step)
1.Identify the greedy metric (e.g. earliest finish time, farthest reach, max profit step).
2.Sort inputs if necessary to enforce the greedy ordering.
3.Maintain a single state variable (e.g., `farthest_reach = 0`, `current_gas = 0`).
4.Iterate through elements and update state irreversibly.
5.Return final accumulated score or boolean feasibility.
Watch for (Interview Traps)
Assuming greedy works without proof (e.g. Coin Change fails for denominations [1, 3, 4] with target 6)
Premature optimization: always verify if dynamic programming is required before committing to greedy
Failing to check if starting point is unreachable in Jump Game
Farthest Reach Greedy (Jump Game)
def can_jump(nums: list[int]) -> bool:
farthest = 0
for i, jump in enumerate(nums):
# If current index is beyond the farthest reach, we cannot proceed
if i > farthest:
return False
# Update maximum index we can reach
farthest = max(farthest, i + jump)
if farthest >= len(nums) - 1:
return True
return True
Cost
O(n) single pass or O(n log n) with sorting · O(1) auxiliary space (Extremely memory efficient when greedy choice property holds.)
What is the Greedy Algorithms pattern in coding interviews?
Making locally optimal choices that guarantee global optimal outcomes It is categorized under Advanced & DP and features 432 curated interview problems across Easy (42), Medium (295), and Hard (95) difficulties.
Which tech companies frequently test Greedy Algorithms?
Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.