When you’re working on a LeetCode problem, there is always a solution available, either the official solution or a solution from the discussion forums. So, as you’re working on your own solution, you always have the option of getting help. Your study strategy, therefore, needs to tell you how long to work on a problem before looking at a solution.
It’s easy to see why immediately looking at the solution is a bad idea. The only way to learn how to solve coding problems is by solving coding problems. Just reading about them doesn’t help. So we can rule out 0 minutes as the right amount of time to work on a problem.
Another approach is never looking at the solution until you have solved the problem on your own. If you can’t solve a problem, just move on to another one and return when you have more experience. This approach sounds reasonable. Every problem requires specific background knowledge, and until you have that background, you can’t make a proper attempt. So it’s better to save the problem for when you’re prepared for it.
The problem with this approach is the trial and error that it requires. Since you never look at the solution, you don’t know what skills you need to solve the problem, which means you don’t know when you’re ready to make another attempt. You could come back to the problem and find that you’re still missing a critical skill. If, rather than never looking at the solution, you plan to look at the solution only when you get stuck, you’re just moving the trial and error into the future. When you look at the solution, it will reveal what skills you need, but not which problems to use to practice them.
To get around these problems, you can use the techniques of problem lists and spaced repetition. Rather than approaching each problem as a test that tells you if you’re smart enough to solve it, look at it as a stepping stone to learn the next sub-skill on the way to mastering a topic.
Using this approach, you can minimize the time you spend on each problem. If you follow a problem list, you can solve problems in an efficient order, with each problem leading to the next one. If you get stuck on a problem after spending a reasonable amount of time (on the order of 1-2 hours at the most), read the solution to see what you’re missing. Then add the problem to your spaced repetition list and try it again in a day or two. Using this process, you can ensure that you’re always working on the right skills and solving problems at the right level.
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.