In the geeky world of programming puzzles, programming interviews, and competitive programming (loosely defined), LeetCode is one of the major players. And on LeetCode, 2021 begins with another month of LeetCoding Challenges. In my 2020 wrap-up post last week, I described the process that I use to get the most out of these challenges. I plan to continue refining that process in the coming year.
Model Solutions
As I argued in last week’s process article, solving a new coding puzzle every day is useful, but it’s not the most efficient way to gain expertise. To learn these problems well, you have to repeatedly return to previously-solved problems until you’re intimately familiar with the algorithms, data structures, and techniques that come up regularly. The first step in this process of repetition is creating a model solution.
When you solve a LeetCode problem for the first time, the quality of your solution will vary a lot. If the problem is similar to one that you have already learned well, your solution might be close to optimal. If it requires an algorithm or technique that you have never learned, you might not be able to solve it at all. Or it could be somewhere in between.
If I decide to solve a problem a second time (because it meets my criteria for a good problem) and I didn’t find an optimal solution the first time around, I don’t just repeat my own solution. That would have the effect of reinforcing whatever shortcomings I included in the solution. Instead, I first do some research on how others have solved it. LeetCode sometimes has an official solution, but I find the LeetCode discussion forums to be even more useful, since they provide a range of ideas from multiple contributors. Using the advice I find in the discussions, I write a model solution consisting of both code and explanation. Although the code in my model solution often draws on a working solution from the discussions, I don’t just copy and paste it. I re-write the solution in my own style, make sure I understand what the code is doing, and submit my re-written version to ensure that it passes all the test cases. I also write an English description of the solution and, if necessary, create diagrams illustrating the solution details. As I practice the model solution over time, I update the code and the description to be more understandable and easier to learn.
Periodically this year, I plan to publish examples of these model solutions as blog posts, supported by code on GitHub.
Quora in 2021
In 2020, I decided to do my weekly writing in the form of Quora answers, which I linked here each week. I plan to do that again in 2021 on weeks that I don’t publish a model LeetCode solution. As always, I will also post a daily link in the Coder vs. Coder space on Quora.
The reason for using Quora in this way is the same as last year: Quora is still the place where a critical mass of people gather to discuss topics related to competitive programming and coding interviews. These topics also come up on Reddit, Hacker News, Codeforces, LeetCode, Stack Overflow (where they are immediately deleted), and other places. But for all its many faults (and they are numerous), Quora is a reasonably canonical online source for these topics, and there are always questions being asked there.
Good luck with your own projects in 2021!
(Image credit: JuliaC2006)