A model solution shouldn’t just explain how to solve a problem. It should also help you remember the solution, so you can use it in the future for similar problems. Here are a few more ideas for writing solutions you’ll remember.
Use multiple levels of abstraction
Think of a model solution like a pyramid, with the submitted code at the base and a tag like “binary search” or “hash table” at the apex. At the base, all the details are worked out, and all ambiguity has been clarified. That’s what is required to write instructions that can be understood by a computer. At the tip, there are just a few words that name the concept that the problem covers. Between these two extremes, you can explain the problem at different levels. For example, to elaborate on the “binary search” tag, you could say, “Run a binary search on the sorted input array by checking the middle value, then checking either the left or right half depending on how the middle value compares to the target. Repeat until you either find the target or run out of input values to search.”
By writing a solution at multiple levels of abstraction, you can make the solution useful regardless of how much of it you remember. If you just need a small hint, the topic tag may be enough. If you can’t figure out how the topic applies to the problem, you can move lower in the pyramid to get a bit more detail. This is often how LeetCode hints are structured, so people can read just the first hint if that’s all they need, or multiple hints if they need more help.
Divide your solution into chunks
Another way to break up a model solution uses the concept of chunking from cognitive psychology. According to this theory, human working memory has a limited capacity, on the order of seven items. If the number of items you’re working with gets too large, some items are lost.
The reason we can solve complex problems despite this memory limitation is that each item can contain a collection of smaller items. For example, if you try to learn a solution by remembering individual lines, you’ll quickly run out of working memory when you try to reproduce it. If you instead chunk multiple lines into a block of code, that will considerably reduce the number of chunks in a solution. Once you learn a solution well, you could even retrieve the entire solution as one chunk.
As you write your model solution, think about how to divide it into chunks, to reduce the demands on your short-term memory.
Explain the solution in multiple ways
You may have had the experience of reading a solution that doesn’t clarify anything about the problem. Sometimes that’s because the solution is just poorly written. But it could also be that you’re just missing some essential context. When you write your own model solution, you can only use the background you currently have. A year later, you might come to it with a different background. Keep this in mind as you repeat problems and revise your model solutions. As you gain experience, you may be able to write a different solution to the same problem, even if you don’t change the algorithm. Both solutions may be well-written, and the earlier one may even be better for an audience with less experience. But the later one makes use of what you learned in the intervening months. When you gain enough experience, it may make sense to rewrite a solution from your new perspective.
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.