Greedy Coding Problems: 163 Questions with Solutions

163 greedy coding problems — 35 easy · 106 medium · 22 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.

  • Problems: 163
  • By difficulty: 35 easy · 106 medium · 22 hard
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
  • Cost: Free on every plan; sign in to run and submit

A greedy algorithm makes the locally best choice at each step and never revisits it. It is fast and simple when it is right — scheduling by earliest finish, jumping as far as possible, taking the largest coin — and quietly wrong when it is not. The problems here practise both halves: spotting the exchange argument that proves the greedy choice safe, and recognising the cases where only dynamic programming or search will do.

How greedy works, step by step

nums10310203240516reach = 6path: 0 → 1 → 4 → 6
Jump game, with a greedy farthest-reach scan. Example: nums = [1, 3, 0, 0, 2, 0, 1]
  1. reach is the farthest index known to be reachable. It starts at 0, where we stand; every index up to reach can be landed on, and nothing past it is known to be reachable yet.
  2. From index 0 a jump of up to 1 lands as far as 1, so reach = 1. Shorter jumps land inside the stretch already covered, which is why only the farthest point matters.
  3. Index 1 is within reach, and from it a jump of 3 lands at 4, so reach grows to 4: every index from 0 to 4 is now reachable.
  4. nums[2] = 0 goes nowhere, but that is no trap: reach is already 4, so index 3 is still reachable and the scan carries on.
  5. nums[3] = 0 adds nothing either, since 3 + 0 = 3 is behind reach = 4; a zero only traps the scan when reach stops at it.
  6. From index 4 a jump of 2 lands at 6, the last index, so reach = 6 covers the end and the scan can stop.
  7. The end is reachable, so the answer is true: 0 → 1 → 4 → 6 works, using the jumps that pushed reach forward. Had i ever passed reach, it would be false. One pass and one number kept: O(n) time, O(1) space.

Greedy study plan

14 of the 163 Greedy problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 149 in the full list below are practice at your own pace. Then move on to Recursion.

Day 1

Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

Day 2

Medium problems: the same pattern with one twist each. Name the twist before you code.

Day 3

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 4

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 5

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 6

Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

Day 7

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Day 8

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Next topic: Recursion

Greedy: the essentials

When to reach for it

An optimisation over a sequence of choices with an obvious local rule — the interval that ends earliest, the farthest reachable index, the smallest item that still fits — and an input size (10⁵ or more) that rules out a table over every state. The first line of the solution is usually a sort by the key the rule uses.

The pattern

State the rule, then try to break it before coding: hunt for a small input where the local choice blocks a better total. If you cannot, sketch the exchange argument — take any optimal solution, swap its first choice for the greedy one, and show the result is no worse. The code is then one pass that commits to each choice and never looks back.

def can_jump(nums):
    reach = 0                       # farthest index reachable so far
    for i, step in enumerate(nums):
        if i > reach:
            return False
        reach = max(reach, i + step)
    return True

Cost

Usually O(n log n) for the sort plus O(n) for the pass, with O(1) extra space; rules that keep the best few candidates in a heap stay at O(n log n).

Common mistakes

  • Trusting a rule because it passes the examples. Coins {1, 3, 4} making 6: largest-first gives 4 + 1 + 1, but 3 + 3 needs only two coins — that problem is DP.
  • Sorting by the wrong key: selecting the most non-overlapping intervals needs end times, not start times or lengths.
  • Ties the proof never considered.
  • Committing for good to a choice the problem lets you revise; keep the candidates in a heap and swap the worst one out instead.

Start with

All greedy problems

Easy (35)

Medium (106)

Hard (22)

Companies that ask greedy problems

Next topic: Recursion