Learning a model solution is worthwhile because it gives you a process for solving similar problems. If you know how to analyze and solve one binary search problem, you can apply it to similar situations where you have sorted data and need to search for a target value. The more model solutions you know, the more likely you are to have a ready solution for any given LeetCode problem.
But while model solutions give you a detailed guide for solving similar problems, you won’t always get a problem that you can directly apply them to. So it’s best to see model solutions as an important component of a complete problem-solving process. Here’s an example of such a process for solving any LeetCode problem, even if it’s different from any problem you have learned previously.
Select a problem and open it.
This could be the daily challenge problem, a contest problem, or a model problem that you’re learning.
Start a timer
How long you take to solve a problem can tell you a lot about your progress, especially once you have some historical data about the time these problems typically take. A LeetCode contest or a coding interview has a built-in time limit. But even when you’re practicing on your own, you can compare your performance to previous attempts for the same problem, or to similar problems. The LeetCode problem page even has an integrated timer.
Analyze the problem statement
LeetCode problems are written in an academic style that many people find unintuitive. And sometimes they are just written poorly. The purpose of the analysis step is to convert the original problem into a format that makes more sense to you. Then you don’t have to keep going back to the original statement while you’re solving the problem. At a minimum, make notes about the input format, output format, and what your solution is expected to return.
Solve the problem on paper
Now that you understand what the problem is asking for, or you have some reasonable guesses, sketch an idea for solving the problem. This is the step where you might bring in a model solution that you have previously studied.
Write a pseudocode version of your solution
Pseudocode is a useful way to clarify an algorithm without getting bogged down in syntax or other implementation details. Convert your solution idea from the previous step into pseudocode. As you do this, you may find that you don’t understand certain parts of the solution well enough to write them as an algorithm. In that case, it’s good to work these out before you get to the coding step.
Translate your pseudocode into actual code
Now you have to work out any details that you skipped during the pseudocode step, including syntax and library details. Writing actual code is part of most coding interviews, so make a note of any areas where you find it difficult to translate an abstract algorithm into code. You can focus on these later during your study process.
Test and debug
LeetCode gives you a few test cases and lets you add your own. Unlike some platforms, the LeetCode judge even tells you what the correct answer is for any test case, which makes debugging easier.
Submit (and maybe debug some more)
LeetCode’s hidden test cases are trickier than the practice cases, so you may have more debugging to do in this step.
Stop the timer and record your elapsed time
Once you get an accepted solution, record how long it took you.
Read other solutions
Even if your solution was accepted, it’s probably not the best solution that anyone has ever submitted for this problem. Part of the learning process is seeing what other people have come up with. When you find good ideas, you can incorporate them into your model solutions.
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.