00 - How To Use This Book
Read this once before solving anything. Ten minutes.
The one rule
Never read the Java before you understand the pseudocode.
Java is dense. A beginner reading Java sees syntax noise – generics, int[] declarations,
HashMap<Integer,Integer> boilerplate – and misses the algorithm underneath. Pseudocode strips
that noise away so the idea is visible. Once the idea is clear, the Java writes itself.
Every problem in this book is laid out in this fixed order:
- Problem – what is being asked, in plain English.
- Intuition – the first thought a strong solver has. Why does this pattern fit?
- Pseudocode – language-neutral steps. Read this slowly.
- Java solution – the pseudocode, translated.
- Complexity – time and memory, with reasoning.
- Dry-run – the algorithm executed on a concrete input, line by line.
The 20-minute rule
For each problem, spend up to 20 minutes trying it yourself before reading the solution.
If you solve it, compare with the book. If you don’t, read the Intuition section only, then
try again for another 15 minutes. Only then read pseudocode + Java.
Why 20 minutes? Shorter and you train yourself to give up. Longer and you waste time. Twenty
minutes is the sweet spot where struggle produces learning.
When you see a new problem, ask these diagnostic questions in order:
- Is the data sorted, or can I sort it?
- Yes -> Two Pointers or Binary Search.
- Am I looking for a contiguous subarray / substring with some property?
- Do I need to count, check existence, or group items?
- Does the problem involve pairs/triples, or reversing order?
- “recent / next greater / valid pair” -> Stack.
- Is it a tree or graph traversal?
- Tree -> Trees pattern. Graph -> Graphs pattern.
- Does the problem ask “how many ways” or “best/worst over all possibilities”?
- Yes -> Dynamic Programming.
- Does it ask for all combinations / permutations / subsets?
- “Top K”, “Kth largest”, scheduling, merging sorted streams?
- Yes -> Heap / Priority Queue.
- Word prefix lookups?
- Start/end pairs, meetings, ranges?
- Can I make the locally-best choice at each step and trust it globally?
- Flags, parity, power-of-two, “without extra space”?
These are heuristics, not laws. After ~30 problems the recognition becomes automatic.
How to use the tests
Every problem folder has SolutionTest.java. Run it:
cd patterns/01-arrays-hashing/217-contains-duplicate
javac Solution.java SolutionTest.java && java SolutionTest
You should see:
If a test fails, read the failing case. The test file tells you exactly which input broke.
LeetCode itself uses hidden tests; the tests here are the same public cases LeetCode shows you,
plus a couple of edge cases per problem.
Spaced repetition
Do not solve 100 problems in a week. You will forget them. Recommended pace:
- Week 1-2: Patterns 1-4 (Arrays/Hashing, Two Pointers, Sliding Window, Stack). ~25 problems.
- Week 3-4: Patterns 5-9 (Binary Search, Linked List, Trees, Tries, Heap). ~32 problems.
- Week 5-6: Patterns 10-13 (Backtracking, Graphs, Greedy, Intervals). ~25 problems.
- Week 7-8: Patterns 14-16 (1-D DP, 2-D DP, Bit Manipulation). ~18 problems.
After finishing a pattern, re-solve 2 of its problems from memory 3 days later. That is
what makes patterns stick.
When you get stuck
- Read the “Concepts used” callout at the top of the problem README first. It lists every
prerequisite idea in plain English with a link to the glossary. If a concept is unfamiliar,
read its glossary entry before continuing.
- Re-read the pattern README (the
patterns/XX-name/README.md file). It lists the
pattern’s trigger signals and a template.
- Read only the Intuition section of the problem, then try again.
- Only as a last resort, read pseudocode + Java together.
- After seeing the solution, close the book and re-type the solution from memory. Typing it
from understanding (not copying) is what cements it.
The checkpoint quizzes
Every problem README now contains two short “Checkpoint” quiz blocks – one right after the
Intuition, one right after the Dry-Run. Each pattern’s README.md ends with a longer
Pattern Mastery Quiz. The questions ramp simplest first (recall a fact) and get harder
(move toward applying and transferring the idea), so a beginner can always answer Q1 and is
stretched by the last question.
How to use them well:
- Attempt every question before expanding the answer. A wrong guess you correct yourself
sticks far better than a right answer you merely read.
- Use the collapsible answers. Each answer is wrapped in a
<details> block – click
“Show answer” only after you have committed to a guess. On GitHub these render as collapsible
sections; in a plain text editor the answer is visible underneath the tag.
- Checkpoint A checks the concept (do you understand why the pattern fits?).
Checkpoint B checks execution (can you trace a new input, spot an edge case, adapt the
idea?). If you trip on Checkpoint A, re-read Intuition. If you trip on Checkpoint B, re-do the
Dry-Run with your own numbers.
- The Pattern Mastery Quiz is your gate to move on. If you can answer its pattern-recognition
questions (given a brand-new problem statement, which tool fits?), you are ready for the next
pattern. If not, re-solve two problems in this section from memory first.
Retrieval practice – forcing yourself to recall before checking – is the single most effective
way to make a pattern stick. Treat the quizzes as part of the lesson, not a test.
Where to look up unfamiliar terms
Every technical term used in this book is defined in plain English in
docs/10-glossary.md. When a problem README uses a term for the first
time, it links to the glossary entry. Bookmark the glossary – you will reference it often in
the first few patterns.
What this book does NOT cover
- Hard problems (by design – see the main README).
- Concurrency / multithreading LeetCode problems.
- SQL problems.
- System design.
This is a patterns and algorithms book for the coding interview’s algorithmic portion.
Next: 01-patterns-overview.md