Greedy Algorithms Interview Questions (original) (raw)

Last Updated : 18 Sep, 2025

Greedy is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. Greedy algorithms are used for optimization problems.

Easy Problems

  1. Activity Selection Problem
  2. Minimum Coins
  3. Job Sequencing
  4. Largest Number by One Swap
  5. Fractional Knapsack
  6. Minimum Platforms

Medium Problems

  1. Kruskal’s Algorithm
  2. Huffman Coding
  3. Prim’s Algorithm
  4. Dijkstra’s Shortest Path Algorithm
  5. Maximum sum of three stacks
  6. Policemen catch thieves
  7. Connect n ropes with minimum cost

Hard Problems

  1. Minimize Cash Flow
  2. Minimum time to finish all jobs
  3. Dail's Algorithm
  4. Boruvka’s algorithm
  5. Distribute candies among children

Top Theoretical Interview Questions on Greedy Algorithms

1. Explain the difference between Greedy Algorithms and Dynamic Programming. Why can’t we always use Greedy instead of DP?

**Key Reason Greedy Fails Sometimes:

**Example where Greedy fails but DP works: 0/1 Knapsack Problem.

2. In Huffman Encoding, why does the greedy approach of combining the two least frequent symbols yield the optimal prefix code?

Huffman's algorithm repeatedly combines the two symbols with the least frequencies.

**Note: Therefore, Huffman encoding produces the minimal average code length.

3. Describe a real-world scenario where the greedy algorithm does not yield the optimal solution. Why does the greedy approach fail in that case?

**Why It Fails:

**Note: Greedy lacks global foresight; optimal solutions require dynamic programming or predictive models.

4. Why does the Fractional Knapsack problem allow greedy algorithms but not the 0/1 Knapsack problem?

In Fractional Knapsack, taking a fraction of an item is allowed, enabling proportional value maximization based on value-to-weight ratio.

Greedy works because partial decisions can be adjusted at each step without restriction. In contrast, in 0/1 Knapsack, items cannot be divided and greedy choices may prevent better combinations later.

**Note: This violates the greedy choice property.

**5. How is the Greedy algorithm applied in the Coin Change problem and why does it fail for certain coin denominations?

**Greedy approach: Always pick the largest denomination coin that does not exceed the remaining amount.

**Why it fails:

**Note: Greedy Choice Property fails for arbitrary coin sets.

**6. In interval partitioning problems, why does the greedy approach of scheduling activities based on earliest start time fail to produce the minimum number of resources?

**Greedy Strategy: Sort activities by earliest start time and assign them to available resources.

**Why it fails:

**Note: Greedy approach fails because it ignores how end times affect future conflicts, violating optimal substructure.

**7. Is the Greedy strategy always faster than Dynamic Programming? Justify with time complexity arguments.

**However, speed does not mean correctness:

**Note: Greedy is not always the right choice despite being faster.

8. Explain with an example how Greedy fails in scheduling jobs with deadlines and varying profits where each job takes more than unit time.

Standard greedy approach assumes unit time jobs.

**Example:

Greedy picks Job A first (highest profit), consuming time 2, but misses Job B’s deadline. Optimal solution schedules Job B first, maximizing total profit.

**Note: Greedy fails when job durations vary and require global planning.

**9. In Minimum Number of Platforms problem (Railway Scheduling), explain why sorting arrival and departure times and processing them sequentially is optimal.

**Why optimal?

**Note: Greedy works perfectly due to the predictable linear structure of the timeline.

10. Can a greedy approach solve the Set Cover problem optimally? Why or why not?

**Why it fails to be optimal:

**Note: Greedy is useful for approximations but not guaranteed optimal for Set Cover.