Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
PROBLEM-SOLVING TECHNIQUES

Greedy Algorithms

Take the locally best choice at each step - when you can prove it’s safe.

Intermediate9 problems3 interactive subtopicsO(n) time · O(1) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/4From index 0, reach 2
farthest = 0 + 2 = 2
01234
2
3
1
1
4
i
  • active this step
  • inside the window
  • visited / done
  • result
Space Play←→ StepR Replay
1 / 4
PYTHONnums = [2, 3, 1, 1, 4]
1def can_jump(nums):
2 farthest = 0
3 for i, step in enumerate(nums):
4 if i > farthest:
5 return False
▶6 farthest = max(farthest, i + step)
7 return True
STEP 1 OF 4Setup

From index 0, reach 2

0 + 2 = 2 is the farthest index reachable so far.

VARIABLES

i
0
farthest
2
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

A greedy algorithm commits to the best-looking option now and never revisits it. It works when a local choice can be shown to never hurt the final answer.

Why it matters

When it applies, greedy is usually O(n) or O(n log n) and much simpler than DP.

Think of it like this

Paying in rupees with the fewest notes: take the largest note that fits, then repeat. That works for standard currency, not for every coin system.

Use it when

  • Interval scheduling - pick whatever finishes earliest
  • Reachability, as in Jump Game
  • Problems with an exchange-argument proof

Avoid it when

  • A counter-example exists - coins [1, 3, 4] for amount 6: greedy uses 3 coins, the best is 2
  • You need every solution or exact counts

Complexity cheatsheet

OperationTimeSpace
Single greedy passO(n)O(1)
Sort, then greedyO(n log n)O(1) to O(n)
DP alternativeoften O(n²)O(n)

Often confused with: dynamic programming - it weighs every choice; greedy commits to one.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

minimum number ofmaximum number ofcan you reachscheduleassign
  • Sorting by end time or by a ratio is a common first step
  • If you can’t prove it, test a small counter-example before committing
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Assuming greedy works without a proof or a counter-example check
  • Sorting by the wrong key - start time instead of end time for scheduling
  • Stopping early when a later element could extend the reach

Edge cases to test

  • A single element - already at the end
  • A zero that blocks progress, like [3, 2, 1, 0, 4]
  • All zeros after the first element
Interview tip: Give a one-line exchange argument: “swapping any choice for the greedy one never makes the answer worse.”
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Coins [1, 3, 4], amount 6. Greedy takes 4 first. What is the best possible?

06

Practice

Apply what you just watched on real interview problems.

Generate Greedy problem in AI DSA Practice →
  • BeginnerAssign CookiesLeetCode 455 ↗ (opens in a new tab)
  • IntermediateJump GameLeetCode 55 ↗ (opens in a new tab)
  • AdvancedCandyLeetCode 135 ↗ (opens in a new tab)
BacktrackingDynamic Programming