(And I mean in real life, not only for coding interviews!)
You are probably using them already, even if you don't know it, so let's explore how they work and —more importantly— when they don't.
🧵👇
Here is the gist of a greedy algorithm: it builds a solution using the best option at every single step.
Here is the intuition (for when they work): "if we make the optimal option at every step, we will end up with the optimal solution overall."
👇
An example:
How do you make 73 cents using the least amount of coins? In the US, you will do the following:
▫️2 x 25 cents
▫️2 x 10 cents
▫️3 x 1 cent
At every step, I selected the larger denomination that fit.
This was a greedy solution. It's also the optimal solution.
👇
Another example:
You want to maximize the number of movies you can watch at the theater. You know the start and end time of each movie.
Using a greedy algorithm you can get the optimal solution: at every step watch the movie that ends the earliest.
👇
Greedy algorithms are intuitive and very simple to implement.
However, they always don't give you the optimal solution!
Before deciding on whether a greedy algorithm is what you want, you need to understand whether your solution is optimal or not!
👇
Imagine that we want to find the path on the tree that produces the largest sum.
A greedy algorithm will select 10 + 20 + 40 for a total of 70.
However, the optimal solution is 10 + 1 + 100 for a total of 111!
Here, greedy is not good.
👇
The Knapsack is another classic problem where a greedy approach will not give you the optimal solution.
By the way, this problem is a classic! You want to look at it because you'll hear about Knapsack all the time. Here is the link: en.wikipedia.org/wiki/Knapsack_…
👇
Let me give you a few more examples where a greedy approach is used with optimal results:
Look at the attached video. A company using this tool will execute 100x faster than everyone else. There's simply no match for how fast AI can transform what you do.
I'm working here with @PromptQL. They will help you build a reasoning AI that is specialized to your business.
This makes an ocean of difference:
• Connect to all of your data
• Build a massive knowledge graph
• Incorporate your unique know-how
• Learn over time
The learning part is the thing that blew my mind:
You can teach the system how to interpret your data and how you prefer things to be done.
This knowledge can be reviewed, edited, and deployed so everyone at your company starts using the new version of the model.
If you have a list of things you've always wanted to solve, let an agent do them:
• Refactor code and ensure tests still run
• Find and fix bugs
• Close open tickets from your backlog
• Update documentation
• Write tests for untested code
• You can use it with any of the major models (GPT-X, Gemini, Claude)
• It has an option to Chat and Edit with the model
• It has an Agent mode to make changes to the notebook autonomously