Maximize LeetCode with These Tips
“Without goals, and plans to reach them, you are like a ship that has set sail with no destination.”
- Fitzhugh Dodson
LeetCode is like a double-edged sword. It’s one of the best resources to help you prepare for technical interviews, yet using it ineffectively can lead to wasted time and feeling overwhelmed.
In one of my blog posts on Becoming a DS & Algorithms Pro, I listed LeetCode as an excellent resource to practice interview-like programming problems. In this post, I’ll expand further on how to effectively use LeetCode and develop habits that will prepare you for coding interviews.
Yalla, let’s begin.
Table of Contents
Review DS & Algorithms
I can’t stress this enough - it’s a must to make sure you’re familiar with data structures and algorithms. This will set you up for success while trying to solve any LeetCode problem. Without thinking twice, you’ll be able to think of relevant data structures or algorithms that can help you solve a unique problem. Like a toolkit, both data structures and algorithms are your tools for solving programming problems. And like a handyman choosing the right tool for the right job, you’ll know which data structure or algorithm is right for the problem you’re trying to solve.
Can you technically use LeetCode without knowing data structures and algorithms? Sure you can…..but you’ll eventually hit a wall and find it difficult to solve most problems.
For example, let’s say a LeetCode problem is asking you to sort an array in place with a time complexity of O(n log n). How would you solve this? One solution is using the quick sort algorithm with a median pivot. Without existing knowledge of algorithms, this would be a tough problem to solve.
If you don’t know data structures and algorithms, start studying them one at a time. While studying one, you can try solving LeetCode-specific problems for that DS/algorithm which will help you further cement your new knowledge. For more information on studying DS and Algo, check out my previous blog here.
Utilize Problem Filters & Lists
There‘s a wide array of programming problems available on LeetCode. So where to start? This will depend on you and where you are in your coding journey. If you’re new to coding or haven’t practiced in a while, start solving Easy problems. If you’re studying a Linked List data structure, then start solving Linked List programming problems to further cement your learning. You can also check out LeetCode’s curated lists of problems like LeetCode 75 Study Plan to Ace Interviews, Top Interview Questions, and more! For LeetCode’s curated lists, just be aware it’s a mix of random difficulty levels with varying problem types.
Time Yourself
While trying to solve LeetCode problems, avoid spending endless hours on them. Instead, set a timer for 30 minutes and try solving the problem. If you can’t solve it within 30 minutes then stop and review the solution to the problem in depth.
So why time yourself? Because in a live coding interview, you’ll be given 30-45 minutes to solve a programming problem. Developing this habit while practicing LeetCode problems will strengthen your time management skills and set you up for success during real interviews.
With that said, here’s how you can allot your 30 minutes while solving a LeetCode problem:
First 10 minutes: Fully understand what the problem is asking then develop a rough solution without code. Use comments to write out your proposed solution.
Second 10 minutes: If you’re stuck, spend more time figuring out a solution. Otherwise, start coding your solution.
Last 10 minutes: Fix any compile errors and run your code. If time permits and if possible, optimize your current solution.
Following this method isn’t a hard rule, but just guidance to help make the most of your 30 minutes. Some problems are more difficult, forcing you to spend more than 10 minutes to figure out a rough solution. And that’s okay! As long as you’re disciplining yourself to stick to the allotted time.
Talk to Yourself
You read right, get into the habit of talking to yourself. While solving problems yourself, there’s zero communication and the thought process is all in your head. This is contrary to a real interview, where you’re expected to communicate your thought process and approach to solving a problem.
Here are some general tips on talking to yourself through a problem:
Talk aloud while reading the problem
Talk through your analysis of the problem
Talk through your proposed solutions at a high level
If you’re not able to figure out a solution after a few minutes, talk about your blocker and what’s preventing you from moving forward
As you’re coding the solution, talk aloud to highlight the steps you’re taking
Others in your household will think you’re crazy, but this habit will setup you up for success in tech interviews.
Avoid Coding Right Away
As programmers, it’s easy to read a problem and start coding immediately. Whether you’re solving a problem or working on an app professionally, this is a harmful habit that is both counter-intuitive and can lead to wasted time. In fact, spending time planning will save you time on execution. Brian Tracy, an international speaker, author, and productivity expert states the following:
“Every minute spent in planning saves ten minutes in execution”
Make sure to take a step back and spend some time planning out a solution. If your initial solution has a quadratic runtime, is there a more efficient solution? Did you take into consideration the problem constraints? What about edge cases?
By spending time and putting thought into your solution, you’ll be amazed at how quickly you’re able to implement the solution - thus saving you time on execution.
Learn from your Mistakes
"The definition of insanity is doing the same thing over and over again, but expecting different results."
- Albert Einstein
As I mentioned earlier, if you’re not able to solve a problem within 30 minutes then review the solution in depth. Try to solve that same problem the week after. If you discover you’re struggling with a concept that’s causing reoccurring mistakes, review that concept over again. After re-reviewing that concept, try solving LeetCode problems specific to that concept for a couple of days. For example, if you’re constantly making mistakes with Heaps problems, re-review Heaps thoroughly then practice Heap LeetCode problems. Remember that practice, repetition, and consistency are key!
What if your solution is accepted by LeetCode? That’s awesome, but continue to find ways to improve upon your existing solution. If your solution has a terrible runtime or memory complexity, spend time reviewing how you’d improve your existing solution. Also, compare your accepted solution to other solutions that are shared in LeetCode. You’ll learn a lot from other people’s code on how to simplify your existing code. LeetCode has a great feature where you can filter community solutions by programming language - so take advantage!
Don’t Give Up!
"I have not failed. I've just found 10,000 ways that won't work."
- Thomas Edison
When timing yourself to solve a problem within 30 minutes, use every second to brainstorm and implement a solution. Never throw in the towel and just give up because the problem is too difficult. Use all 30 minutes. Review the problem and talk aloud through several high-level solutions. Identify where you’re struggling exactly. Recall that data structures and algorithms are tools available to help you solve a problem. Which tool is right for this problem? If you’re still stuck, view some problem hints if available.
The key to solving a problem is understanding the problem, then developing a high-level solution. Whether it takes you 2 minutes or 25 minutes. Once you arrive at a solution, implementing the solution should take the least amount of time. It’s happened to me a few times where I couldn’t think of a solution until the 20th minute and still ended up solving the problem! Don’t give up, believe in yourself, and use every second you’re given.
For any comments and questions on this post, follow up on Twitter.
Make sure to share this article. Appreciate you reading through my blog and until next time.