Succeeding at algorithmic coding interviews requires learning about algorithms and data structures. But it’s not enough just to know the theory of algorithms. You also have to implement them quickly in a real (non-pseudocode) programming language. If you’re writing code on a whiteboard or a plain text editor, your code has to be good enough to impress the interviewer, but you can take a few shortcuts with syntax. If you’re using an online judge like LeetCode that compiles and runs your code against test cases, the syntactical standards are higher.
Learning a programming language to solve LeetCode problems is both easier and harder than learning a programming language to solve real-world coding problems. It’s easier because you don’t have to learn everything about the language and its associated infrastructure. For a real-world problem, you might have to learn a front-end language, a back-end language, libraries and frameworks, a database, pseudo-languages like HTML and CSS, and other details that go into creating a working software solution. For a LeetCode problem, you only need to learn a small subset of a programming language and its libraries. And you don’t have to set up a programming environment or automated tests. You just type code into a box and click a button to run it. Everything else is handled for you.
On the other hand, many programmers find interviews to be more challenging than their daily work. Even if we ignore the challenge of learning algorithms and data structures (see the previous tips for more on that) there is still the challenge of coding on demand under both time pressure and the watchful eye of the interviewer. To do well in these conditions, you need to work on language fluency.
The way to improve your programming language fluency is to practice the syntax of a language the same way you practice implementing algorithms. If you’re working on a problem that asks you to solve a 2D maze and you have trouble representing the maze as an array, study how your language handles arrays. Isolate that component of the problem, separating it from the maze-solving algorithm, and practice until it becomes second nature. Make sure you can address the cell in a particular row and column, move North or West or Northwest in the maze, and check whether you are out of bounds. Don’t return to the maze algorithm until you’re clear on the array syntax.
The goal of practicing language syntax is to reach the point where you can express concepts in code as easily as you can write pseudocode, math equations, or diagrams. You never want to know the solution to the problem but be unable to write the correct keywords and tokens to implement it. Ideally, if you can think of the pseudocode for a solution, your fingers should be able to type the equivalent code without much intervention from your brain.
Besides language syntax, you also need to practice your language’s standard library. We’ll cover that in the next tip.
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.