To ace your coding interview for a software engineering job, you’ll need to understand greedy algorithms. Greedy algorithms solve optimization problems using the “greedy heuristic,” making locally optimal choices at each stage. Greedy algorithms are often intuitive and implemented more simply than other algorithms, but are not guaranteed to find globally optimal solutions.
Let’s take a look at some typical greedy algorithm questions.
5 typical greedy algorithm interview questions
Given the array points (balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend), return the minimum number of arrows that must be shot to burst all balloons.
You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position. Return true if you can reach the last index, or false otherwise.
Given an array of strings words, return the smallest string that contains each string in words as a substring. If there are multiple valid strings of the smallest length, return any of them.
Given two integer arrays gas and cost, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1.
Given a set of n activities with their start and finish times, we need to select the maximum number of non-conflicting activities that can be performed by a single person, given that the person can handle only one activity at a time.
Below, we take a look at 50 greedy algorithm questions and provide you with links to high quality solutions to them.
This is an overview of what we’ll cover:
Easy greedy algorithm interview questions
- Medium greedy algorithm interview questions
- Hard greedy algorithm interview questions
- How to prepare for a coding interview
Let's get started.
You might be tempted to try to read all of the possible questions and memorize the solutions, but this is not feasible. Interviewers will always try to find new questions, or ones that are not available online. Instead, you should use these questions to practice the fundamental concepts of greedy algorithms.
As you consider each question, try to replicate the conditions you’ll encounter in your interview. Begin by writing your own solution without external resources in a fixed amount of time.
If you get stuck, go ahead and look at the solution, but then try the next one alone again. Don’t get stuck in a loop of reading as many solutions as possible! We’ve analysed dozens of questions and selected ones that are commonly asked and have clear and high quality answers.
Here are some of the easiest questions you might get asked in a coding interview. These questions are often asked during the “phone screen” stage, so you should be comfortable answering them without being able to write code or use a whiteboard.
Question 1: Assign cookies
- Text guide (Medium/Fatboy Slim)
- Video guide (Nick White)
- Video guide (Kevin Naughton Jr.)
- Code example (fabrizio)
Question 2: Can place flowers
- Text guide (dilyar85)
- Video guide (NeetCode)
- Video guide (Nideesh Terapalli)
- Code example (soumyadeep2007)
Question 3: Lemonade change
Question 4: Partition array into three parts with equal sum
Question 5: Split a string in balanced strings
Question 6: Maximum units on a truck
Question 7: Minimum moves to convert string
Here are some moderate-level questions that are often asked in a video call or onsite interview. You should be prepared to write code or sketch out the solutions on a whiteboard if asked.
Question 8: Jump game
Question 9: Gas station
Question 10: Jump game II
Question 11: Remove duplicate letters
Question 12: Wiggle subsequence
Question 13: Remove K digits
Question 14: Queue reconstruction by height
Question 15: Non-overlapping intervals
- Text guide (Medium/The Startup)
- Video guide (NeetCode)
- Video guide (TECH DOSE)
- Code example (WangQiuc)
Question 16: Minimum number of arrows to burst balloons
Question 17: Task scheduler
Question 18: Maximum length of pair chain
Question 19: Dota2 senate
Question 20: Split array into consecutive subsequences
Question 21: Maximum swap
Question 22: Monotone increasing digits
Question 23: Partition labels
Question 24: Reorganize string
Question 25: Increasing triplet subsequence
Question 26: Max increase to keep city skyline
Question 27: Score after flipping matrix
Question 28: Hand of straights
Question 29: Minimum add to make parentheses valid
Question 30: Advantage shuffle
- Text guide (LeetCode)
- Text guide (Dev.to/seanpgallivan)
- Video guide (Algorithms Made Easy)
- Code example (caraxin)
Question 31: Minimum increment to make array unique
Question 32: Boats to save people
Question 33: Smallest range II
Question 34: Delete columns to make sorted II
Question 35: Bag of tokens
Question 36: String without AAA or BBB
Question 37: Array of doubled pairs
Similar to the medium section, these more difficult questions may be asked in an onsite or video call interview. You will likely be given more time if you are expected to create a full solution.
Question 38: Candy
Question 39: Create maximum number
Question 40: Patching array
Question 41: Smallest range covering elements from K lists
Question 42: Minimum cost to hire K workers
- Text guide (shengqianliu)
- Video guide (Shiran Afergan)
- Video guide (AlgoCademy)
- Code example (lee215)
Question 43: IPO
- Text guide (Massive algorithms)
- Video guide (Imran Sarwar)
- Video guide (Ren Zhang)
- Code example (shawngao)
Question 44: Set intersection size at least two
Question 45: Course schedule III
Question 46: Couples holding hands
Question 47: Minimum number of refueling stops
Question 48: Longest chunked palindrome decomposition
Question 49: Stamping the sequence
Question 50: Minimum number of taps to open to water a garden
4.1 Refresh your knowledge
Before you start practicing interviews, you’ll want to make sure you have a strong understanding of not only greedy algorithms but also the rest of the relevant algorithms and data structures. Check out our guides for detailed explanations and useful cheat sheets.
- Depth-first search
- Breadth-first search
- Binary search
- Dynamic programming
- Greedy algorithm
- Divide and conquer
Data structure guides:
4.2 Practice on your own
Once you’re confident in all of these, you’ll want to start working through lots of coding problems.
For greedy algorithms you can obviously use the questions we’ve listed above. For the other algorithms you need to know, check out the rest of this series:
- Depth-first search interview questions
- Breadth-first search interview questions
- Binary search interview questions
- Sorting interview questions
- Dynamic programming interview questions
- Backtracking interview questions
- Divide and conquer interview questions
We also recommend working through our list of 73 data structure interview questions.
One of the main challenges of coding interviews is that you have to communicate what you are doing as you are doing it. Talking through your solution out loud is therefore very helpful. This may sound strange to do on your own, but it will significantly improve the way you communicate your answers during an interview.
It's also a good idea to work on a piece of paper or google doc, given that in the real interview you'll have to code on a whiteboard or virtual equivalent.
3.3 Practice with others
Of course, if you can practice with someone else playing the role of the interviewer, that's really helpful. But sooner or later you’re probably going to want some expert interventions and feedback to really improve your coding interview skills.
That’s why we recommend practicing with ex-interviewers from top tech companies. If you know a software engineer who has experience running interviews at a big tech company, then that's fantastic. But for most of us, it's tough to find the right connections to make this happen. And it might also be difficult to practice multiple hours with that person unless you know them really well.
Here's the good news. We've already made the connections for you. We’ve created a coaching service where you can practice 1-on-1 with ex-interviewers from leading tech companies. Learn more and start scheduling sessions today.
Any questions about greedy algorithms?
If you have any questions about greedy algorithms or coding interviews in general, don't hesitate to ask them in the comments below. All questions are good questions, so go ahead!