As you get more experienced at LeetCode practice, you can envision a hierarchy of problem-solving skills, where each level helps with a different aspect of problem-solving.
At the base of the hierarchy is the most concrete skill: Learning a model solution and using what you learn to solve the specific problem for which you wrote the solution. Studying a model solution this way is not just memorization, but it is a very direct way to learn a skill. First, you learn the steps to solve Problem A, and then you use those steps to solve Problem A. With enough repetitions, it provides a good foundation for the “Problem A” skill.
One level up in the hierarchy is a slightly more challenging and abstract practice: Solving a problem using the skills you learned from a similar problem. This describes the main problem-solving loop for LeetCode practice. Start learning a topic, solve a practice problem, learn more about the topic, solve more problems, and so on until you get good at solving problems in that topic tag (at least through the Medium difficulty level). A standardized problem-solving process helps get the most out of this practice loop.
Topic-wise practice can get you a long way into LeetCode practice, since LeetCode consists of problems that are grouped into standard topic categories. But for harder problems, it isn’t enough to know how to solve a general problem type. Hard LeetCode problems are hard because they require more than a cookbook approach. They require creativity. But that doesn’t mean there’s no way to get better at solving them. A canonical book on mathematical problem-solving, How to Solve It by George PĆ³lya, provides a set of principles and strategies to try. For example: use symmetry; consider special cases; solve a simpler problem first. This is more general advice than “find the middle of a sorted collection and determine if the target is in the left or right half.” But because it’s more general, it can apply to more complex problems.
Since How to Solve It comes from the tradition of mathematical problem-solving, it’s a good fit for LeetCode problems, which are often described in mathematical terms. But as an ultimate level of the skill hierarchy, we can examine problem-solving in even more general terms applicable to all areas of science and engineering. As an example, Ozan Varol’s book Think Like a Rocket Scientist uses examples from the Mars rover program and other challenging real-world projects. Some of his advice: reason from first principles; conduct thought experiments; learn from your failures.
It may seem like overkill to learn techniques that got robots to Mars and use them to solve toy software problems. But although well-defined LeetCode problems may be easier than messy real-world problems, that doesn’t mean they are easy. And LeetCode problems can work better for practice than problems that aren’t easily repeatable. So don’t worry that the technique you’re using to figure out a LeetCode problem might be too powerful for that purpose. If you test it out on LeetCode, you may find it coming to mind more readily when you need it for a bigger project.
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.