What is LeetCode, and why do I post solutions to it online?
A few months back, I entered the crazy world of LeetCode.
What is LeetCode
For those of you who don’t know, LeetCode is a site that posts thousands of coding problems at varying difficulties, and asks you to solve them, whilst providing various test cases that your solution must pass. Additionally (and in my opinion most valuably), LeetCode also has a big community of users who share and comment on solutions to said problems.
LeetCode’s most common user is someone seeking employment at one of the world’s biggest tech companies, all of whom utilise coding problems (AKA algorithmic coding) as a means of picking out the best candidates in the thousands of daily applicants they receive.
Now, the rationale here isn’t that you need to be able to come up with complicated algorithmic solutions in order to work at Google or Facebook, but rather that by learning such algorithms and applying them whilst interviewing, you show that you’re willing to go above and beyond to get the job. It’s a bit like asking 100 people interviewing to be a chef to count to 1,000,000 — you may never actually need to do it, but by trying (and potentially succeeding), you’ve differentiated yourself from the rest of the applicants.
Now that isn’t to say that you won’t ever use the kind of complex algorithms and data structures you find on LeetCode in the real world. In fact, there are some concepts, such as “Big O Notation”, that really can change your day-to-day work as an engineer. In addition, depending on the team you end up working in, graph traversal or LinkedList manipulation may be as common a task as writing unit testing. These companies aren’t saying you will use such methods during your time with them, but they want to know that if they need you to, you can.
Why would I want to share LeetCode solutions?
To some, the act of posting a solution to a problem on the open internet may seem a little odd. It’s a bit like posting the answers to a test; yes readers will be able to answer the questions, but if they’re asked an even slight variation of the original question, they’ll be stumped.
But in reality, these problems rarely have a single correct answer. In fact, the process is typically more about identifying methods and applying concepts than it is arriving at a single correct solution. Even if you do provide a perfect solution, and one that beats out every other submission in terms of timing, you still might find another solution in the comments that makes you say “Oh, that’s a much better way of doing it!”.
To give you a great example; this LeetCode problem asks you to find all pairs of numbers in an array that add to a given number. The most obvious solution to me was to loop through the array twice (one inside the other), and add all possible pairs together. This solution worked, was pretty quick for the example case of 4 numbers, and was easy to understand. But lo and behold, my submission was incredibly slow compared to others, unusably so with a bigger dataset. So I quickly jumped into the discussion and found a method that’s infinitely better; (Hash)Maps! Now not only do I have a better solution to submit, but the next time I’m writing similar code in my day-to-day work, I stop and think; “Would a Map be better here?”. None of this would be possible without sharing solutions.
Why I started posting the solutions online
So we’ve established what LeetCode is, as well as why you might share solutions for their problems. But why, you may be thinking, post them online?
1. Because the LeetCode discussion section is… meh
Although a fantastic resource for finding solution and guidance, at the end of the day the “Discuss” tab on each LeetCode problem is a fairly typical, short-form post and reply system. The vast majority of responses are short, most submissions are uncommented and underexplained, and generally speaking the whole thing is geared more towards people who are already familiar with the process, and are looking to say “Oh that approach is a little better” rather than “How on earth do I even start to solve this?”.
Now, that isn’t to say that every discussion is like this. Some users go out of their way to take you through their solution step-by-step, some post video walkthroughs, and some just comment their code extensively. There are plenty of great posters on LeetCode who do not fit the description above.
2. Because it helps me learn (and not cut corners)
When faced with a difficult problem on LeetCode, I will often (after staring at the wall for ~10 minutes) switch to the “Discuss” tab and start looking at solutions. If I’m lucky, I’ll soon have a working submission inspired by another poster, and can move onto the next problem.
But the issue here, is that taking this shortcut often leads to under-understanding a solution. Instead of coming up with it organically, and understanding the full process from start to finish, I only really understand how the solution solves the problem (it’s a bit like saying “I know 2 + 2 = 4, but I don’t know why”).
The fix I have come up with to this issue is to write up my solutions in a way that can guide others from start to finish. What this means is that I have to be able to fully understand the solution, in a way that would have allowed me to solve it from scratch, and to a depth at which I am capable of explaining it to the next person in the chain.
It is a strange situation where in order to understand something, I must be able to teach it to others, and I’ve found this really works for me.
3. Because I like writing
As a long-time amateur writer, and frequent online poster on a variety of formats, I enjoy the act of writing. Typically this has been via fiction, but I’ve found it interesting explaining technical solutions in written form, and as per my point above, have found that writing it out aids my own understanding at the same time.
When will I stop?
There are currently almost 2,000 problems on LeetCode. I’ll be surprised if I ever write up 10% of them, not least because beyond that point you’re often repeating the same approach in a slightly modified way.
In truth; I’ll probably stop writing up LeetCode solutions when it stops benefitting me to do so, though it sounds a little selfish to put it that way, doesn’t it?