DSA / Linear Structures / Search & Recursion
DSA · Problem Editorials · A Field Guide

Problems, worked properly

Not a solutions dump. Each entry is a long-form editorial that starts where you start — with a problem statement and no idea which tool to reach for — and walks the whole distance to working Java, stopping at every place the reasoning could have gone wrong.

18 editorials· 7 categories· 5h 30m of reading· every one ends with a one-month recall check

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.

18 of 18 shown
0 / 18 read
01

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.

03 editorials
  1. 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.

    Auxiliary stackInvariant designO(1) queriesData-structure design
    Easy O(1) per op O(N) space 16 min
  2. 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.

    Stack machineExpression parsingOperand orderInfix → postfix
    Medium O(n) time O(n) space 20 min
  3. 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.

    Stack of indicesSentinelBoundary arithmeticTwo-pass O(1) space
    Hard O(n) time O(n) space 20 min
02

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.

02 editorials
  1. 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.

    Fixed windowCount vectorMatch counterSign convention
    Medium O(N+M) time O(1) space 20 min
  2. 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.

    Fixed windowFrequency mapAdd/remove deltaWhy a set fails
    Medium O(N) time O(B) space 19 min
03

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.

02 editorials
  1. 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.

    Two passesFrequency mapOrder preservationWhy a set is wrong
    Easy O(N) time O(N) space 19 min
  2. 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.

    CountingPairs plus a centreParityReframing
    Easy O(N) time O(1) space 16 min
04

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.

01 editorial
  1. 08

    Top K frequent words

    Count everything, then rank by two rules at once: most frequent first, ties broken alphabetically. The real algorithm lives in the comparator.

    Count then rankComparator designBounded heapTwo-key ordering
    Medium O(N+U log K) O(U) space 15 min
06

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.

03 editorials
  1. 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.

    Six core movesXOR cancellationn & (n-1)Cheat sheet
    Medium O(n) time O(1) space 21 min
  2. 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.

    Per-bit columnsCounting contributionLoop inversion
    Medium O(n) time O(1) space 18 min
  3. 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.

    DoublingShift and subtract32-bit overflow
    Medium O(log n) time O(1) space 18 min
07

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.

03 editorials
  1. 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.

    Greedy three-step walkRightmost changeReverse the suffix
    Medium O(N) time O(1) space 20 min
  2. 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?

    Prefix maxSuffix minSafe boundary
    Hard O(N) time O(N) space 16 min
  3. 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.

    Invariant huntingParity argumentSum vs XOR
    Medium O(N) time O(1) space 19 min
Nothing matches . Try a pattern name — stack, window, comparator — or clear the difficulty filters.

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.