Foreword¶
A few years ago, I shared a series of solutions to the “Coding Interviews” problems on LeetCode and received encouragement and support from many readers. In our conversations, one question came up more than any other: “How do I get started with algorithms?” Over time, I became deeply interested in this question.
Jumping into practice problems with little preparation seems to be the most popular approach: simple, direct, and effective. Yet solving problems is rather like playing Minesweeper. Strong independent learners can clear the mines one by one, while those with a weak foundation may run into one setback after another and gradually retreat in frustration. Reading a textbook from cover to cover is another common approach. For job seekers, however, theses, applications, written tests, and interview preparation already consume most of their energy, making a thick textbook a formidable challenge.
If you face similar difficulties, I am glad this book has found you. It is my answer to that question—perhaps not the optimal solution, but at least a constructive attempt. Although this book alone will not secure a job offer, it will guide you through the “knowledge map” of data structures and algorithms. It will show you the shapes, sizes, and locations of different “mines” and teach you ways to clear them. With these skills, I believe you can approach practice problems and technical reading more confidently and gradually build a complete body of knowledge.
I wholeheartedly agree with Professor Feynman's words: “Knowledge isn't free. You have to pay attention.” In this sense, the book is not entirely “free.” To honor the precious attention you invest in it, I will devote as much of my own attention as I can to writing it.
I am aware of the limits of my knowledge. Although the content has undergone considerable refinement, many errors surely remain. I sincerely welcome corrections and suggestions from teachers and fellow learners.
Hello, algorithms!
Computers have transformed the world. Their speed and remarkable programmability make them an ideal medium for executing algorithms and processing data. From the lifelike graphics of video games and the intelligent decisions of self-driving cars to AlphaGo's brilliant games and ChatGPT's natural interactions, these applications are all compelling expressions of algorithms running on computers.
In fact, algorithms and data structures existed throughout the world long before computers. Early algorithms were relatively simple, such as ancient counting methods and steps for making tools. As civilization advanced, algorithms became more refined and complex. From exquisite craftsmanship and industrial products that boosted productivity to the scientific laws governing the universe, ingenious algorithmic ideas lie behind almost everything, whether ordinary or astonishing.
Data structures are equally widespread. Systems ranging from social networks to subway routes can be modeled as “graphs.” Social organizations, from nations to families, often have the characteristics of “trees.” Winter clothing behaves like a “stack”: what goes on first comes off last. A tube of shuttlecocks is like a “queue,” with items inserted at one end and removed at the other. A dictionary resembles a “hash table,” allowing quick lookup of a desired entry.
Through clear animated illustrations and runnable code examples, this book aims to help readers understand the core concepts of algorithms and data structures and implement them through programming. Building on that foundation, it seeks to reveal how algorithms take shape in our complex world and to show their beauty. I hope you find it helpful!