One of the ways to visualize code is through a Code Map. A Code Map helps developers see their codebase structure in a graphical format. This is useful for grasping how different code parts connect, and it makes navigating the code simpler.
Visualize Code in ASCII: N-Queens and More
If you’re like me, you need to see a tool work in order to figure out how it works before being able to trust that it will indeed be good enough for the job at hand. In other words, like me, you’re more of a hands-on, visual learner. For people like us, seeing is essential for understanding—magic just doesn’t cut it. And in coding interview situations, the confidence that comes from knowing exactly what works how is the key to success.
Slide decks#
For this reason, we at Educative have made a concerted effort to use all the tools at our disposal to demystify even the most complex and subtle algorithms used in our coding interview prep material. For example, while revamping the course Coderust: Hacking the Coding Interview, a major improvement came in the form of slide decks that illustrated the working of an algorithm for a manageable input size. This was welcomed by our learners, as it made it easier to make the connections between the written description of the algorithm, the code itself, and the changing states of the data structures during code execution.
For example, the solution to the Sliding Window Median problem requires tracking two halves of a window as it slides across an array of numbers. To better support the written explanation of the solution, we added a slide deck to illustrate how we use two heaps, labeled "small list" and "large list," to accomplish this task.
However, such slides are limited in their utility in that they are a static illustration of an algorithm for a fixed set of inputs.
ASCII printing#
An important question that static slide decks cannot really answer is: "What do the data structures look like if I change the input like so?" Additionally, we may want to see the contents of the data structures used after making a small but critical change in the code in order to visually debug the code. To quickly address these concerns, we turned to ASCII printing to give our users a way to visually trace the execution of a solution.
Let's look at how a relatively simple bit of printing code can help us understand some subtle interactions as well as help us verify the output for specific test cases.
Use case 1: LRU cache#
The code below tests an implementation of a cache (the hidden class LRU Cache) with a Least Recently Used (LRU) eviction policy. It supports two methods: Set() and Get(). The Set() command adds the supplied key-value pair to our cache. If that key already exists, its value is updated. The Get() command fetches from the cache the value associated with the supplied key. If the key is not present in the cache, it returns .
In the test case setup section, we set up a sequence of Get() and Set() commands to test our cache. Looking at the code below, but before running it, can you guess the result of the function call Get(52)?
Now that you've run the code, if you were surprised that Get(52) returns
Use case 2: N-Queens problem#
Here’s another example where printing helps to quickly check, just by looking at the output, whether the result for a specific test case is correct: the famous N-Queens problem.
N-Queens problem statement: Given a chessboard of size , determine how many ways queens can be placed on the board, such that no two queens attack each other. A queen can move horizontally, vertically, and diagonally on a chessboard. One queen can be attacked by another queen if both share the same row, column, or diagonal.
In the code sample below, the implementation of the solution, Solve N Queens(), has not been shown:
After running this code, though we can check online whether the results are correct (since this is such a well-studied problem), we're left with the feeling that we don't really know what is going on. For example, why are there
Tip: Un-comment the command (highlighted) that turns on the printing switch for this code sample, and then run it again.
Now that we can see the valid placements for each test case, we start to understand the problem better—which is the first step in understanding the solution.
In the next post in this two-part series, we'll delve into the code to print a binary tree, and use it to help us solve a thorny problem.
If this short blog post has whetted your appetite for problem-solving, you can dive right in to the challenging set of problems we have curated to help you prepare for your coding interview:
Get your hands dirty dissecting subtle algorithms!
I created Grokking the Coding Interview because I watched too many talented engineers fail interviews they should have passed. At Microsoft and Meta, I saw firsthand what separated the candidates who succeeded from the ones who didn't. It wasn't how many LeetCode problems they'd solved. It was whether they could look at an unfamiliar problem and know how to approach it the right way. That's what this course teaches. Rather than throwing hundreds of disconnected problems at you, we organize the entire coding interview around 28 fundamental patterns. Each pattern is a reusable strategy. Once you understand two pointers, for example, you can apply them to dozens of problems you've never seen before. The course walks you through each pattern step by step, starting with the intuition behind it, then building through increasingly complex applications. As with every course on Educative, you will practice in a hands-on way with 500+ challenges, 17 mock interviews, and detailed explanations for every solution. The course is available in Python, Java, JavaScript, Go, C++, and C#, so you can prep in the language you'll actually use in your interview. Whether you're preparing for your first FAANG loop or brushing up after a few years away from interviewing, this course will give you a repeatable framework for cracking the coding interview.
Frequently Asked Questions
Is there a way to visualize code?
Is there a way to visualize code?
What is the N queens problem?
What is the N queens problem?