Skip to main content
PatternGreedyPatternsAlgorithms

Greedy works when the next step can't haunt you

A practical guide to the greedy pattern -- when to use it, why it works, and the problems that prove it. No formal proofs required.

Fin·Reviewed by Leo Kwan·Apr 3, 2026·5 min read

Greedy — pick early, don't look back

Greedy — interval scheduling, one pick committingFour intervals on a timeline. The first (earliest-ending) interval is picked and breathes in place. Two overlapping intervals after it fade as they're skipped. A shark-fin marks the commit point on the picked interval.time →ApickBskipCDrulepick the earliest-ending intervaldrop every overlap · repeat

take the locally-best move · trust the exchange argument

StrongYes tip

isn't a guess -- it's a bet that the locally optimal choice is globally optimal. The interview skill is knowing when that bet pays off.

Most interview problems make you consider all possibilities, then pick the best. Greedy flips that: make the locally optimal choice at each step and trust that it leads to a globally optimal result. The trick is knowing when this works.

Recognition Signals

Greedy works when a problem has the greedy choice property (local optimum leads to global optimum) and (optimal solution contains optimal sub-solutions). You don't need to prove these formally in an interview. Recognize the signals instead:

Diagram
Rendering diagram...
  • The problem asks for a minimum or maximum of something
  • You can sort the input and process it in order
  • Making the "obvious best" choice at each step doesn't create contradictions later
  • The problem involves scheduling, intervals, or resource allocation

The Greedy Template

def greedy_solve(items):
  items.sort(key=lambda x: x.some_property)
  result = []
  for item in items:
      if is_compatible(item, result):
          result.append(item)
  return result

The hard part isn't the template -- it's choosing the right sort order and compatibility check.

When Interviewers Ask This

Greedy is tricky in interviews because the pattern isn't always obvious. Here's what they're testing:

Interview QuestionLC #Greedy InsightWhat They Want to Hear
"Can you reach the last index?"55Track farthest reachable position"I only care about max reach, not the path"
"Schedule tasks with cooldown"621Most frequent task determines min time"Fill gaps between most-frequent task"
"Minimize cost to send people to two cities"1029Sort by cost difference"Largest cost delta benefits most from cheaper city"
"Assign candies with neighbor constraint"135Two-pass (left-to-right, right-to-left)"Each pass resolves one direction independently"
"Can you complete the gas station circuit?"134If total gas >= total cost, solution exists"Reset start when tank goes negative"
"Reorganize string so no adjacent duplicates"767Always place the most frequent char next"Heap to track remaining frequencies"

The key move in all of these: explain WHY the local choice is safe, not just that you're making it.

When Greedy Fails

Greedy fails when a locally optimal choice prevents a globally optimal solution. Classic example: Coin Change with coins [1, 3, 4] and target 6. Greedy picks 4+1+1 = 3 coins, but 3+3 = 2 coins is optimal.

Diagram
Rendering diagram...

The test: Can you construct a counterexample where the greedy choice leads to a suboptimal result? If yes, you need .

This is the single most important thing to say in an interview when choosing greedy: "I tried to find a counterexample and couldn't." That's your informal proof.

Common Mistakes

  • Not sorting first. Most greedy problems require sorting. Ask yourself: what do I sort by?
  • Assuming greedy when DP is needed. If the problem has overlapping subproblems (like Coin Change), greedy breaks. Always test with a small counterexample.
  • Forgetting the complexity. Greedy solutions are usually O(n log n) due to the sort. If the interviewer wants O(n), the sort is wrong or greedy isn't the right approach.

Practice Problems

  1. Jump Game (LC 55) -- pure greedy, track farthest reach
  2. Gas Station (LC 134) -- circular greedy with a starting point
  3. Task Scheduler (LC 621) -- greedy scheduling with frequency analysis
  4. Two City Scheduling (LC 1029) -- sort by cost difference
  5. Candy (LC 135) -- two-pass greedy on a line
  6. Reorganize String (LC 767) -- greedy character placement with a heap

Sources

Practice Greedy.

Explain your thinking like you're in the interview.

Practice with Fin or Coco
Source note

Fin and Coco are StrongYes editorial personas from the Council of Ternary Vertices — a trinary-star animal civilization that studies Earth's coding-interview process. Anecdotes map animal-universe experience to human interview mechanics; they are NEVER human-career claims. External citations link to public primary sources.

StrongYes editorial guide distilled from interview coaching notes, pattern walkthroughs, and curated prep plans used across the StrongYes library.

Reviewed by Leo Kwan on Apr 12, 2026.

Practice Greedy.

Reading builds recognition. Explaining builds recall. Run these problems with Fin or Coco.