Gaining fluency in language syntax is the first step in getting better at expressing the solution to an interview problem in code. But even in a coding interview, you won’t be expected to implement everything from first principles. It’s true that if the interview question asks how to implement a hash table, then you’ll have to understand the internal details of hash tables. But if you just need a data structure you can use to insert and retrieve in average O(1)
time, you can use the one provided by your language’s standard library.
For coding interview preparation, the difference between built-in language features and a language’s standard library isn’t too important. The best approach to learning either one is to solve problems and read editorials until you encounter a new concept, and then focus on learning that concept. At some point, you’ll encounter features that are part of your language’s standard library, like the C++ Standard Library or the Java Class Library.
Just as with language syntax, the goal for learning a standard library is achieving fluency. Although you can do well in a coding interview even if you forget exactly how to initialize and use a hash table, being fluent in a language library will speed up the coding process and give you more time to think about the hard parts of a problem. It also shows that you have experience using a language. Interviewers often let candidates choose which language they want to use, so they expect candidates to pick one they’re fluent in. If a candidate isn’t fluent in their self-chosen language, the interviewer might wonder how much programming they actually do on a daily basis.
As you solve interview problems and study solutions, you’ll learn which library features are useful for solving those types of problems. Using code from your model solutions, look for patterns in how features are used. You will only need a small subset of the library, so focus on learning this subset well. Within the useful subset, find a consistent way to use library features, which will make them easier to remember. If a library gives you multiple ways to do the same thing, see if you can solve problems using just one of them.
Just as you can write model solutions to document your best approach to solving a problem, you can also write model code for a library feature. Rather than a full solution, this model code is just a block of code with one purpose. For example, some problems can be solved using a priority queue, which many languages offer as a library feature. A priority queue (also called a heap) provides an efficient way to retrieve the maximum (or minimum) item in a collection. To use a priority queue in a solution, you need to know how to initialize it as a max heap or min heap, insert a new item, peek at the min/max item, remove the min/max item, and check the size of the collection. Through practice, you’ll probably find that these are all you need from a priority queue, even if your language offers more properties and methods. So your model code only needs to show how to use these facilities.
Once you write model code for a library feature, you can use it for reference, as an alternative to looking at the library documentation. As you repeatedly do this, you’ll internalize how the feature works, until you reach the point where you don’t have to refer to any model code for that feature.
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.