One way to use LeetCode is to solve as many problems as possible. The argument for that approach is that the more problems you solve, the more experience you get, which will make it more likely that you’ll be able to solve the problems you encounter in an interview. There is some merit to this argument. If you never practice solving binary tree problems, it’s unlikely that you’ll be successful if an interviewer asks you to reverse a binary tree. However, the opposite may not be true. If you practice 100 binary tree problems, you might still not solve the particular binary tree problem you encounter in an interview.
The purpose of model problems and model solutions is to improve the quality of your practice, independent of the number of problems you solve. You should add a problem to your list of model problems because you think it’s the best problem you know of to learn a particular skill. The model solution you create for that problem explains everything important about that problem, including parts you may not notice if you just solve it once and move on.
But writing a model solution doesn’t mean you’re done with a problem. It’s good experience to select a model problem, solve it on your own, study other people’s solutions, and write your model solution. You’ll know a lot more about that problem than you did when you started, and you’ll know better how to solve related problems. But even after doing this work, you’ll be far from an expert in that problem. If you doubt this, find a LeetCode Medium problem and follow the preceding steps to write a model solution for it. Then wait 30 days and try to solve it under timed interview conditions. My guess is that you’ll find it challenging, even though you’re re-solving a problem that you have studied in depth.
To effectively use model problems and solutions, you need another tool, spaced repetition. When you first study a LeetCode Medium problem, there are a lot of details to keep track of. You have to understand what the problem is asking, the algorithms and data structures you need to use to compute the result, and the implementation details for your chosen language. It can be difficult to keep everything straight, so you may not have the spare mental capacity to learn the more general lessons the problem offers. But if you repeat the problem a second time, some of it will be familiar. You won’t have to think as much about some parts of the problem-solving process. You’ll be able to focus on details that you may have missed the first time. With each repetition, some aspect of the problem will become easier, until you reach a level of mastery where you can solve the problem from start to finish with minimal effort.
That’s the repetition part. The spaced part is the time between repetitions. If you solve the same LeetCode Medium problem every day, it will get quite easy after a few days. But if you stop practicing it for 30 days and pick it up again, you might discover that it has somehow become difficult again. To avoid this problem, you need to schedule your repetitions effectively. In the next tip, we’ll look at how to design a spaced repetition plan that makes the most of each practice session.
This year, I’m publishing a series of tips for effective LeetCode practice. To read the tips in order, start with A Project for 2023.