As we saw in Tip 31: Learn Problems and Sub-Problems, you sometimes need to learn a skill that’s smaller than a full LeetCode problem. Tip 31 explains how you can use the standard LeetCode interface to learn concepts at the sub-problem level. But there’s another way to accomplish that goal by using a REPL.
The read–eval–print loop (REPL), a tool from the early days of Lisp programming, lets you execute pieces of a program interactively. Rather than writing an entire program, compiling it, running it, and evaluating the result, you can execute a single line or block of code and see what it does immediately. This speeds up the process of learning language syntax, library functions, and the code blocks that fit together to make an algorithm implementation.
For LeetCode study, the REPL I recommend is the Jupyter Notebook. These notebooks let you write and run blocks of code surrounded by Markdown text, images, equations, and everything else you need to describe a model solution. Visual Studio Code has a convenient Jupyter extension that I use it to write my model solutions and practice journal.
A standard problem editorial, like the ones published on LeetCode, focuses on correct solutions for a particular problem. An editorial may show several ways to solve a problem, maybe with different time and space complexities. But the goal of an editorial is to describe a complete solution, like one that you could submit to LeetCode. Notebooks are a good way to document complete solutions. But they are also good for more exploratory learning.
Using a Jupyter Notebook, you can break a solution into parts and experiment with each one. In addition to “correct” code from the solution, you can show why other variations don’t work. Even runtime errors are shown in the notebook, so you can see what happens when you do something the compiler doesn’t like.
Code comments and naming conventions can help explain what is going on in a model solution. Notebooks take that a step further by letting you embed runnable blocks of code that show exactly what is happening in each part of the solution. And they let you experiment with the code in real time to help you understand exactly how it works.
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.