Leanpub Header

Skip to main content

Deriving Algorithms

Reading, Diagnosing, Building: Twenty-Seven Worked Derivations and 285 Problems That Teach You to Find an Algorithm Instead of Recalling One

Deriving Algorithms
This book is 100% completeLast updated on 2026-09-13

Most algorithm books are catalogues: here is a technique, here is its proof, here are exercises. This one withholds the name of every technique until after you have built it.

Minimum price

$17.00

$17.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

Most algorithm books are catalogues. They present a technique, prove it correct, and give you problems on which to apply it — and they leave out the only part that is hard, which is knowing which technique the problem in front of you wants.

This book is the missing part. Every technique in it arrives at the end of a derivation you can follow: a real problem, an honest attempt that fails, the reason it failed, and the observation that opens it. The name of the technique is withheld until you have already built it, because a name learned before the thing it names is a label rather than an understanding. Nothing here is a catalogue entry. There is no chapter you can skim for a recipe.

Part I builds seven instruments in the order they cost to use — reading a specification, brute force as a starting point, the first engine, invariants, structure that is already there, the state under constraints, and amortised cost — and then closes with a chapter that takes their names away and asks you to choose. Eight chapters. Twenty-seven full derivations. Two hundred and eighty-five problems, each tagged [1] to [5] by how well its ambiguity is hidden rather than by how hard its eventual algorithm is. Every problem sits in one of three layers: a full derivation you watch from the outside, a pivot with the rest left to you, or a statement and a single hint.

Every algorithm in the book has been implemented and checked against a brute-force reference, and every stated measurement has been measured rather than estimated. The ten algorithms of the full derivations appear in Python and C++ in an appendix, cross-checked on two thousand generated inputs each. The book assumes first-year programming — variables, loops, conditionals, functions, arrays — and nothing else.

This book was created through a process that combines careful human planning, content direction, and advanced AI technology, followed by thorough refinement and review to ensure a high-quality final work.

Bundle

Bundles that include this book

Author

About the Author

Hatem M.

Hatem M. is a programmer and technical author whose work focuses on modern C++, large language models, and AI systems.

His books combine first-principles explanations with complete implementations and reproducible experiments. They include C++ Algorithmic Mastery, an eight-volume series on algorithms and problem solving; Build an LLM Inference Engine in C++, which constructs a GPT-style inference engine from scratch; LLM Quantization: From the Bits Up, which develops the theory and practice of neural network quantization from the bit level upward; and C++ Autopsy, a forensic investigation of ten subtle C++ bugs that compiled successfully, ran correctly, and still produced the wrong answers.

Contents

Table of Contents

Front matter
  • What this book is
  • How to read it
  • What is in this volume
Part I — Reading, Diagnosing, Building 1. Before You Solve: Reading the Problem
  • 1.1 The Message
  • 1.2 Where Ambiguity Hides
  • 1.3 The Eight Extremes
  • 1.4 Problems
2. Brute Force as a Starting Point, Not a Failure
  • 2.1 The Five Lines
  • 2.2 Three Kinds of Waste
  • 2.3 The Diagnostic
  • 2.4 Problems
3. The First Engine
  • 3.1 Sixty Steps
  • 3.2 Four Questions
  • 3.3 Choosing the Smaller
  • 3.4 Assuming More
  • 3.5 Two Ways to Write It Down
  • 3.6 Problems
4. Invariants: What Stays True?
  • 4.1 The Flood Panel
  • 4.2 Reading One Back Out
  • 4.3 Designing the Sentence First
  • 4.4 Does It Stop?
  • 4.5 When It Is Already Wrong
  • 4.6 Problems
5. Looking for Structure That Is Already There
  • 5.1 The Round That Cannot Be Made
  • 5.2 Six Questions
  • 5.3 What Order Buys
  • 5.4 Monotonicity, Briefly
  • 5.5 When Nothing Fires
  • 5.6 Problems
6. Naming the State
  • 6.1 Sixty Days
  • 6.2 The Question, Precisely
  • 6.3 Three Ways It Goes Wrong
  • 6.4 Dropping a Dimension
  • 6.5 Problems
7. When to Stop: The Limits of Improvement
  • 7.1 The Heaviest and the Lightest
  • 7.2 Three Arguments
  • 7.3 Chasing What Is Not There
  • 7.4 When One Step Lies
  • 7.5 Problems
Closing Part I 8. Routing: Problems With No Chapter Attached
  • 8.1 What This Chapter Withholds
  • 8.2 The Seven Questions, In The Order They Cost
  • 8.3 Reading A Wrong Diagnosis
  • 8.4 Problems
Appendix P1 — Bridge to Python and C++
  • What changes in translation
  • The algorithms
  • How these were checked
Editorial notes
  • What was verified
  • What was changed
  • Chapter 8, and why it exists
  • Left for the author
Index

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub