CONTENTS

Greedy Algorithms for Coding Interviews

Learn when greedy works, how to prove the choice, and common interval and reachability patterns.

May 29, 2026
43
A

Greedy is not "pick what looks best" unless you can explain why that local choice never blocks the global optimum. The proof is usually an exchange argument or an invariant.

Greedy Patterns

PatternInvariantLesson
Farthest reachIf index is beyond reach, all future paths failJump Game
Reset startNegative local tank invalidates the whole candidate rangeGas Station
Earliest endFinishing earlier leaves more future roomNon-overlapping Intervals
Last boundaryA partition must include every last occurrence inside itPartition Labels

Greedy Checklist

  1. Define the local choice.
  2. Show what the choice preserves.
  3. Explain why delaying or changing the choice cannot improve the result.

FAQs

How do I know greedy is safe?

You need an invariant or exchange argument. If you cannot state one, try DP or backtracking first.

Are interval problems always greedy?

No, but many become greedy after sorting by end time or start time depending on the target.

Share this article

0 comments

Please login to comment.
No comments yet.