Skip to content
Learn Netverks

Lesson

Step 31/36 86% through track

greedy-intro

Greedy algorithms intro

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

An orientation to the DSA track—Big-O, classic structures, graph/DP patterns, and C++ playground practice for interviews.

You need explicit complexity reasoning before scaling services or passing FAANG-style loops—brute force rarely survives review.

You will apply Greedy algorithms intro in contexts like: Resource allocation, sequence alignment, and knapsack-style product trade-offs.

Compile and run C++17 snippets in the playground (`int main`, `std::cout`); after each run, state time and space complexity before moving on. Also read the interview prep blocks; state Big-O aloud before and after you change the code.

After basic /python/intro or /cpp/intro syntax—ideally alongside /numpy/intro for array thinking; intensify before interview season.

A greedy algorithm makes the locally best choice hoping for a global optimum. Works when problem has greedy choice property and optimal substructure—e.g. activity selection, Huffman coding preview.

When greedy works

Prove exchange argument or stay greedy-safe—otherwise use DP. Counterexample: coin systems {1,3,4} target 6—greedy 4+1+1 beats 3+3 depending on definition; canonical coin systems matter.

Activity selection sketch

Sort by finish time; pick non-overlapping activities greedily—O(n log n) sort + O(n) scan.

Minimum coins (canonical)

#include 
#include 

int main() {
    std::vector coins = {25, 10, 5, 1};
    int amount = 63, used = 0, count = 0;
    for (int c : coins) {
        int take = amount / c;
        count += take;
        amount -= take * c;
    }
    std::cout << "coins used=" << count << " left=" << amount << "\n";
    return 0;
}

Important interview questions and answers

  1. Q: Greedy vs DP?
    A: Greedy one decision, no table; DP explores overlapping states when greedy fails.
  2. Q: Proof in interviews?
    A: Often cite sorting key + exchange argument—show why local choice safe.

Self-check

  1. When is greedy incorrect without proof?
  2. What is complexity of sort-then-scan greedy?

Pitfall: Greedy without proof fails coin-change and some scheduling variants—mention counterexamples.

Interview prep

When greedy works?

Greedy choice property + optimal substructure—needs proof.

vs DP?

Greedy no table; DP when overlapping states need caching.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Greedy proof?
  • When greedy fails?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump