A model solution is a place to record everything you think is important about a problem and its solution. If you can learn and remember your model solution, you’ll be able to reproduce the solution to the model problem, and you’ll have a head start on solving similar problems.
When you write your own model solutions rather than relying on pre-written solutions, you can customize them so they make sense to you, which means they’ll be easier for you to remember. Just writing and explaining a solution helps you remember it. But if you find that you can only solve a problem after short intervals and not after longer intervals, that’s a sign that your solution isn’t memorable enough. It’s not enough just to write a good editorial that clearly explains every step of the solution. You also have to write it in a way that helps you recall the key parts of the solution when you need them. Here are some ways to accomplish that.
Explain the solution in multiple ways: You can explain a solution using code, pseudocode, code comments, narrative text, static diagrams, animated diagrams, and other creative methods. Each of these gives you a different way to remember the solution. For example, if you’re trying to reproduce a solution and you’re having trouble with the code, remembering the pseudocode may give you what you need.
Use diagrams: To get a solution accepted on LeetCode, the only thing you have to submit is code. If your code is accepted, you know that your solution is correct and meets the minimum performance requirements. Once you have the code, it’s easy enough to write a few sentences in your model solution explaining how it works. Drawing a diagram requires a different way of thinking, so a visual representation of the solution, along with narrative text, will help you understand and remember the solution better.
Pick good identifier names: Competitive programming has a reputation for encouraging short variable names, which contestants use to write their solutions as fast as possible. Coding interviews also have a time limit. But for learning and remembering a solution, it’s best to pick meaningful variable names, while balancing the need to write the solution under time pressure. If you’re practicing a problem and you have trouble remembering a section of the solution, renaming a variable or function could help make it more memorable.
Write hints: LeetCode supplies official hints for many problems, so people can try solving a problem with a hint rather than by jumping directly to the solution. Hints also simulate the experience of a coding interview, where the interviewer is there to help you along if you get stuck. In your model solution, write a few hints, where each hint supplies more information. You can use these when you’re still learning the solution.
Record which parts of a solution you don’t remember: A practice journal can help you discover which parts of your solution need to be more memorable. If you remember some parts of a solution but not others, make a note of that. Then use your notes to improve the solution.
Make your solution explain itself: In real-world software, log files can help programmers track down the source of a bug. LeetCode solutions can also include logging (usually print statements). These logs can help clarify the steps that a program uses to solve a problem, and the state (values of variables) at each step. When you find test data that produces good log output, copy it into your model solution.
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.