DEV Community

Cover image for Guide to LeetCode: A Personal Guide to the Most Common and Useful Patterns for Solving Algorithm Questions
Mario Ruci
Mario Ruci

Posted on

Guide to LeetCode: A Personal Guide to the Most Common and Useful Patterns for Solving Algorithm Questions

Are you tired of struggling with algorithm questions on LeetCode? As someone (with 5+ years of experience) who has started practicing and studying these problems, I can relate to the frustration of not knowing where to start or how to approach a particularly tricky question. After much trial and error, I have come up with a personal guide to the most common and useful patterns for solving algorithm questions, which I hope can help others navigate this challenging terrain.

For each pattern I have listed 10 LeetCode questions to practice on and the list is sorted by learning curve.

 

  1. Brute Force - Beginner

Start simple. Try all possible solutions and check if they work.

Practice:

 

2. Greedy - Beginner

Make the locally optimal choice at each step and hope that it leads to a global optimum solution.

Practice:

 

3. Two-Pointers - Beginner

Use two pointers to traverse an array or a linked list simultaneously.

Practice:

 

4. Sliding Window - Beginner

Maintain a window of elements in an array or a string and move the window to find the solution. Combined with 2 pointers.

Practice:

 

5. Backtracking - Intermediate

Systematically explore all possible solutions by building candidates and then abandoning them as soon as you determine that they cannot lead to a valid solution.

Practice:

 

6. Depth-First Search (DFS) - Intermediate

Traverse a graph or tree by exploring as far as possible along each branch before backtracking.

Practice:

 

7. Bepth-First Search (BFS) - Intermediate

Traverse a graph or tree by exploring all the neighbors at the current level before moving on to the next level.

Practice:

You can practice the same as DFS and the following.

 

8. Divide and Conquer - Advanced

Break down a problem into smaller sub-problems, solve each sub-problem independently, and then combine the solutions to get the final result.

 

9. Dynamic Programming - Advanced

Break down a complex problem into smaller sub-problems and solve them individually. This approach can be used to solve problems that exhibit overlapping subproblems.

Practice:

Notes:

  1. I would suggest you start with some data structure basics, then solve around 20 easy questions to get an idea of the questions and their formatting
  2. These are not all the patterns that exist, and the questions are not listed by difficulty. Each pattern might have variations or be combined with another one. By practicing you will get better.
  3. The questions listed for practice might not be the most suitable ones, but you get the idea.
  4. This list serves as a starting point for those who don't know how to get started. Follow the path or just create your own, "memento mori".

Thanks for reading.

Top comments (5)

Collapse
 
krlz profile image
krlz • Edited

Awesome, dude! Thanks a lot for this amazing opportunity! I've been working for this small company for the last five years, and I've become really familiar with our business model and the products we create as a team. But, man, I gotta tell you, when I started tackling some of these exercises, I was blown away! I struggled with some of the beginner-level exercises, and that just made me more eager to learn even more! I think it's super important to always stay up-to-date with the latest techniques and tricks, and not get too comfortable in our own bubble

Collapse
 
mrmarioruci profile image
Mario Ruci

Happy i could help a bit. A good platform where you can learn more in detail that I found is neetcode.io.

Collapse
 
krlz profile image
krlz • Edited

I will take a look thanks a lot!

Collapse
 
liopun profile image
Captone Habiyaremye

In 2023, with ChatGPT you can level up your Leetcode game and problem solving skills with this open source tool: leetchatgpt.com/

youtube.com/watch?v=yQ7m25nEJTA

Collapse
 
mrmarioruci profile image
Mario Ruci

Sure, cool tool.