6 Easy Steps to Master Any LeetCode Problem

Mastery of any LeetCode question can be broken down to 6 steps

TheConnoisseur
7 min readAug 24, 2023

LeetCode, whether you do it for fun or do it for interview practise, we all have our systems and ways of tackling questions.

But how many LeetCode questions have you truly mastered?

It’s not about how many problems you’ve solved, it’s about how many you truly understand and your problem solving skills, mastery of the problem

Mastery is being able to take what you’ve learnt from a question and apply it to other similar questions to produce a quality result there as well. Just solving a problem will not get you this deep understanding.

Luckily, mastering a LeetCode problem can be broken down into 6 simple steps.

How Should You Tackle Each Question?

I like to break tackling a LeetCode question into these 6 simple steps:

  1. Understand the problem
  2. Attempt to plan out and solve the problem with no help
  3. Attempt to solve the problem with help, if you haven’t already
  4. Can you improve the time and space complexity of your solution?
  5. Look at a sample solution and video: takes notes if you want to
  6. Review the question at a later date

Now let’s go through what each of these mean.

We’ll use this classic LeetCode question as an example as we go through each step

1. Understanding the Problem

We would start by reading the question and understanding exactly what it wants us to do.

To help with this, we can read the examples LeetCode gives us to better understand any how the solution should work and clear up any questions about potential edge cases we may have had.

Reading the constraints is important as it could prohibit some potential solutions you may already have in mind.

Here, the constraints tell us the maxium and minimum size of the array being passed to us and the ranges of values that the array can hold.

So looking at all this, I can boil this problem down to “Check if every element in the array is unique” or “Does any element in the array that appears more than once.”

Putting the problem in your own words is a powerful way to solidify your understanding of the problem and show a potential interviewer you know what you’re doing.

Now we have a solid understanding of the problem we are looking to solve, let’s move onto solving it.

2. Plan Out and Solve The Solution With No Help (Or Attempt To)

Before even thinking about solving the problem, plan something out. This doesn’t need to be anything elaborate, just an idea in your head of how you want to solve the problem.

You should think about things like what data structures and algorithms am I going to use, what’s the time/space complexity of my idea and so on.

When you have something you think will work, you don’t need to be certain to try it out, then open your favourite IDE and code it out.

If your solution works, great! And if it doesn’t then don’t get too hung up about it. Have a few more attempts and if you can’t figure it out at all in around 30 minutes then move onto the next step.

Remember, the purpose of solving these LeetCode questions is to develop critical problem solving skills and knowledge of data structures and algorithms. Some questions you just won’t have the knowledge to solve them blind so there’s no shame in looking things up, as long as you learn from it.

So for our contains duplicate problem, I might come up with a plan of sorting the array and then looping through it and seeing if any two consecutive elements are the same. The time and space complexity of this will depend on the search algorithm used, but let’s assume O(nlogn) time and O(1) space.

Now I have a plan I would code something out like this:

class Solution(object):
def containsDuplicate(self, nums):
nums.sort()
for i in range(len(nums)-1):
if nums[i] == nums[i+1]:
return True
return False

Great this works! Let’s move on.

3. Attempt to Solve The Problem With Help, If You Haven’t Already

If you didn’t manage to solve the problem blind with no help, then there is no shame in looking up algorithms and data structures that might help you.

Just remember, LeetCode is about learning. So as long as you have learnt something, you gained value out of the question.

For resources use whatever you want to help you. A few resources I’ve found helpful are:

If you still can’t solve the solution with help, then move onto step 5: sample solutions.

Otherwise let’s consider how optimal our solution is in step 4.

4. Can you improve the time and space complexity of your solution?

This bit is probably the hardest part for most people as it requires thinking outside of the box, so don’t worry if you don’t come up with any improvements: the fact you are thinking about how you could improve your solution is the main thing.

So, you’ve got a working solution to the problem, let’s look at potentially improving it.

For this, feel free to use external resources to help you. There is value in improving your solution on your own but some questions require in depth knowledge of particular data structures and algorithms to the point where you will hit a brick wall without knowing them.

If you haven’t already, work out the time and space complexity of your solution. This will give you an idea of whether it can be improved in either of those areas.

If you need help with this then here’s a good article for it: Time Complexity and Space Complexity — GeeksforGeeks

Now, how you improve it is so bespoke and particular to each person, question and solution that I can’t give you any one all best fit approach, but here’s a few general pointers:

  • If you think your time or space complexity could be improved, then focus on that
  • Are there any data structures or algorithms you could use for improved efficiency?
  • Try refactoring your code to be more concise

To finish this stage off, I would say if you’ve gone 15 minutes at this and can’t come up with anything, then move on. You’ve already thought about improvements which is a great mentality to have in these problems.

So, looking at my solution to two sum, I could easily improve the time complexity by removing the sorting from it somehow and coming up with some alternative solution.

5. Look at a Sample Solution and Video

Find a sample solution and compare it with your solution for similarities and improvements you could have made.

Analyse the time and space complexity of the sample solution to get an idea of what the expected optimal solution space is for this kind of problem. Compare this to the complexity of your solution.

I would highly recommend looking at a video solution as well to walk you through the thinking process to get the solution rather than just the end result. You want to develop these thinking patterns yourself for when you tackle your next problem.

I personally use NeetCode.io for this, but if you look most LeetCode solutions on YouTube there will be someone explaining the optimal solution of it.

Also taking notes helps but don’t stress it if you think it wouldn’t help you.

For solving two sum, I would watch this video and pay attention to any data structures and algorithms uses.

I would copy the code over to my IDE and submit it to LeetCode to confirm it works.

Now, onto the final step.

6. Review the Question at a Later Date

After a week or two of solving doing the question, you want to briefly go through it again.

This doesn’t have to be anything long, just take 10 minutes to look at the question, remember how to solve it optimally, what data structures and algorithms it involved and review the optimal solution for it.

This is the part most people trip up on.

You don’t have to do this, but trust me this is the part which stops most people from mastering these questions

That’s All!

That’s it. Six simple steps to master a LeetCode question.

While this will take more time than just solving the question and moving on, this process will give you more value out of the question and make you more fit for a coding interview.

Thanks for reading and happy coding!
If you enjoyed this article then feel free to clap it and check out some of my other articles :)

--

--

TheConnoisseur

Avid Computer Enjoyer, I write about topics I'm interested in, mainly programming.