Foreword¶
A few years ago, I shared solutions to the “Coding Interviews” problem series on LeetCode. Readers offered a great deal of encouragement and support. One question came up more than any other in our conversations: “How do I get started with algorithms?” In time, that question began to fascinate me.
Diving straight into practice problems seems to be the most popular approach. It's simple, direct, and effective. But working through problems can feel like playing Minesweeper. Confident independent learners manage to clear the mines one at a time. Those with less of a foundation may hit one mine after another and gradually lose heart. Reading a textbook from cover to cover is another common approach. For someone looking for a first job, though, a thesis, job applications, written tests, and interviews already take up most of their energy. Getting through a hefty textbook can feel like a daunting task.
If that sounds familiar, I'm glad this book found you. It's my answer to that question. Even if it isn't the optimal solution, it is at least a step toward one. The book alone won't land you a job offer, but it will guide you through a map of data structures and algorithms. You'll learn to recognize the shapes and sizes of the “mines,” see where they tend to lie, and practice ways to clear them. With those skills, I believe you'll find it easier to tackle problems, read the literature, and gradually build a connected understanding of the subject.
I agree wholeheartedly with Professor Feynman's remark: “Knowledge isn't free. You have to pay attention.” In that sense, this book isn't entirely free. To do justice to the attention you're investing in it, I will give writing it all the attention I can.
I know the limits of my own knowledge. Although the book has been revised and refined, I am sure many errors remain. I welcome corrections and criticism from teachers and students alike.
Hello, algorithms!
Computers have transformed the world. Their speed and programmability make them ideal for running algorithms and processing data. The lifelike graphics in video games, the decisions made by self-driving cars, AlphaGo's remarkable moves, and the ease of conversing with ChatGPT all show what algorithms can do on a computer.
Algorithms and data structures were already part of life long before computers. Early algorithms were relatively simple: a method for counting or a sequence of steps for making a tool. As civilizations advanced, algorithms grew more intricate. From the ingenuity of traditional crafts to industrial products that free people from labor, and even the scientific laws that describe the universe, subtle algorithmic ideas underlie much of what we find both ordinary and astonishing.
Data structures are just as pervasive. A social network or a subway system can be modeled as a graph. Organizations ranging from a nation to a family have features of trees. Layers of winter clothing behave like a stack: the first layer you put on is the last one you can take off. A tube of shuttlecocks works like a queue, with an item inserted at one end and removed at the other. A dictionary resembles a hash table in the way it lets you look up an entry quickly.
Through clear animations, diagrams, and runnable examples, this book aims to help you understand the core ideas of algorithms and data structures and implement them in code. From there, we'll explore how algorithms come to life in a complex world and discover their beauty. I hope the book serves you well!