Interview concept

What are greedy algorithms?

A greedy algorithm builds a solution by taking the best immediate choice at each step. It is correct only when the problem has the greedy-choice property and optimal substructure, as in interval scheduling and Huffman coding.

Interview concept

How it works

When greedy works

It needs the greedy-choice property, where a local optimum leads to a global optimum, plus optimal substructure.

Classic examples

Interval scheduling, Huffman coding, and minimum spanning trees are canonical greedy problems.

Greedy vs DP

When greedy choices fail, dynamic programming that explores options is usually required.

Interview concept

Responsible use

Use any live AI assistant only where interview, workplace, school, and platform rules allow it. Do not use generated answers to misrepresent your skills, experience, or authorship.

FAQ

Common questions.

How do I know if greedy will work?

Prove the greedy-choice property, often with an exchange argument, or test it against dynamic programming on small cases.

What is the difference between greedy and dynamic programming?

Greedy commits to one choice per step; dynamic programming considers all choices and reuses subproblem results.