Leanpub Header

Skip to main content

Topics

Algorithms and Data Structures

From what an algorithm is and how its cost is counted to binary search, recursion, trees, graphs, heaps and the coding interview, algorithms and data structures as told through the free samples of the Leanpub books about them.

Created from the free samples of 7 Leanpub books and the descriptions and tables of contents of 10 more, with every source cited.

An algorithm is a finite sequence of precisely defined steps that solves a problem or carries out a computation, and a data structure is a way of arranging data so that the steps can be carried out cheaply. A set of lecture notes on this shelf compares an algorithm to a recipe, with ingredients as inputs, an ordered set of instructions and a cake as the output, and requires five properties of it: that it finishes, that each step is unambiguous, that its inputs and outputs are defined, and that every instruction is simple enough to be carried out, in principle, with pencil and paper.[2] A beginner's book makes the companion distinction between an algorithm and a program: the algorithm is an abstract description of what is to be done and the program is an implementation of it, so that a program can be run and an algorithm cannot.[5]

The subject is the oldest part of computer science and the part most often examined. The lecture notes give five reasons for studying it, correctness, efficiency, a toolbox for new problems, the fact that nearly every other field of computing is built on it, and the technical interview, and call the last the most important single prerequisite for passing one at a large technology company.[2] The interview shapes this shelf. Most of its books are written for candidates, several are collections of solved problems, and the general textbooks that dominate the subject elsewhere are few here.

This article covers where to begin, the history, what efficiency means and how it is analysed, searching and binary search, recursion, the principal data structures, and interview preparation. Its sources are the free samples of the Leanpub books listed at the end, with the descriptions and tables of contents of the books whose samples were not available or did not touch the subject, and every claim that rests on a source is cited to it.

Where to start

A reader who has never programmed should begin a step earlier than algorithms proper. Learn Programming with FlowRun (2025) teaches variables, branching, loops and arrays through flowcharts that run in a browser, on the ground that the eye follows arrows more easily than text. Its preface lists the ways in which an algorithm can be set down, in words, in speech, as a formula, as a table or as a flowchart, and gives one small procedure, which classifies a temperature as hot, chilly or cold, in two of them, conceding that the difference shows only when a program grows longer or repeats itself;[5] and Logic-First Coder covers the logic and mathematics underneath programming, from Boolean algebra and sets to the notation for an algorithm's cost.[13] Python Programming: Data Structures and the Standard Library is about the collections one language provides, lists, dictionaries, tuples and sets, and when to use each.[14]

With some Python, the natural first book is TLDR: Data Structures and Algorithms, which is free, asks only for a basic understanding of the language and explains each idea through a story before coding it.[1] Data Structures and Algorithm Design using Python (2025) follows the order of a university course and teaches Python along the way.[2] Super Study Guide: Algorithms & Data Structures (2024) is a short illustrated guide for readers refreshing what they once knew,[10] and C++ Algorithmic Mastery: 1000 Challenges from Beginner to Legendary takes a learner of C++ from first programs upward through eight volumes of small solved problems.[4]

Candidates preparing for interviews have books by language. The Problem Solver's Guide To Coding (2024) assumes basic C++ and data structures at about the level of a second-year undergraduate;[3] Mobile Coding Interviews Using Kotlin/Swift is for iOS and Android developers who work in Swift or Kotlin;[12] and three adjacent question-and-answer references, The Modern C++ and STL Interview Compendium, The C# and .NET Interview Compendium – Second Edition and Python Under Pressure, include chapters on data structures and algorithms within a wider treatment of one language.[7][8][9] A working JavaScript developer who wants structures beyond the array and the hash map can go to Advanced Data Structures Handbook: The JavaScript Edition.[11]

History

Algorithms are far older than computers. The word comes from the name of a ninth-century Persian mathematician whose treatise on arithmetic with Hindu numerals was known in Latin by its opening words, and procedures for calculation go back to antiquity. The idea was made exact in the 1930s, when several definitions of what can be computed turned out to be equivalent, and the first decades of electronic computing produced the classic repertoire: sorting methods, hash tables, balanced trees, shortest-path algorithms. The analysis of algorithms, the counting of their steps as a function of the size of the input, became a discipline of its own in the 1960s and 1970s.

The oldest algorithm in these books is also the one they use to show why the subject matters. Around 300 BC, the lecture notes relate, a Greek mathematician described a method for finding the greatest common divisor of two numbers that is still in use: the divisor of two numbers is also the divisor of the smaller and of the remainder left when the larger is divided by it, and the rule is applied until the remainder is nothing. For 54 and 24 two divisions are enough to reach the answer, six. The notes first write the obvious program, which tries every candidate, then a tidier version that is slower, and draw the lesson that cleaner code is not always faster code and that what was needed was a fundamentally better algorithm.[2] The newest layer of history on the shelf is the interview problem site, from which one author's solutions are taken unchanged.[3]

Efficiency and its analysis

Two correct algorithms for the same problem may differ enormously in cost. Knowing a language's syntax, the lecture notes hold, does not make a good programmer, because a program built on an inefficient algorithm will be slow however cleanly it is written, and they make the point with a copy shop. Three customers arrive together at one machine with jobs of a hundred, five and twenty pages. Served in order of arrival they wait an average of more than an hour; served shortest job first they wait an average of ten minutes. Nothing changed but the order, and the same principle, the notes add, is used by an operating system to schedule its work. A third example concerns how a problem is represented before it is solved. To decide whether an essay has been copied, or which pages resemble a query, each document is turned into a list of numbers, the counts of the words of an agreed vocabulary, and documents can then be compared by arithmetic.[2]

To compare algorithms without reference to a particular computer, analysis uses an idealised machine with a single processor, on which instructions run one after another and every basic operation, including access to any location in memory, takes one unit of time. Running time is then not a number of seconds but a count of basic operations as a function of the size of the input, where size means the number of items for a sort, the number of nodes or edges for a graph and, for the divisor problem, the number of bits needed to write the numbers down. Because the count can depend on the content of the input as well as its size, three cases are distinguished, and a search through a list shows them: if the target is the first element one comparison suffices, if it is last or absent every element is compared, and on average about half are.[2] Logic-First Coder ends on the notation in which such counts are stated,[13] and Super Study Guide: Algorithms & Data Structures opens with the kinds of algorithm and the mathematics they rest on.[10]

The search just described, which checks each element in turn, is linear search. TLDR: Data Structures and Algorithms introduces its faster relative with a game. A younger brother is thinking of a number between one and a thousand; guessing from the start, the reader is still wrong at the 374th attempt. The method is not at fault, the book observes, since with no information every number is equally likely; it is the rules of the game that allow only one number to be eliminated at a time. When the brother agrees to say whether each guess is too high or too low, the reader guesses the middle of the range, discards half, and repeats.[1]

That is binary search, and it requires that the collection be sorted. The book states it in three steps, check the middle element, discard the half that cannot contain the target, repeat on what remains, and settles the detail that causes confusion, which element is the middle one when the count is even. Its implementation keeps two indices marking the ends of the range still in play and stops either when the middle element is the target or when the indices cross, which means the target is absent. It then proves what intuition suggests: every wrong guess removes at least half of what is left, so after a number of guesses the remainder is at most the original count divided by that power of two, and the number of guesses needed grows only with the logarithm of the size. The chapter closes with three recurring forms in which the technique appears in interview problems, the classic search, the search for a boundary and the search over possible answers.[1]

The second and third forms extend the idea. A boundary search looks not for a value but for the point at which a condition begins to hold. Asked for the first occurrence of a number that may be repeated, the ordinary comparison no longer narrows the range, and the test is altered so that the search closes on the leftmost match. A search on answers applies the method to a range that is not in the input at all. The book's example is the integer square root, computed without any built-in function: the answer lies between nothing and the number itself, it is the largest integer whose square does not exceed the number, and that is a boundary which repeated halving will find.[1]

Recursion

A recursive function solves a problem by calling itself on a smaller instance of it. TLDR: Data Structures and Algorithms explains the idea with a lazy manager who does a small share of a project and delegates the rest to a subordinate, who does the same. Its first attempt at a function that counts down from a number prints the number and hands the rest to the same function with the number reduced by one, and it runs on past zero for ever; tracing the calls step by step shows why. The book adds that tracing is only a first aid, since following every call reduces to a mechanical process an idea that is conceptually richer. What is missing is a base case, the point at which the delegation stops, and the chapter goes on to a procedure for constructing recursive solutions, the stack of calls that a running recursion builds up, and two ways of working out its cost.[1] The greatest-common-divisor method has the same shape, a rule applied to ever smaller numbers and a base case when the remainder is zero.[2]

Recursion underlies several of the general strategies for designing algorithms that the lecture notes list in their preface: divide and conquer, which splits a problem and combines the answers; backtracking, which explores a large space of possibilities and retreats from dead ends; the greedy technique, which takes the best choice available at each step; and dynamic programming, which breaks an optimisation problem into overlapping subproblems and reuses their solutions.[2] Mobile Coding Interviews Using Kotlin/Swift arranges its material by such patterns, among them two pointers, the sliding window and dynamic programming, on the argument that recognising a recurring pattern serves better than memorising hundreds of problems.[12]

Data structures

The choice of structure follows from the question to be asked of the data. The lecture notes model the world's flights as a graph, with airports as nodes and flights as edges carrying a cost, a duration and a distance. The route with the fewest stops is then found by a breadth-first search and the cheapest or fastest by a shortest-path algorithm, in milliseconds, where trying every combination would never finish. Their curriculum introduces sets, stacks and queues alongside backtracking, the heap for problems of priority, and binary search trees with a self-balancing variant for keeping data in order.[2] Super Study Guide: Algorithms & Data Structures covers the same ground in four parts, foundations, linear structures and hash tables, graphs and trees, and sorting and search.[10]

Advanced Data Structures Handbook: The JavaScript Edition begins where those leave off, with the limits of arrays, hash maps and linked lists, and implements the trees used by file systems and databases, prefix trees and heaps, before turning to probabilistic structures that answer questions about very large sets approximately and in little memory, and to strategies for deciding what a cache should discard.[11] An adjacent book shows such structures at work. Build a SQL Database Engine in C++ builds a database engine in C++ from an empty directory, on the stated method that nothing is asserted and everything is demonstrated: where another book would say that a tree index is generally logarithmic, this one promises the arithmetic of its branching, the count of pages read and a benchmark over a million keys. Its thesis is that a database engine is neither magic nor a monolith but a stack of layers that can each be understood; every unit includes a real bug, reproduced, diagnosed and closed with a test, and the reader is meant to attempt each challenge before reading the solution.[6]

Three more adjacent books touch the subject from particular directions. Data Munging With Perl [2ed] gives a chapter to designing the data structures of a program that transforms data,[15] Assembly Arithmetic Algorithms-DOS builds routines for converting between strings and integers in assembly language,[16] and How does computer science work? explains in self-contained chapters how compilers, databases and other systems built on these ideas work.[17]

Interview preparation

The interview books teach by solved problem. The Problem Solver's Guide To Coding presents more than seventy challenges from a well-known practice site, grouped by topic from arrays to dynamic programming, each with an approach, code in C++ and an analysis of its cost. Its author is candid about the conventions of the genre: the solutions are the ones actually submitted, they keep the original constraints of each problem and so do not handle invalid input or corner cases, and they prefer readable code to the fastest, since most readers are beginners. Some problems end with similar ones for the reader to try alone, practice being what fixes an idea, and the material was refined by being shared week by week with communities of developers.[3] C++ Algorithmic Mastery: 1000 Challenges from Beginner to Legendary works by volume. Its first book alone lists a hundred and twenty problems, beginning with printing a line of text and adding two numbers, and each problem carries a hint, a tested solution, its complexity and an explanation.[4]

The language references approach the interview from the other side of the table. The preface of The Modern C++ and STL Interview Compendium says that an interview in its language is rarely satisfied by syntax, since one conversation can run from the cost of moving a value to what happens when a growing array reallocates its storage, and offers more than nine hundred questions that explain mechanisms and trade-offs.[7] The C# and .NET Interview Compendium – Second Edition and Python Under Pressure do the same for their languages, each with chapters on data structures and algorithms among others on the language itself.[8][9]

Questions about Algorithms and Data Structures

Which algorithms book should I start with?

With some Python, the natural first book is TLDR: Data Structures and Algorithms, which is free, asks only for a basic understanding of the language and explains each idea through a story before coding it.[1] A reader who has never programmed should begin a step earlier, with Learn Programming with FlowRun, which teaches variables, branching, loops and arrays through flowcharts that run in a browser.[5]

What is the difference between an algorithm and a program?

An algorithm is an abstract description of what is to be done and a program is an implementation of it, so a program can be run and an algorithm cannot.[5] Data Structures and Algorithm Design using Python compares an algorithm to a recipe, with ingredients as inputs, an ordered set of instructions and a cake as the output.[2]

How does binary search work?

Binary search works on a sorted collection in three steps: check the middle element, discard the half that cannot contain the target, and repeat on what remains.[1] Every wrong guess removes at least half of what is left, so the number of guesses needed grows only with the logarithm of the size.[1]

How is the running time of an algorithm measured?

Not in seconds but as a count of basic operations, stated as a function of the size of the input, on an idealised machine where every basic operation takes one unit of time.[2] Because the count can depend on the content of the input as well as its size, three cases are distinguished: a search through a list takes one comparison if the target is the first element, a comparison of every element if it is last or absent, and about half of them on average.[2]

What is a base case in recursion?

A recursive function solves a problem by calling itself on a smaller instance of it, and the base case is the point at which that delegation stops.[1] TLDR: Data Structures and Algorithms shows what happens without one: a function that counts down by printing a number and calling itself with the number reduced by one runs on past zero for ever.[1]

How should I prepare for a coding interview?

Mobile Coding Interviews Using Kotlin/Swift arranges its material by patterns such as two pointers, the sliding window and dynamic programming, on the argument that recognising a recurring pattern serves better than memorising hundreds of problems.[12] The Problem Solver's Guide To Coding teaches by solved problem, with more than seventy challenges that each carry an approach, code in C++ and an analysis of its cost, and some end with similar problems for the reader to try alone.[3]

Sources

Every source is a Leanpub book. The note after each says which part of it was read; where no sample was available, only the public description and table of contents were used.

  1. 1.
    TLDR: Data Structures and Algorithms, John G: From Knowing Python to Cracking Leetcode Patterns Free sample: two chapters, on binary search and on recursion. 2026. Free sample: PDF
  2. 2.
    Data Structures and Algorithm Design using Python, Shashank Free sample: the preface and the first three lectures, on what algorithms are, the greatest common divisor, and the analysis of running time. 2025. Free sample: PDF
  3. 3.
    The Problem Solver's Guide To Coding, Nhut Nguyen: Master essential algorithms, basic data structures, and common programming techniques by solving coding challenges in C++ Free sample: front matter only, with the preface on how the challenges were chosen and solved. 2024. Free sample: PDF
  4. 4.
    C++ Algorithmic Mastery: 1000 Challenges from Beginner to Legendary, Hatem M.: Solved and Tested — A Complete 8-Volume Journey from Beginner to Advanced Algorithms Free sample: the introduction to the first volume, its list of problems and the easiest of them. 2026. Free sample: PDF
  5. 5.
    Learn Programming with FlowRun, Sakib Hadžiavdić: A visual introduction into the world of programming Free sample: the preface, on algorithms and programs and the ways of expressing an algorithm, and the first exercises. 2025. Free sample: PDF · EPUB
  6. 6.
    Build a SQL Database Engine in C++, Hatem M.: Through Challenges --- Storage, Query Processing, Joins, Indexes, Vectorized Execution, and Crash Recovery (Complete, Units 1-14) Free sample: the statement of what the book is, its plan, and the opening of the first unit. 2026. Free sample: PDF
  7. 7.
    The Modern C++ and STL Interview Compendium, Yohan J. Rodríguez: Interview Questions and Answers for Modern C++ Developers Free sample: the preface and the first chapter, on the history of the language standards; cited for the preface. 2026. Free sample: PDF
  8. 8.
    The C# and .NET Interview Compendium – Second Edition, Yohan J. Rodríguez: Interview Questions and Answers for C# and .NET Developers The free sample holds questions on the language only: from the description and table of contents. 2026. Free sample: PDF
  9. 9.
    Python Under Pressure, Ahmed: A Complete Interview Guide — written from the interviewer's side of the table. The free sample is one chapter on another subject: from the description and table of contents. 2026. Free sample: PDF
  10. 10.
    Super Study Guide: Algorithms & Data Structures, Afshine Amidi, Shervine Amidi No sample: from a short description. 2024.
  11. 11.
    Advanced Data Structures Handbook: The JavaScript Edition, Hela Ben Khalfallah: Simplifying Complexity for Real-World Applications No sample: from the description and table of contents.
  12. 12.
    Mobile Coding Interviews Using Kotlin/Swift, Mahmoud Ramadan: Pattern-Based Problem Solving in Swift & Kotlin No sample: from the description and table of contents. 2026.
  13. 13.
    Logic-First Coder, Alex R. Insight: Mastering Algorithmic Thinking, Problem Solving, and Clean Code Architecture No sample: from the description and table of contents. 2026.
  14. 14.
    Python Programming: Data Structures and the Standard Library, Edgar Milvus No sample: from the description and table of contents. 2026.
  15. 15.
    Data Munging With Perl [2ed], Dave Cross: Techniques for data recognition, parsing, transformation and filtering No sample: from the description and table of contents of a second edition in progress. 2026.
  16. 16.
    Assembly Arithmetic Algorithms-DOS, Chastity White Rose: 16-bit DOS Edition No sample: from the description and table of contents. 2026.
  17. 17.
    How does computer science work?, Hela Ben Khalfallah: Answering 28 Key Questions Every Software Engineer Should Know No sample: from the description and table of contents.

Further reading

Other books in the library on neighbouring ground, not cited above.

  • C++ Under Pressure: A Complete Interview Guide from 1,000+ Interviews, Ahmed: Written by the interviewer , not the candidate Interview preparation in C++ written from the interviewer's side; it says it is not an algorithms book and keeps them to reference appendices. 2026. Free sample: PDF
  • Java Coding Problems - Second Edition, Packt Publishing Ltd: Become an expert Java programmer by solving over 250 brand-new, modern, real-world problems More than two hundred problems and solutions on the newer features of Java, with attention to performance and complexity. 2024.

This is an article created from the free samples of 7 Leanpub books and the descriptions and tables of contents of 10 more. We're building these articles to provide you, the Leanpub reader, with a good jumping-off point to discover more Leanpub books that are interesting and relevant to you. We never use material which is paid-only when building these articles: we only use material which is already free on Leanpub. However, Leanpub authors can opt out from inclusion in articles from their book settings page.

Updated November 2026.

In the Leanpub store: 234 books · 2 courses in the Algorithm category · also filed under Data Structures

All topics