Leanpub Header

Skip to main content

The Problem Before the Algorithm

Reading, Deriving, and Organising — A Complete Method for Solving Problems You Have Never Seen, from a Single Loop to a Whole System

The Problem Before the Algorithm
This book is 100% completeLast updated on 2026-11-16

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 — and then shows you how to organize a problem that is too long for any one technique.

Minimum price

$28.00

$28.00

You pay

Author earns

$

Also available for 2 book credits with a Reader Membership

PDF
EPUB
1,526
Pages
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, Reading, Diagnosing, Building, 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, naming the state, and the limits of improvement — and then removes their labels and asks you to choose.

Part II, Complexity, is hidden structure at scale: trading space for time, reduction, greedy choice and when it deceives, divide and conquer, stored answers, searching huge spaces, graphs, randomness, and the decision to stop looking for an efficient solution.

Part III, Length, is for problems that are hard because there is a lot of them rather than because any piece is hard: where the boundary is, where to cut, how to layer, what an interface promises, how to check part of a whole, how to assemble correct parts into a correct whole, and what to do when the problem changes while you are solving it.

Twenty-five chapters in three parts, with an interlude between the first two. Ninety full derivations. Nine hundred and seventy problems, each tagged [1] to [5] by how well its difficulty is hidden rather than by how hard its eventual algorithm is, and each sitting 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 problem left for you to finish — every Layer 2 and Layer 3 problem, and every problem of the Interlude — has a short answer at the back: the idea that decides it, the result, and a way to check your own, 887 answers in all, linked both ways. The book assumes first-year programming — variables, loops, conditionals, functions, arrays — and nothing else. The ten algorithms of Part I's full derivations are given in Python and C++ in an appendix.
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.

Bundles

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 book
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 Interlude — Routing: Problems With No Chapter Attached
  • R.1 What This Chapter Withholds
  • R.2 The Seven Questions, In The Order They Cost
  • R.3 Reading A Wrong Diagnosis
  • R.4 Problems
Part II — Complexity 8. Trading Time for Space, and Back
  • 8.1 Any Stretch of the Year
  • 8.2 Three Tables Worth Building
  • 8.3 When the Table Does Not Pay
  • 8.4 The Other Direction
  • 8.5 Problems
9. Reduction: Turn It Into a Problem You Know
  • 9.1 The Lost Alphabet
  • 9.2 The Anatomy of a Reduction
  • 9.3 A Dictionary of Masks
  • 9.4 When Reductions Go Wrong
  • 9.5 Problems
10. Choosing Without Looking Back: When It Works and When It Deceives
  • 10.1 As Many Events as Possible
  • 10.2 What a Rule Consists Of
  • 10.3 The Exchange Argument
  • 10.4 Making Counterexamples
  • 10.5 Four Signals That Greed Will Fail
  • 10.6 Problems
11. Divide and Conquer: Where the Work Really Is
  • 11.1 Two Rankings
  • 11.2 The Template, and the Bill It Writes
  • 11.3 The Combine Is the Problem
  • 11.4 Reading a Recurrence
  • 11.5 Where the Method Stops Working
  • 11.6 Problems
12. From Induction to Stored Answers
  • 12.1 The Hall, by Revenue
  • 12.2 Two Conditions, Tested Rather Than Defined
  • 12.3 Top-Down or Bottom-Up
  • 12.4 Recovering the Choice
  • 12.5 Shrinking the Table
  • 12.6 The Shapes States Take
  • 12.7 Problems
13. Searching Huge Spaces: Abandon Early, Choose Well
  • 13.1 Fourteen Sessions
  • 13.2 The Tree Behind Every Search
  • 13.3 Four Ways to Cut a Branch
  • 13.4 Which Decision Next
  • 13.5 When the Search Must Stop
  • 13.6 Problems
14. Thinking in Graphs: Finding the Network Inside the Problem
  • 14.1 The Running Order
  • 14.2 What Is a Node?
  • 14.3 Which Search, for Which Question
  • 14.4 Five Shapes of Model
  • 14.5 When a Graph Is the Wrong Model
  • 14.6 Problems
15. Randomness as a Tool: When a Coin Beats a Plan
  • 15.1 The Upload That Stalls the Server
  • 15.2 Two Kinds of Luck
  • 15.3 What “On Average” Promises
  • 15.4 When Chance Is Not Allowed
  • 15.5 Problems
16. When There Is No Efficient Solution: Hardness as an Engineering Decision
  • 16.1 The Recording Booth
  • 16.2 Five Signals of Hardness
  • 16.3 The Reduction, Run Backwards
  • 16.4 Four Ways Out
  • 16.5 What a Guarantee Says
  • 16.6 Problems
Part III — Length 17. Complex or Long: Telling the Difficulty Apart
  • 17.1 Two Tickets
  • 17.2 Three Kinds of Difficulty
  • 17.3 Long Outside, Complex Inside
  • 17.4 Problems
18. Drawing Boundaries: What Is Inside the Problem and What Is Outside
  • 18.1 The Duplicate Letters
  • 18.2 Five Lines
  • 18.3 Four Signs That the Boundary Is Moving
  • 18.4 Assumptions Nobody Wrote Down
  • 18.5 Problems
19. Decomposition: Where to Cut
  • 19.1 The Overnight Report
  • 19.2 Four Ways to Cut
  • 19.3 Four Signs of a Bad Cut
  • 19.4 Problems
20. Abstraction in Layers
  • 20.1 The Box Cutter
  • 20.2 Choosing the Level
  • 20.3 Leaks
  • 20.4 Too Many Layers
  • 20.5 Problems
21. Interfaces and Contracts
  • 21.1 The Hundred-Thousandth Invoice
  • 21.2 Three Clauses
  • 21.3 The Narrow Interface
  • 21.4 Who Checks, Who Answers
  • 21.5 Problems
22. Partial Verification
  • 22.1 The Ride That Grew
  • 22.2 From the Contract to Tests
  • 22.3 Stand-ins
  • 22.4 Which Part First
  • 22.5 When a Part Will Not Be Verified
  • 22.6 Problems
23. Assembly: From Correct Parts to a Correct Whole
  • 23.1 The Quiet Dashboard
  • 23.2 Four Sources of Assembly Error
  • 23.3 What the Whole Costs
  • 23.4 The Bottleneck
  • 23.5 Problems
24. When Understanding Changes Midway
  • 24.1 The Subtitle Lines
  • 24.2 Early Warning Signals
  • 24.3 The Decision Equation
  • 24.4 Designing for Change in Advance
  • 24.5 Problems
25. The Final Problem
  • 25.1 The Book Bus
  • 25.2 Reading It
  • 25.3 Cutting It
  • 25.4 Distances
  • 25.5 The First Cut, and Where It Broke
  • 25.6 The Judge
  • 25.7 A Route First, Then the Days
  • 25.8 Improving Whole Plans
  • 25.9 How Good Is It
  • 25.10 The Requests
  • 25.11 Putting It Together
  • 25.12 The Map of the Mindset
  • 25.13 Open Problems
Appendix A — Bridge to Python and C++
  • What changes in translation
  • The algorithms
  • How these were checked
Appendix B — Answers
  • Chapter 1 · Before You Solve: Reading the Problem
  • Chapter 2 · Brute Force as a Starting Point, Not a Failure
  • Chapter 3 · The First Engine
  • Chapter 4 · Invariants: What Stays True?
  • Chapter 5 · Looking for Structure That Is Already There
  • Chapter 6 · Naming the State
  • Chapter 7 · When to Stop: The Limits of Improvement
  • Interlude · Routing: Problems With No Chapter Attached
  • Chapter 8 · Trading Time for Space, and Back
  • Chapter 9 · Reduction: Turn It Into a Problem You Know
  • Chapter 10 · Choosing Without Looking Back: When It Works and When It Deceives
  • Chapter 11 · Divide and Conquer: Where the Work Really Is
  • Chapter 12 · From Induction to Stored Answers
  • Chapter 13 · Searching Huge Spaces: Abandon Early, Choose Well
  • Chapter 14 · Thinking in Graphs: Finding the Network Inside the Problem
  • Chapter 15 · Randomness as a Tool: When a Coin Beats a Plan
  • Chapter 16 · When There Is No Efficient Solution: Hardness as an Engineering Decision
  • Chapter 17 · Complex or Long: Telling the Difficulty Apart
  • Chapter 18 · Drawing Boundaries: What Is Inside the Problem and What Is Outside
  • Chapter 19 · Decomposition: Where to Cut
  • Chapter 20 · Abstraction in Layers
  • Chapter 21 · Interfaces and Contracts
  • Chapter 22 · Partial Verification
  • Chapter 23 · Assembly: From Correct Parts to a Correct Whole
  • Chapter 24 · When Understanding Changes Midway
Index

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

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.)

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