How to fall in love with Leetcode and master it
Whether you like it or not, Leetcode-style technical interviews are here to stay. Most of the top tech employers use these interviews to assess the candidate’s skills. This is what these companies have been doing for years and there is no evidence that any of them are planning to change this approach any time soon.
Many software developers hate this situation. After all, the algorithmic problems represented by Leetcode-style challenges are not something developers encounter every day in their work. Therefore, even a developer with years of experience won’t be able to complete such challenges quickly enough to pass an interview unless he or she deliberately practices such challenges.
Having to practice for Leetcode is what puts many people off from applying to work at their dream companies. But it doesn’t have to be this way.
I spoke to many developers who were good at solving Leetcode challenges. They all had certain things in common, which I have distilled for you in this article.
In today’s article, I will help you fall in love with the Leetcode practice and become a master at it based on these fundamental principles in 10 simple steps.
1. Drop the “grinding Leetcode” phrase from your vocabulary
This may not seem like an actional step, but it certainly is. This step is even more important than any tips on specific techniques.
One of the main reasons you find Leetcode hard is that you see it as a chore. You see it as an additional activity you have to “grind” through in addition to your day job and any other responsibilities.
And one of the reasons for seeing Leetcode practice as a chore is the words you use. Every time you refer to it as “grinding” or something along these lines, you condition yourself to see it as a difficult and unpleasant chore. The more you say it — the stronger the conditioning becomes.
Therefore, if you want to start liking Leetcode, the first step would be to stop using the words that are making you feel the opposite.
2. Reframe Leetcode as fun
Once you stop talking about Leetcode as if it’s a chore, the next step would be to reframe it in your head as a fun activity. This is not some kind of brain hack or self-hypnosis. Leetcode practice can genuinely be fun.
If you think about it, the process of solving a Leetcode challenge is not very different from solving a puzzle or playing a strategy video game. Such games are just as challenging, but they don't feel like chores. This is because you don't face any mental resistance to doing fun activities even when they are hard.
So, if you keep reminding yourself that the Leetcode challenges are no different from playing a video game, over time you will see them as fun. You will build this association in your brain.
This may not happen overnight, but you will eventually change your mindset if you keep trying.
3. Start doing Leetcode for entertainment
All developers I met who are good at Leetcode have one thing in common. They do these challenges with enthusiasm. To them, practicing Leetcode is just another form of entertainment. And why it shouldn’t be?
Yes, you may think that doing another cognitively challenging task once you already completed a day of cognitively challenging tasks at work is a poor form of entertainment. However, many of you have absolutely no problem playing video games or doing dance classes after work, both of which are just as challenging.
Many people even play simulation games for entertainment, such as Microsoft Flight Simulator. They literally do for entertainment what other people do for work.
So why not do Leetcode challenges for entertainment? If you develop the right attitude towards them, they will become just as fun as playing one of those complex video games or learning martial arts.
However, unlike these other forms of entertainment, the hours spent doing Leetcode won’t be purely recreational. They may (and, probably, will) substantially increase your chances of getting the software development job of your dreams.
4. Add Leetcode practice to your regular schedule
Once a recruiter from Google calls you, it will be too late to start practicing Leetcode for your technical interview which will happen in a few weeks. You will need, on average, to have solved at least 200 medium-difficulty problems to become even moderately proficient with Leetcode. However, the expectations at the Google interview will be high. Moderate proficiency may not be enough.
Each Leetcode challenge takes at least an hour, maybe more. So, you can imagine how difficult it would be to solve so many challenges in a short few weeks. You may not have the luxury of finding an extra 200 hours to spare.
To prevent this from happening, the best strategy is to carry on practicing regularly. And the best way to do it is to formally add your Leetcode practice to your daily (or, at least, weekly) schedule.
This serves two purposes:
You will always be ready to attend a technical interview
Once you’ve done it for a while, it will become a habit
Habits make it easy to stick to the schedule. You will no longer have to force yourself to do it. On the contrary, something will feel wrong if you happen to skip a scheduled session.
And now, since we got the mindset and habit-formation tips out of the way, let’s have a look at specific practice techniques.
5. Focus on medium-difficulty problems
Most of the technical interviews focus primarily on medium-level Leetcode problems. Occasionally, you will encounter a hard problem. However, medium-level problems are what you will encounter the most.
Therefore, while practicing, it makes sense to spend the bulk of your time working through medium-difficulty challenges. While FAANG companies are unlikely to give you the exact problems that you will see on Leetcode, the problems you will encounter will probably be similar to the ones you’ve already seen.
There is also another reason to stick with medium-level problems. For an average experienced developer, easy problems are too easy. Hard problems, on the other hand, might be too hard as they require a fairly advanced knowledge of algorithms.
If you focus on solving easy problems, you will get bored quickly. If you focus on hard problems, you may get overwhelmed. However, when you focus on the problems of moderate level of difficulty, you may be able to enter the state of flow and start enjoying the process even more.
6. Attempt to solve the problem to at least pass the base test cases
Every Leetcode problem has public and private test cases. The public test cases typically represent a handful of conditions to verify the logic. Private test cases, however, would contain various edge cases and verify the solution’s performance. For example, your solution may be correct but very poorly optimized, which will make it time out in certain scenarios.
You may find some challenges to be hard, especially if they are typically solved by an algorithm you aren’t familiar with. However, even if this is the case, you should at least attempt to solve the challenge to such an extent that it passes public test cases.
This will give you the habit of attempting a challenge even if the problem seems unfamiliar. If you develop this habit, there is far less chance of freezing during a real technical interview.
There is another benefit to it too. Once you start working on the solution, you may figure out an answer that didn’t look very obvious before. This may even happen in a challenge that you thought there was no way you could have solved.
7. If you are stuck, timebox the remaining effort
If you have been trying to solve a Leetcode challenge for an hour or so and haven’t made much progress, there is little point in continuing. Your time would be better spent understanding the algorithm that would solve this challenge.
Therefore, it is a good idea to give yourself a time limit once you stop making any meaningful progress. Once the time is up, you can look up the existing solution and apply it to your code. This will equip you to solve similar problems in the future.
8. Don’t just blindly apply other people’s solutions
While it is sometimes a good idea to apply other people’s solutions to Leetcode problems, the only way to derive value from it is to figure out what the solution is doing.
You can start by trying to figure it out yourself by going through the code. In fact, this is probably the best way. Humans remember better things that they figured out themselves over something that was explained to them by others.
However, there will be situations where the code will not make any sense. If this happens, your best bet is to look up the explanation on the web. Luckily, these explanations tend to be easy to find, as the public Leetcode challenges are well known.
One of the best explanations for most Leetcode challenges can be found on the Neetcode YouTube channel.
One more thing. If you had to use somebody else’s solution to solve a Leetcode challenge and then you figured out how it works, try to do the challenge again and apply this solution from scratch.
This way, you will remember what the principles behind the solution were. Otherwise, if you just copy the solution and read how it works, you will probably forget it very quickly.
9. Look up other solutions if yours had a mediocre rating
So, you solved a Leetcode challenge and your solution made all public and private test cases pass. Well done!
However, upon submitting it, you may find out that your solution is relatively poor compared to others. Perhaps it says that it’s faster than only 50% of other solutions. Perhaps it’s even less.
The best thing you can do in this case is look up the solutions that performed well. Yes, the algorithm you applied is good enough since all test cases passed. But there is definitely room for improvement.
This is especially important because “Could you do better?” is a very common question you will be asked during a technical interview. Getting familiar with more optimal algorithms will prepare you for this question.
10. Narrate your steps as you are solving a challenge
In a Leetcode-style interview, your interviewer doesn’t care that much about whether or not you will be able to solve a problem within the allotted time. While this is important, it’s much more important to show your thinking process to them. Therefore, you will be expected to talk to them about the steps you are taking and why you are taking them.
The best way to practice this is to narrate your steps while you are solving Leetcode challenges at home. You don’t have to do it out loud either. You can do it in your head.
This practice has some other benefits beyond preparing for an interview. Explaining the steps you are taking helps you structure your thoughts. It crowds out other thoughts out of your head and helps you focus.
Conclusion
As we can see, the best tips on how to master Leetcode have nothing to do with any specific practice techniques. Your mindset and your habits form the foundation. The specific techniques come later.
When it comes to the specific techniques, some of them may seem unorthodox. For example, you may have been surprised that I encourage people to look up other people’s solutions.
Some people may see it as a plagiarism. But it definitely isn’t. After all, you aren’t publishing someone else’s work and pretending that it’s your own. You are doing it to learn something that you don’t know from people who already know it and willingly published their solution to help people like you.
And no, this doesn’t make you “stupid” or “lazy” either. It’s not laziness to ask for help when you get stuck. It’s the most optimal use of your time.
And it’s not stupidity to not know something. If you don’t know something, you can learn it. This is precisely what we are doing by analyzing other people’s Leetcode solutions.