Greedy Algorithm Interview Questions
How to approach greedy algorithm questions
A greedy algorithm builds a solution one step at a time, always taking the option that looks best right now and never going back to change an earlier choice. In interviews, a greedy solution often looks like a single pass through an array that keeps track of the best answer so far, which can turn an O(n²) brute-force solution into an O(n) one.
The hard part is knowing when greedy is safe. Before you commit, try to break your idea with a small counterexample, and be ready to explain why the locally best choice never rules out the best overall answer. If you find a counterexample, the problem probably needs dynamic programming instead.
A good way in: start from the brute-force solution, then ask what you'd need to remember as you walk through the input once so that you could drop the inner loop.
Interview Cake isn't just another question database. Each question below walks you through the problem step by step, with hints and explanations as you need them, just like a real interviewer.
Greedy algorithm practice questions
Given yesterday's stock prices, find the max profit from one buy and one sell. A classic interview problem, solved in O(n) time and O(1) space. keep reading »
Highest Product of 3 »Find the highest possible product that you can get by multiplying any 3 numbers from an input array. keep reading »
Product of All Other Numbers »Given an array of integers, return an array where each item is the product of all the others, without division, in O(n) time. Step-by-step solution. keep reading »
Cafe Order Checker »Write a function to tell us if cafe customer orders are served in the same order they're paid for. keep reading »
In-Place Shuffle »Do an in-place shuffle on an array of numbers. It's trickier than you might think! keep reading »
Keep going with the Greedy algorithms section of our coding interview course.
Concepts to review
- Greedy algorithms: What makes an algorithm greedy, and when the greedy choice gives the right answer.
- Brute force: The baseline a greedy solution improves on, and a good place to start.
- Bottom-up dynamic programming: The usual fallback when the greedy choice isn't always safe.
- In-place algorithms: Working with O(1) extra space, as in an in-place shuffle.
Quick reference: Big O cheat sheet
Greedy algorithm interview FAQ
What is a greedy algorithm?
How do I know if a greedy approach will work?
Can a greedy algorithm give the wrong answer?
What's the difference between greedy algorithms and dynamic programming?
What are some common greedy algorithm interview questions?
More interview questions by topic
- Array and string interview questions
- Hash table interview questions
- Sorting and binary search interview questions
- Binary tree and BST interview questions
- Graph interview questions
- Dynamic programming and recursion interview questions
- Stack and queue interview questions
- Linked list interview questions
- Bit manipulation interview questions
- System design interview questions
- All coding interview questions
Interview Cake