If you're a software engineer preparing for a coding interview at a top tech company like Facebook, Google, or Amazon, you'll need to practice solving plenty of algorithm problems.
That's why we've compiled a comprehensive list of 71 typical questions grouped by type (DFS, BFS, sorting, etc.) and included links to high quality solutions.
We've also included the ultimate cheat sheet, giving you key information on space-time complexity for each algorithm at a glance.
Here's a summary of what you'll find below:
- Algorithms cheat sheet
- Depth-first search questions
- Breadth-first search questions
- Binary search questions
- Sorting questions
- Dynamic programming questions
- Greedy algorithm questions
- Backtracking questions
- Divide and conquer questions
- How to prepare for a coding interview
Let's get into it!
Click here to practice coding interviews with ex-FAANG interviewers
1. The ultimate algorithms cheat sheet
When you're preparing for coding interviews, it helps to have at least some of the key information in one place. We've created an 8-page cheat sheet, which you can use as a handy reference point for the 8 main algorithms.
Click here
Right, let's take a look at some typical algorithm questions.
2. Depth-first search
Depth-first search (or DFS) is a traversal option for trees or graphs in which child nodes are visited before their siblings on the same layer.
2.1 Easy depth-first search questions
Question 1: Binary tree inorder traversal
- Text guide (Dev.to/javinpaul)
- Video guide (DEEPTI TALESRA)
- Code example (OldCodingFarmer)
Question 2: Same tree
- Text guide (Fatal Errors)
- Text guide (LeetCode)
- Video guide (NeetCode)
- Code example (OldCodingFarmer)
Question 3: Symmetric tree
- Text guide (Baeldung)
- Text guide (Medium/Hary Krishnan)
- Video guide (Back To Back SWE)
- Code example (lvlolitte)
2.2 Medium depth-first search questions
Question 4: Surrounded regions
- Text guide (After Academy)
- Video guide (NeetCode)
- Video guide (Nick White)
- Code example (Chronoviser)
Question 5: Clone graph
- Text guide (Medium/Deeksha Sharma)
- Video guide (Michael Muinos)
- Video guide (NeetCode)
- Code example (mohamede1945)
Question 6: Number of islands
- Text guide (Codertrain)
- Video guide (Kevin Naughton Jr.)
- Code example (girikuncoro)
Question 7: Course schedule
-
- Text guide (Yu’s Coding)
- Video guide (NeetCode)
- Video guide (Nideesh Terapalli)
- Code example (varunu28)
2.3 Hard depth-first search questions
Question 8: Binary tree maximum path sum
- Text guide (After Academy)
- Video guide (NeetCode)
- Video guide (Michael Muinos)
- Code example (arkaung)
Question 9: Alien dictionary
- Text guide (Medium/Feng Wang)
- Video guide (NeetCode)
- Video guide (happygirlzt)
- Code example (Tenderleo)
Question 10: Serialize and deserialize binary tree
- Text guide (Baeldung)
- Video guide (Back To Back SWE)
- Code example (gavinlinasd)
How did you get on? To practice with more depth-first search questions like this, check out our list of 50+ depth-first search questions with solutions.
3. Breadth-first search
Breadth-first search (or BFS) is one traversal method for trees and graphs in which all vertices on one layer are visited before visiting their children on the next layer – i.e. every node on layer i is visited before the nodes on layer i+1.
3.1 Easy breadth-first search questions
Question 11: Maximum depth of binary tree
- Text guide (Aaronice)
- Video guide (NeetCode)
- Code example (makuiyu)
Question 12: Minimum depth of binary tree
- Text guide (After Academy)
- Video guide (Terrible Whiteboard)
- Code example (simkieu)
Question 13: Maximum depth of N-ary tree
- Text guide (Medium/Annamariya Tharayil)
- Video guide (Nick White)
- Code example (TKroos)
3.2 Medium breadth-first search questions
Question 14: Binary tree level order traversal
- Text guide (Educative)
- Video guide (Kevin Naughton Jr.)
- Video guide (Nick White)
- Code example (SOY)
Question 15: Binary tree zigzag level order traversal
- Text guide (zhenyu0519)
- Video guide (Michael Vandi)
- Code example (fangkunjnsy)
Question 16: Binary tree level order traversal II
- Text guide (Medium/Navneet Ojha)
- Video guide (Terrible Whiteboard)
- Code example (OldCodingFarmer)
3.3 Hard breadth-first search questions
Question 17: Word ladder
- Text guide (Medium/Nathan Brickett)
- Video guide (Nick White)
- Code example (jianchao-li)
Question 18: Word ladder II
- Text guide (medium/spylogsster)
- Video guide (TECH DOSE)
- Code example (antdavid)
Question 19: Cut off trees for golf event
- Text guide (Zirui)
- Video guide (Shivam Patel)
- Code example (shawngao)
Keen to work through some more? See our article, 44 breadth-first search questions and solutions. That should keep you busy for a while!
4. Binary search
Binary search is one of the fastest search algorithms because it halves the search space with each iteration. It requires an ordered set that also has constant access times, meaning that only sorted arrays are suitable for binary search.
4.1 Easy binary search questions
Question 20: Search insert position
- Text guide (GeeksForGeeks)
- Video guide (NeetCode)
- Video guide (TECH DOSE)
Question 21: Sqrt(x)
- Text guide (GoodTecher)
- Video guide (Terrible Whiteboard)
- Code example (AlexTheGreat)
Question 22: First bad version
- Text guide (Medium/Mac Sampson)
- Video guide (Kevin Naughton Jr.)
- Code example (Cheng_Zhang)
4.2 Medium binary search questions
Question 23: Search in a rotated sorted array
- Text guide (Educative)
- Video guide (NeetCode)
- Video guide (Errichto)
- Code example (1337beef)
Question 24: Find first and last position of element in sorted array
- Text guide (Enjoy Algorithms)
- Video guide (Nick White)
- Code example (stellari)
Question 25: Search a 2D matrix
- Text guide (TheLeanProgrammer)
- Video guide (NeetCode)
- Code example (LeetCode)
4.3 Hard binary search questions
Question 26: Median of two sorted arrays
- Text guide (Medium/hamid)
- Video guide (NeetCode)
- Code example (MissMary)
Question 27: Count of smaller numbers after self
- Text guide (Seanforfun)
- Video guide (happygirlzt)
- Code example (mayanist)
Question 28: Find minimum in rotated sorted array II
- Text guide (GraceMeng)
- Video guide (Naresh Gupta)
For more questions like these, check out our list of 50 binary search questions and solutions.
5. Sorting
A sorting algorithm can be performed on arrays or linked lists, in order to rearrange elements according to a series of instructions. Many algorithms require, or perform better on, a sorted dataset.
5.1 Easy sorting questions
Question 29: Contains duplicate
- Text guide (Medium/punitkmryh)
- Video guide (Nick White)
- Code example (jmnarloch)
Question 30: Valid anagram
- Text guide (Project Debug)
- Video guide (Nick White)
- Code example (OldCodingFarmer)
Question 31: Meeting rooms
- Text guide (Aaronice)
- Video guide (NeetCode)
- Code example (Seanforfun)
5.2 Medium sorting questions
Question 32: Sort an array
- Text guide (cc189)
- Video guide (leetuition)
- Code example (HaelChan)
Question 33: 3Sum
- Text guide (fizzbuzzed)
- Video guide (Nick White)
- Video guide (NeetCode)
- Code example (shpolsky)
Question 34: H-index
- Text guide (TitanWolf)
- Video guide (Algorithms Made Easy)
- Code example (yfcheng)
5.3 Hard sorting questions
Question 35: Maximum gap
- Text guide (Buttercola)
- Video guide (Coding Decoded)
- Code example (zkfairytale)
Question 36: Merge k sorted lists
- Text guide (After Academy)
- Video guide (NeetCode)
- Video guide (Kevin Naughton Jr.)
- Code example (reeclapple)
Question 37: Count of smaller numbers after self
- Text guide (Mithlesh Kumar)
- Video guide (happygirlzt)
- Code example (confiscate)
Need some more sorting questions to practice with? No problem, we've got plenty more here: 54 sorting questions and solutions.
6. Dynamic programming
Dynamic programming is an algorithmic paradigm used to create optimal solutions for complex problems by breaking them down into simpler sub-problems that can be solved recursively.
6.1 Easy dynamic programming questions
Question 38: Maximum subarray
- Text guide (Baeldung)
- Video guide (Back to Back SWE)
- Code example (FujiwaranoSai)
Question 39: Climbing stairs
- Text guide (Medium/Analytics Vidhya)
- Video guide (NeetCode)
- Code example (liaison)
Question 40: Pascal's triangle
- Text guide (Dev.to/seanpgallivan)
- Video guide (Nick White)
- Video guide (NeetCode)
- Code example (rheaxu)
6.2 Medium dynamic programming questions
Question 41: Longest palindromic substring
- Text guide (RedQuark)
- Video guide (NeetCode)
- Video guide (Errichto)
Question 42: Unique paths
-
Text guide (Medium/Arpit Choudhary)
-
Video guide (NeetCode)
-
Video guide (Kevin Naughton Jr.)
- Code example (jianchao-li)
Question 43: Unique paths II
-
Text guide (Medium/Nerd For Tech)
-
Video guide (TECH DOSE)
-
Code example (tusizi)
6.3 Hard dynamic programming questions
Question 44: Regular expression matching
- Text guide (RedQuark)
- Video guide (Tushar Roy)
- Video guide (NeetCode)
- Code example (LeetCode)
Question 45: Maximal rectangle
- Text guide (Rohith Vazhathody)
- Video guide (Knapsak)
- Video guide (TECH DOSE)
- Code example (morrischen2008)
Question 46: Edit distance
- Text guide (After Academy)
- Video guide (Back to Back SWE)
- Video guide (Tushar Roy)
- Code example (anderson5)
Check out plenty more dynamic programming questions here: 53 dynamic programming questions and solutions.
7. Greedy algorithms
A greedy algorithm is an algorithmic paradigm that finds the optimal solution to a problem by breaking the problem down into smaller (local) parts and finding the best solution for each of these parts.
7.1 Easy greedy algorithm questions
Question 47: Assign cookies
- Text guide (Medium/Fatboy Slim)
- Video guide (Nick White)
- Video guide (Kevin Naughton Jr.)
- Code example (fabrizio)
Question 48: Can place flowers
- Text guide (dilyar85)
- Video guide (NeetCode)
- Video guide (Nideesh Terapalli)
- Code example (soumyadeep2007)
Question 49: Lemonade change
- Text guide (Tutorial Cup)
- Video guide (Nick White)
- Code example (lee215)
7.2 Medium greedy algorithm questions
Question 50: Jump game
- Text guide (Learnbay)
- Video guide (NeetCode)
- Code example (1337beef)
Question 51: Gas station
- Text guide (After Academy)
- Video guide (NeetCode)
- Code example (daxianji007)
Question 52: Jump game II
- Text guide (Medium/Nerd For Tech)
- Video guide (NeetCode)
- Code example (Cheng Zhang)
7.3 Hard greedy algorithm questions
Question 53: Candy
- Text guide (LeetCode)
- Video guide (Algorithms Made Easy)
- Code example (meng789987)
Question 54: Create maximum number
- Text guide (Medium/dume0011)
- Code example (dietpepsi)
Question 55: Patching array
- Text guide (HackingNote)
- Video guide (Timothy H Chang)
- Code example (StefanPochmann)
For plenty more greedy algorithm questions, see 50 greedy algorithm interview questions.
8. Backtracking
Backtracking is a form of brute-force problem solving, but with the ability to discard potential solutions early, before they are fully explored. It is an algorithmic paradigm for incrementally finding solutions to problems.
8.1 Easy backtracking questions
Backtracking questions don't tend to be very easy, so we've only included one example in this first section.
Question 56: Binary watch
- Text guide (Medium/Competitive Programming)
- Video guide (Owen Smith)
- Code example (xietao0221)
8.2 Medium backtracking questions
Question 57: Letter combinations of a phone number
- Text guide (After Academy)
- Video guide (NeetCode)
- Video guide (Kevin Naughton Jr.)
- Code example (lei31)
Question 58: Generate parentheses
- Text guide (RedQuark)
- Video guide (Back to Back SWE)
- Code example (brobins9)
Question 59: Permutations
- Text guide (issac3)
- Video guide (NeetCode)
- Code example (OldCodingFarmer)
8.3 Hard backtracking questions
Question 60: Word break II
- Text guide (Java questions)
- Video guide (babybear4812)
- Code example (Cheng_Zhang)
Question 61: Sudoku solver
- Text guide (After Academy)
- Video guide (Back To Back SWE)
- Code example (LeetCode)
Question 62: Stickers to spell word
- Text guide (linlaw)
- Video guide (NeetCode)
- Code example (zestypanda)
You can find lots more backtracking questions to practice with right here: 47 backtracking interview questions.
9. Divide and conquer
Divide and conquer is an algorithmic paradigm used to solve problems by continually dividing the problem into smaller parts until a part is easy enough to solve (conquer) on its own. The solutions to the solved parts are then combined to give the solution for the original problem.
9.1 Easy divide and conquer questions
Question 63: Maximum subarray
- Text guide (Techie Delight)
- Video guide (Ghassan Shobaki)
- Code example (jianchao-li)
Question 64: Majority element
- Text guide (enjoy algorithms)
- Video guide (Nideesh Terapalli)
- Code example (coderoath)
Question 65: First bad version
- Text guide (Studytonight)
- Video guide (TECH DOSE)
- Video guide (Kevin Naughton Jr.)
- Code example (RainbowSecret)
9.2 Medium divide and conquer questions
Question 66: Kth largest element in an array
- Text guide (Coder’s Cat)
- Video guide (NeetCode)
- Code example (jmnarloch)
Question 67: Search a 2D matrix II
- Text guide (Medium/Nerd for tech)
- Video guide (Back to Back SWE)
- Code example (LeetCode)
Question 68: Longest substring with at least K repeating characters
- Text guide (Medium/dume0011)
- Video guide (Knowledge Center)
- Code example (cdpiano)
9.3 Hard divide and conquer questions
Question 69: Median of two sorted arrays
- Text guide (Medium/hamid)
- Video guide (NeetCode)
- Code example (MissMary)
Question 70: Reverse pairs
- Text guide (Tutorialhorizon)
- Video guide (take U forward)
- Code example (fun4LeetCode)
Question 71: Count of smaller numbers after self
- Text guide (Programmerall)
- Video guide (happygirlzt)
- Code example (confiscate)
For lots more divide and conquer questions, see our article 50 divide and conquer interview questions and cheat sheet.
Need a boost in your career as a software engineer?
If you want to improve your skills as an engineer or leader, tackle an issue at work, get promoted, or understand the next steps to take in your career, book a session with one of our software engineering coaches.
10. How to prepare for a coding interview
If you're reading this article, it probably means that you're already up and running on the most important step towards preparing to ace a coding interview at Facebook, Google, Amazon, Microsoft, LinkedIn, Airbnb or another tech company: practicing a lot of questions.
However, that's not all you need to be confident of killing it in an interview situation. To make sure you're really, truly prepared, we recommend following the three steps below. For extra tips, take a look at our list of 21 coding interview tips from ex-interviewers.
10.1 Refresh your knowledge
You'll need to be confident on all the algorithms we've gone through here. To refresh your knowledge, and to review data structures, check out our specific guides:
Algorithms explained:
- Depth-first search
- Breadth-first search
- Binary search
- Sorting
- Dynamic programming
- Greedy algorithm
- Backtracking
- Divide and conquer
Data structure guides:
To review big-O notation in order to assess the time and space requirements of your algorithms, study our complete guide on big-O notation and complexity analysis.
10.2 Learn a framework
We recommend getting used to the step-by-step approach explained in the video below. The video was made by Amazon, but it's relevant for coding interviews at any tech company.
Here is a summary of the approach:
- Step 1: Clarify
- Ask clarification questions to remove ambiguity about the problem
- Explore the edges of the problem
- Step 2: Plan
- Discuss potential approaches you could take
- Pick an approach and lay out the high level steps
- Step 3: Implement
- Write clean code, not pseudocode
- Comment on your code as you go
- Step 4: Test
- Start by testing with a simple example
- Try breaking your code with edge and corner cases
- Step 5: Optimize
- Calculate time complexity
- Discuss how you can optimize your solution
For a more complete breakdown of this framework, plus a full example answer, take a look at our guide on how to answer coding interview questions.
Once you're comfortable with a framework, you'll want to start putting it into practice. This brings us to the next step:
10.3 Practice on your own
To practice algorithm questions, we recommend going through the questions we've listed above and trying to solve each one. If you find you need more questions on any of the topics, simply click on the links below:
- Depth-first search interview questions
- Breadth-first search interview questions
- Binary search interview questions
- Sorting interview questions
- Dynamic programming interview questions
- Greedy algorithm interview questions
- Backtracking interview questions
- Divide and conquer interview questions
For data structure questions, we recommend practicing with our list of 73 data structure interview questions. Don't forget to practice questions on a whiteboard or simple text editor instead of an IDE.
10.4 Practice with others
Practicing solving coding problems on your own is a great way to prepare, but it's not enough. If you make it to the onsite interview stage, you'll be expected to code on a whiteboard (or virtual equivalent) and talk through your approach as you code. This can be really tricky if you're not used to it.
That's why we recommend practicing with experts who can give you insightful feedback. If you know a software engineer or similar who has experience running interviews at the company you're interviewing at, 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 like Facebook, Google, and Amazon. Learn more and start scheduling sessions today.