What these are
The write-up an editorial should be: why the obvious idea fails, what observation forces the data structure, then the code — in that order.
How to read one
Stop at the dry-run table and predict the next row yourself. Every page has a live simulator — drive it before you read the explanation of it.
The one-month rule
Each editorial closes with the single sentence worth remembering a month later. If you can say it cold, the problem is genuinely yours.
Stacks & Monotonic Order
Problems where the thing you saw most recently is the thing you need next. The stack is almost never the interesting decision — what you choose to put on it is.
-
01
Design a min stack
Push and pop in O(1) is free. The trick is answering “what is the minimum right now?” instantly — without ever scanning.
-
02
Evaluating postfix expressions
This is not a string problem. It is a memory problem — and the way you are forced to remember things points straight at one data structure.
-
03
Longest valid parentheses
The trick is not storing brackets — it is storing indices. The answer is a length, and lengths are measured between boundaries.
Sliding Window
A window moves one step and almost nothing changes. These are won by tracking the difference — what left, what entered — instead of recomputing the whole.
-
04
Every anagram of a pattern
An anagram is the same characters in a different order — so forget the letters and track the counts. When the counts match, you have found one.
-
05
Distinct elements in every window
Neighbouring windows share almost everything. Do not recount — track only what leaves and what enters, and keep a running tally.
Hashing & Frequency
Counting as a first-class step. Once you hold the totals, the question usually stops being about the sequence and turns into arithmetic on the counts.
-
06
The first unique element
You cannot tell whether the first value is one-of-a-kind until you have seen the whole array. Learn the totals first, then walk in order.
-
07
The longest palindrome you can build
You may rearrange freely — so positions stop mattering and counts take over. It becomes pure bookkeeping: how many pairs, plus one centre.
Heaps & Ordering
When you need the top of an order without paying to sort everything — and when the real algorithm turns out to live inside the comparator.
Binary Search
Halving works on far more than a sorted array looking for a target. What it really needs is one yes/no question whose answer flips exactly once — and the hard part is finding that question.
-
09
First and last position of a value
One binary search finds an occurrence. To pin down the edges of a run of duplicates, don’t stop when you find it — keep leaning toward the boundary.
-
10
Search a rotated sorted array
Rotation breaks global order in exactly one place — so around any midpoint one half is always still sorted. That surviving structure is all binary search needs.
-
11
Find a peak element
There is no target and the array is not sorted. But the local slope always points toward higher ground — and uphill has to end in a summit somewhere.
-
12
Preimage of the factorial zeroes
How many x give x! exactly k trailing zeroes? You never touch a factorial — you count factors of five, notice the count only climbs, and search a number line with no array under it.
Bit Manipulation
A small vocabulary, not a bag of tricks. Read the low bit, drop it, cancel with XOR, delete the lowest one — every classic bit problem is a sentence built from those words.
-
13
The bit vocabulary
Eight classic problems, but not eight tricks. They are all built from a handful of moves. Learn the words, and the sentences write themselves.
-
14
Total Hamming distance
Sum the differing bits over every pair. The trick is not faster pair comparison — it is refusing to look at pairs at all, and counting one bit column at a time.
-
15
Divide two integers
No multiply, no divide, no modulo. Division is just “how many divisors fit” — counted in powers of two instead of one at a time.
Arrays & Invariants
Problems that look like they want a simulation. Each one is really asking for a property the operations cannot change — find it and the answer is a single pass, or arithmetic.
-
16
The next permutation
Reordering a sequence into the very next one in dictionary order. Not by listing all N! arrangements — by making the smallest possible change as far right as it can go.
-
17
Max chunks to make it sorted
Do not simulate sorting every partition. Ask the sharper question: where can you cut so that nothing ever needs to cross the line?
-
18
Reaching zero by split and merge
Split a number into two that sum to it; merge two into their XOR. Do not hunt for the moves — find the property the moves cannot touch.
Still to be written
The catalogue grows one editorial at a time. These categories are reserved — the topic notes for them already exist under DSA; what is missing is a worked problem written to this standard.
- Arrays & Two Pointers
- Intervals
- Recursion & Backtracking
- Trees
- Graphs
- Dynamic Programming
- Greedy
- Tries & Matrices