How to study data structures and algorithms to rock your interview

data structures and algorithms

Many people would have you think that preparing for programming interviews is all about practice problems. The internet abounds with lists of practice interview questions and answers, but rarely delves into the the details of what exactly you need to know.

But practice questions can only get you so far. If you don’t know how to implement a tree, how will you know how to traverse it in level order? If you don’t know about hash tables, how will you store mappings of strings to their count in a document with O(1) access?

The fact of the matter is, if you don’t know the basics, you can do practice problems until the cows come home and not make any real progress.

You need to dedicate time to studying data structures and algorithms.

But what should you study? How much should you study? How should you study? How do you know if you’ve studied enough? Let’s answer these questions.

What should you study?

This is a simple question that completely lacks a simple answer. It is also the thing that I see most people do wrong.

On forums, I often come across people asking questions about how to solve interview questions related to machine learning. While it’s true that you could solve FizzBuzz using Tensorflow like Joel Grus does, you’ll notice that he didn’t actually get the job. Overcomplicating things isn’t necessarily useful to you.

“But,” you might say, “what if they ask me machine learning questions?” Yes, it’s possible, but what are the chances? It’s much more likely that you would be asked a question about traversing binary trees or sorting a linked list.

The key to studying is to maximize the time you have by studying the most high-value topics. That means you shouldn’t study everything. After a certain point, your time would be better spent elsewhere, so focus on core data structures and algorithms.

Study hash tables and linked lists. Review sorting and searching, trees and graphs, arrays and strings. Know how to implement and use these things in your language of choice.

If you’re unsure what exact topics you should focus on, look at the different categories of questions we have on our practice questions page. You can also look at the table of contents of Cracking the Coding Interview for free in the Amazon preview. And unless you have a very compelling reason, DON’T go beyond these lists of topics.

How much should you study?

While this is a fundamental question, there’s no one-size-fits-all answer. Everyone comes into their coding interview prep with completely different backgrounds and experience. Regardless, everyone is expected to answer the same questions and have the same core of knowledge, so how do we all get to the same place?

New computer science grads have it the easiest. You’ve been steeped in data structures and algorithms for the past 4 years, so you may not really need to put in much effort here at all. As you go through study topics, you can just refresh any knowledge you may be iffy on.

The farther out from school you are, however, the more work you are likely to need. Currently, most companies will still ask algorithmic interview questions even for more senior engineers, so it will definitely be necessary to review basic data structures and algorithms so that you’re not caught in the interview with your pants down.

Finally for bootcamp grads and other people without a traditional CS background, you may need to dedicate a lot of time. Whether or not you’ve learned this stuff before, you’re expected to know it, and that may take significant effort, so make sure you leave enough time.

How should you study?

One of the best ways to study a data structure or algorithm is to implement it. While reading about separate chaining hash tables is one thing, you’ll really get a much deeper understanding of how they work if you have to learn it well enough to write the code.

You don’t need to code up everything. There gets to be a point where it no longer benefits you, but for each data structure and algorithm, consider coding up any tricky bits. Write a deleteNode() method for a binary search tree. Write a distance() method that returns the shortest distance between two nodes in a graph. If you don’t have time to code up everything, focus on the tricky parts.

This also has an added benefit. Many common questions are simple variants on the core set of algorithms and data structures that you need to learn. By knowing how to code them up, it’ll be easy to modify that existing code in your interview to fit the question. You can also code them up with pen and paper to practice for whiteboard coding.

Have you studied enough?

How do you know when you actually understand a concept? You don’t want to waste time studying something that you already know well, but at the same time, you don’t want to miss important material.

Let me introduce the Teddy Bear Technique. This is a very simple way to test your knowledge and know whether you need to devote time to additional study. All you have to do is explain the concept you are trying to learn to a teddy bear or other inanimate object (or a person if you can find a willing victim).

The idea here is that the act of speaking aloud and teaching a topic will highlight areas where you are prepared on a topic versus those where you need some work. Explaining the topic, particularly focusing on explaining it in a way that someone without knowledge of it already could understand, forces you to know the topic very well.

There are a couple keys to making this technique work for you:

  1. Be honest with yourself. Since this is a solo exercise, there’s not going to be anyone to tell you if they didn’t understand something. You need to be honest with yourself whether or not you adequately explained a given concept or whether you glossed over some of the important details.
  2. Explain like your teddy bear knows nothing about the topic (trust me, it doesn’t). If you talk like it already knows a lot about the topic, it is easy to skip some relevant background information that you actually need to know.

After explaining a subject, you have to go back and evaluate. How did that feel? Was all the information there in your head and readily accessible or did you struggle to remember how something worked? If necessary, go back and review the topic or subtopics and repeat this process until you know the subject down cold.

Recommended reading

Everyone has their own preferences when it comes to the best algorithms books and prep materials, but here are a few that I recommend:

  • Cracking the Coding Interview
    If you are just looking for some review, this is a good place to go. The actual explanations are kept to a minimum, but you can get a very good sense for what things you actually need to know for your interviews.
  • Elements of Programming Interviews
    Similar to CTCI, but with a teaching style focused heavily on sample problems.
  • Algorithms 
    For people who don’t know this stuff already, this book goes into a lot more detail. There’s also a Coursera course to go along with the book, which I recommend if you have the time.

In the end, studying is important. By knowing common data structures and algorithms down cold, it will give you a big leg up when it comes to interviewing. There are only so many combinations of techniques, so chances are, you’ll see a problem that is similar to something you’ve done before. Wouldn’t that be nice?

Get Free Stuff!

Sign up now and I'll send you weekly emails with more content like this.

We won't send you spam. Unsubscribe at any time. Powered by ConvertKit
  • Piyush Chaudhary

    Hi,
    Awesome post. Could you please also name a few Data Structures which everybody should implement in their language of choice ?

    • Definitely hash tables and trees are really helpful, as well as their related methods, like inserting and deleting. Linked lists are important, but pretty irrelevant if you’re using Python, so it depends on what language you’re using. I also would recommend implementing a sorting algorithm or two, like mergesort or quicksort, and both DFS and BFS in a tree or graph. That’s definitely the bare minimum, but hopefully gives you an idea where to start.

  • Ankita

    Hi, Thanks for the post.
    Could you please tell in graph Data structure, are all the algo of finding shortest path important from interview point of view? Or implementing DFS,BFS and topological sort will be enough?

    • Its definitely worthwhile to know how you might find a shortest path if needed, but I wouldn’t spend much time on that. Your focus should be more on BFS/DFS/topo sort and the applications thereof.

  • Antriksh Verma

    You understand the pain of finding relevant material in big Google library , big Thanks for sharing !!

  • harshit bajpai

    I have about a month and half. I already know many things about coding and I am from a CS background and have implemented them at some point. Can I complete these data structures and algos in this much time?

    • Absolutely! It’s all a matter of using your time wisely. Focus on the things that you’re struggling the most with and make sure that you really actively study, rather than passively just reading through things.