Grokking Algorithms

Updated on December 1, 2022
Aditya Bhargava
Aditya Bhargava

Author of Grokking Algorithms & Engineering Manager at Etsy

How can you leverage the power of illustrations to solve coding problems in your daily struggles? Join Aditya Y. Bhargava, author of "Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People,” and Gabi O'Connor, senior software engineer II at Etsy, to find out how you can create analogies with abstractions. Learn how to embrace algorithms and how they can help you in your day-to-day job.

How can you leverage the power of illustrations to solve coding problems in your dayly struggles? Join Aditya Y. Bhargava, author of "Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People,” and Gabi O'Connor, senior software engineer II at Etsy, to find out how you can create analogies with abstractions. Learn how to embrace algorithms and how they can help you in your day-to-day job.


Gabi O'Connor: Hi, everyone. I'm Gabi O'Connor, and I'm so excited to talk with Aditya Bhargava today about his book "Grokking Algorithms- An "Illustrated Guide for Programmers and Other Curious People." I've known Aditya as a coworker at Etsy, where we have both worked as software engineers, as a friend, and talented amateur ice cream maker. But only as I've been preparing for this interview have I also come to know him as an incredible author. I'm so impressed by his ability to articulate these stents in somewhat abstract concepts that can sometimes be the bane of an aspiring engineer's existence. I wish I'd had this book when I was just starting to learn how to code, and I'm so glad to have it even now as a resource, if I interview for a job in the future and honestly, just to geek out about algorithms and elegant problem-solving. So, without further ado, let's talk to Aditya Bhargava. Hi, Aditya.

Aditya Bhargava: Hi, Gabi.

Gabi O'Connor: So, why don't you tell everyone a bit about yourself and what this book is about?

Aditya Bhargava: Sure. It's an illustrated book on algorithms and it's meant to be easy to read.

Gabi O'Connor: Nice. So, how did this book come about?

Aditya Bhargava: Well, it started, I had a blog where I was illustrating concepts from Haskell. Haskell is a programming language I enjoy working with. And so I had a few blog posts on there, and Manning, the publisher, saw those and contacted me and invited me to submit a proposal for a book. I have always wanted to write a book on algorithms. I felt like that was kind of a gap because I think algorithms are very interesting and cool but they're often hard to read and are, you know, more academic books. And so I submitted the proposal and they liked it.

How is this book different from other algorithm books?

Gabi O'Connor: Nice. As you said, there are plenty of algorithm books. What makes this one different?

Aditya Bhargava: As I said, it's illustrated. It's easy to read. And a big part of that is, I have included a lot of very concrete examples in the book. I have an example in the book where I'm using a mango seller. You're trying to find a friend who's a mango seller. Another example, you're going with friends to a movie theater and you're trying to figure out how to sit together in the movie theater. So, you know, what I've done is, I've picked a very curated set of algorithms and focused on quality over quantity. So, you know, with the smaller set, I'm really able to focus on providing more examples for each topic.

Gabi O'Connor: I think from my reading it, the thing that really stood out to me was your use of illustrations, to illustrate these concepts. And like art and funny, realistic ways of depicting some of these concepts. So, I'm curious how you came to that approach and what you find effective about it.

Aditya Bhargava: Yes, it's a good question. It's an approach I have been using for myself for years. It's connected to this idea of a memory palace where, you know... I think medical students use this a lot where it's so hard to remember all this different terminology, but if you have a concrete thing, I mean you tie abstract concepts to it, then it's a lot easier to remember. I have been drawing for years, so it was very natural for me to tie abstract concepts to little drawings that I make. As I said, I started sharing that with these blog posts, and one of my blog posts, it's about Monads, which is a very difficult topic in Haskell. And it became very popular and it's still the most popular post. But when it became popular, that's when it felt like probably other people would benefit from this approach.

Recommended talk: Apps, Algorithms & Abstractions • Dylan Beattie • GOTO 2020

Algorithms’ choices

Gabi O'Connor: Yes. So, why did you choose, or how did you choose these algorithms, the ones that you included in the book?

Aditya Bhargava: That's a good question. As I said, you know, I picked a very curated set. So it doesn't cover as many topics as the "MIT Introduction to Algorithms Book," for example. I picked the algorithms that I myself find to be useful for my work, which I find myself reaching for frequently. The book is about problem-solving. So, I picked algorithms where if I have a problem at work, I could reach for one of these algorithms and solve it. And so again, I focused on a smaller number of topics, spending more time on each topic.

What’s interesting about algorithms?

Gabi O'Connor: Nice. Very cool. So, you mentioned a little bit earlier that you were interested in algorithms from an early age. What is interesting about algorithms in your mind?

Aditya Bhargava: It's really interesting to me how creative you can get with them. I code a lot in my free time, and it's always surprising to me that I end up using one of these ideas. So, for example, you know, I draw obviously, I also, I've been trying to get more into drawing, like comic strips. I really enjoy comic strips. Like I used to read "Calvin and Hobbes," you know, years ago. One tedious part about it is drawing the panels. It's so finicky, you spend so much time doing the measuring. So I wanted to write a little app for myself that would draw the panels for me, you know. Something easy that I could say like, you know, I want three rows of two or whatever.

While I was making that, I realized the easiest way to model that information would be as a binary tree. And even today, I haven't found a better way to model that. And it's just so surprising, right? Because they're so, such disconnected areas of focus. 

I think that's what I really enjoy. You know, it's not like learning a language where, you know, you might be using a different language 10 years from now or something. They're very basic ideas. They're very human ideas. I often talk about these ideas to people who know nothing about computers and they're also able to understand them because they're such fundamental ideas.

Key takeaways from the book

Gabi O'Connor: I also noted that when I was reading this book, my husband is a writer and not technical, or not an engineer. I often struggled with talking about my work and creating analogies to describe what the work is and kind of put it in layman's terms. I thought your book did a really great job of being able to illustrate these concepts. Like literally illustrate these concepts, but in ways that can make sense to anyone at any, you know, stage of their engineering experience or even non-engineering., I thought that was great. And I totally agree. So, what in your mind are some key takeaways from the book?

Aditya Bhargava: I think this book is really all about solving problems. I think my key takeaway is if you're stuck on a problem at work, try one of the techniques in this book. And I guess to even refine that more, my number one piece of advice would be if you're stuck on a problem, try using a different data structure. It's really amazing how many problems can be solved by just using a hash table or using a graph.

Recommended talk: Learn about the algorithms and tricks behind Shazam from an experienced developer • Roy van Rijn

Why is coding a creative endeavor?

Gabi O'Connor: Very cool. So, shifting gears a little bit, I think you touched on a little bit earlier, just the joy and the creativity in algorithms, and coding generally. What in particular do you find creative about coding?

Aditya Bhargava: It's cool to make those surprising connections. And I find that the more I know, the more I'm able to make those connections. I do feel like it's a very human field even though, it's computers. And just to give a quick example, one of my early chapters is about binary search, which is a very common technique used for speeding up search. And, of course, no one outside of computer science would know about this, but if you just try this simple trick and I'll just do it now, and maybe, you know, viewers can try themselves or they try it on a relative or whatever. I'd tell people, you know, I'm thinking of a number between 1 and 100, guess what it is. And they might take guess like 37 or something.

And then I'll say, "sorry, that's too low." Then immediately their next guess will be something like 65 or 70. You know, they're not hearing that's too low and then saying, "Oh, 38." Because all of us instinctively, as soon as we hear that's too low, we know that you need to cut the remaining search space in half. So maybe they say 70. And then I say, "Oh, that's too high." And so now they know the number has to be between 37 and 70. So, again, they're not gonna say 69, you know, I mean, this is sort of a longer example. But just to say it is just so human to do this, and it's so cool to be able to keep making those connections every day.

Gabi O'Connor: I totally agree. So, how do you find joy in coding at work?

Aditya Bhargava: I'm excited to work in areas that I don't know a lot about. I find that it forces me to learn new things, and a lot of the joy I get, again, is like seeing those connections, seeing how different parts exist and work together. If I know a lot about the back end at a company, I find it interesting to work on the front end, maybe work on the big data stack or try to take one component and copy it to performance or something. I find that keeps things fresh and also like keeps surprising me with new cool things about coding.

What did you learn about teaching?

Gabi O'Connor: Cool. So, you talked about how this is your first book, and that you transitioned from writing blog posts, to writing this book. We've talked about this before, but I have a teaching, so I'm a career changer. I was a teacher before I was an engineer, and I used to also teach programming to high school students, as a volunteer program. And I was curious about what you learned about teaching during the process of writing this book.

Aditya Bhargava: That's a good question. I should also mention I'm a career changer too. So, we're both career changers and we've talked about that a little before. It's nice to have that different background. I feel like, I bring something new to my job and I'm sure you feel the same way. One thing I've learned about teaching is, I find it very useful for me when teachers don't skip any steps. They show things and they show every step of something. I had an algorithms teacher, who did that, who would literally take an algorithm and walk through what it's doing to an array for every element in the array.

I found that to be very useful. That means I want to make sure that like if I'm skipping a step, that means I'm leaving space for a reader to have a question that's not answered by the book. So I try to avoid that. But mostly I think the repetition really helps because it helps me read something, think, oh, I think I understand it, but I wanna check and then see it again, and then feel like, "Okay, I do understand it." So, I do a lot of that in this book, you know, for example, I have this whole section on Recursion, where literally I'm going through a snippet of code line by line, and on each line, I'm saying, "Here's what this stack looks like now here's when the stack looks like now." I find that that's a lot more helpful to me than just saying, you know when you call the function again, you're adding a call to the cost side.

Gabi O'Connor: Nice. And we talked a bit earlier about, you know, using the use of illustrations in this book. How do you feel that these illustrations can support the methodology that you were just describing?

Aditya Bhargava: I think it's a different check on your understanding, right? Because maybe you've read the text, but seeing the image helps you confirm whether you've understood it or not. So, very often in the book, I'll put something in text and then I'll also illustrate it, and maybe I'll illustrate it two or three different ways even. So, I think it plays a good supporting role for the text. There are parts of the book where I really rely on the illustrations primarily to explain something because there are, you know, there are things where it's either easier to understand or it's more satisfying.

 I have this whole section on divide and conquer. And divide and conquer means you have a problem, you break it down into smaller and smaller pieces and solve those pieces, and then eventually you solve the whole problem. You know, and that I can break that in text, but it's a lot more satisfying to me as a reader to have a rectangle, for example, and cut it up into smaller rectangles and show all of these smaller rectangles being finished and then kind of feel like the whole thing is done. You know, there's just something, sort of deeply appealing about that kinda explanation to me.

Recommended talk: A Practical-ish Introduction to Data Science • Mark West • GOTO 2020

Gabi O'Connor: There are other algorithm books that have graphs and diagrams and different kinds of visual aids, but yours is the first book I've read where some of these ideas are illustrated with cute animals and people and jokes about Seinfeld and a lot of different ways of communicating these ideas. And I'm curious if you have any thoughts about why that is. Why maybe there isn't more art in some technical books?

Aditya Bhargava: I think the most likely answer is people sometimes don't feel confident about their drawing skills. I do think that's changing because truly, you're not drawing like a realistic person or something. It's very easy to just draw circles and lines and explain something in that way. I think it's maybe a lack of confidence in their drawing ability, but I would really encourage people to draw more.

Creating analogies with abstract ideas: tips & tricks

Gabi O'Connor: Do you have any suggestions for people who want to get better at or think about creating analogies for these abstract ideas, like any tips that you have for people trying to get better with this?

Aditya Bhargava: That's a good question. I do have a few tips. First I'd say the analogies, and the examples are the core of the writing. Just expect to spend a lot of time on them. Truly, I mean, these chapters, the number of times I went back and forth with my editor where we'd like come up with an analogy, I'd even do some of the illustrations, and then we'd say, "No, this doesn't really feel, you know, this doesn't work that well." So, just be prepared to think through several different analogies.

Then the other thing I'd say is to talk to multiple people. Explain your idea to both technical and non-technical people and see what they think. As I said, I have a lot of "Seinfeld" episodes in the book. I have a lot of different Easter eggs. I wanted to write a book that would feel interesting to read. So it's not just about conveying information. Every chapter I wanted to feel like, "Okay, I want to read this chapter. This feels like interesting material to me." So, that's something to look for if you are talking to my wife about ideas all the time, and if I can tell she's just bored about talking about this, then I don't want to put that idea in the book. I don't want to put a boring chapter in the book.

Gabi O'Connor: I kind of have a tip too, I think for people who are trying to improve these creative skills because it's been something I've been thinking about a lot recently myself. I think that sometimes it can be hard to create if you're afraid of messing up. And I think that a lot of us who kind of are high achievers are a bit of perfectionists. So, I took a workshop recently about drawing in perfect portraits, and I think one tip was just like, use paper and don't be afraid to use up the paper, the paper's there, it wants to be used. And I was like, "That's true, just use it." And doesn't have to be perfect. I also took at a tech conference and,  before the Pandemic. I went to an "Improv for Introverts workshop." The whole idea was kind of letting loose of trying to be perfect or trying to be, you know, just living in the moment and not being so anxious about things going perfectly. I felt that was really helpful too. So, maybe there are things like that that people could look into in their areas, or just don't be afraid to try new things and to fail at them.

Aditya Bhargava: I think that's a great point. I guess to add to that, I have been getting more into digital art. I've sort of just done it traditionally in the past, but I got an iPad. I started using Procreate. And so I started watching videos on YouTube of artists that I admire, drawing, painting something appropriate. It's just amazing how many times they start over on something, like... And it really helped me feel like, "Okay, they did this kind of stunning piece of art. They didn't just sit down and draw every line perfectly." Like often they'll do a whole sketch and they'll even start coloring and then they'll say, "I don't like this." And because it's digital, you can like reduce the opacity or something and like use it as a reference for the new thing you're drawing, or sometimes they like, just erase it completely. So, yes, don't be afraid to start at work.

Recommended talk: How to Read Complex Code? • Felienne Hermans • GOTO 2021

What you wish you had known when you started writing the book

Gabi O'Connor: You can't prove if you don't if you're not afraid to fail on occasion and learn. That's how you learn. So, was there anything that you know now that you wish you'd known when you started writing this book?

Aditya Bhargava: I wish I had known how long it was going to take. I think that I would've still done it. I'm really glad the book exists. I get emails from people saying how much they like it. Which is always lovely. But I think when I started at Manning, they had said, at most these books take a year and a half. It'll probably be done in a year or... And it took more than three years for the whole thing to be done. Just part of that was we did keep rewriting so much. I mean, the dynamic programming chapter took five months because you would keep thinking, it's done. And then feeling like, but it's not explaining it well enough. I mean, dynamic programming I find very hard to explain well. 

Gabi O'Connor: Yes, how did the length of working on this project for so long compare to working on long projects at work in your day job?

Aditya Bhargava: Well, this is definitely the longest project I've worked on. They feel similar in that you don't want to think about the whole project at once because it can feel daunting. And so it's good to break it up into smaller segments. Of course, with the book I did, like, okay, I'm working on this chapter and I'm not going to think about the other chapters right now. At work, I've worked on some projects that I have taken on for more than a year. We've just done milestones, and it does feel... I mean, even though this book felt like, "Oh my gosh, this book is never gonna end, this project at work never gonna finish." But having those milestones really helps me see that I'm making progress.

Gabi O'Connor: Yes, totally. So, I think we may have come to the end of the questions that I had about this book, but I wanted to see if you had anything else that you wanted to share with people watching this, or people who are interested in reading your book.

Aditya Bhargava: I would just leave you with algorithms are fun. Like they can be fun. So, I hope you find some joy in this book or in algorithms in general.

Gabi O'Connor: I definitely finished the book and was like, okay, I need to look into these things more of the things, of the concepts or further reading that I didn't know of. I'm also inspired to start creating more creatively and illustrating. Thank you for your time, Aditya. It was great speaking with you.

Aditya Bhargava: Thank you, Gabi.

Gabi O'Connor: Bye.