LeetCode works because algorithmic coding interviews focus on a limited set of topics. You don’t have to learn all of computer science, just certain algorithms and data structures, which LeetCode attempts to capture in its problem library. This list of tips recommend studying the LeetCode topics by selecting a few model problems for each topic, and learning how to solve those problems. Given enough problems for a topic, you’ll learn how that topic works in practice.
One way to prove that you know an algorithm well enough is if, when you start a new problem, you can completely focus your mind on the details of that problem without thinking about the algorithm. Since the algorithm is unchanging, you don’t want to waste any brain power rethinking it. The important thing is what a particular problem is asking.
The practical reason that you have to focus on the problem rather than the algorithm is that you can only think about what you can fit in your working memory, and working memory has a limited capacity. If you have stored the algorithm in long-term memory, you can use your full working memory for the current problem.
Another reason is confidence. If you know an algorithm well, you won’t be tempted to rush into implementation. You’ll know that when you decide to implement the solution, the details of the algorithm will come easily to mind. So you can spend as much time as you need thinking about the problem first.
As an example, consider the LeetCode problem Determine if a Cell Is Reachable at a Given Time, which was used as a Daily LeetCoding Challenge problem. The problem asks: Given an infinite grid and a start and destination cell, can you travel from the start to the destination in a specific amount of time?
LeetCode problems that require moving around grids usually involve Breadth-First or Depth-First Search solutions. So it’s tempting to start implementing one of those and see what happens. Without giving away too much, let’s just say that there are reasons these algorithms don’t apply in this case. But to reach that conclusion efficiently, you have to think first and code later.
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.