</>CodexBash
Learn path · free to read

One idea at a time, then the problems that use it

A path from counting digits to graphs, numbered so you always know where you are. Each lesson explains a single technique, shows it running step by step, and hands you the problems where it is the right answer. C, C++, Java and Python are only ways of talking to a computer. The thinking is what carries across.

36 lessons, in order. Start at one, or start where you are stuck.

Arrays and hashing

step 12
  1. Carrying an answer forward: Kadane's algorithm

    Intermediate12 min read

    The best subarray ending here is either this element alone or this element added to the best one ending just before it. That single sentence is the algorithm.

    0 of 5 practice problems solved

Two pointers and windows

step 14
  1. Sliding windows: fixed size, then variable

    Intermediate14 min read

    A window is a stretch of a list you keep looking at while both its ends move right. Get the fixed-size version first, because the variable one is the same idea with a rule instead of a length.

    0 of 8 practice problems solved

Searching

step 18
  1. Binary search on the answer, when nothing is sorted

    Intermediate13 min read

    A dozen questions that look nothing alike are one question: guess an answer, check it in a single pass, and use the fact that answers come in a run of no followed by a run of yes.

    0 of 4 practice problems solved

Linked lists

step 20
  1. Loops in a linked list: two walkers, no extra memory

    Intermediate13 min read

    A list that points back at itself has no end to find. One slow walker and one fast one settle whether there is a loop, how long it is, where it starts and how to cut it, using two variables and nothing else.

    0 of 5 practice problems solved

Stacks and queues

step 23
  1. Monotonic stacks: the nearest bigger thing, in one pass

    Intermediate12 min read

    A whole family of problems asks for the nearest larger or smaller value. They all fall to a stack kept in sorted order, and they all cost one pass.

    0 of 5 practice problems solved

Trees

step 26
  1. Search trees: one rule, and everything that follows from it

    Intermediate13 min read

    A search tree keeps smaller on the left and larger on the right. Every operation on one, and every trap, comes from taking that rule seriously across the whole tree rather than one node at a time.

    0 of 3 practice problems solved

Heaps

step 27
  1. Heaps: when you only ever want the next one

    Intermediate10 min read

    A tree that gives up on full sorting and keeps only one promise: the smallest is on top. That is cheaper, and often it is all you need.

    0 of 4 practice problems solved

Graphs

steps 29 to 31
  1. Graphs: things, and the connections between them

    Intermediate11 min read

    Drop the rule that every node has one parent and you have a graph. Two ways to walk one, and the choice decides what you can prove.

    0 of 4 practice problems solved

  2. Breadth first and depth first: the same loop, a different container

    Intermediate13 min read

    Both walks visit everything reachable. Only one of them answers questions about shortest, and the difference between them is which end you take things off.

    0 of 4 practice problems solved

  3. Topological order: doing things in an order that works

    Intermediate16 min read

    Courses with prerequisites, build steps, task lists. If the dependencies contain a cycle there is no valid order, and finding that out is the same algorithm.

    0 of 3 practice problems solved

Backtracking

step 32
  1. Backtracking: choose, explore, un-choose

    Intermediate15 min read

    When a problem asks for every arrangement rather than one number, you need a way to try each choice and take it back. Three lines do it, and one of them is the line everybody forgets.

    0 of 6 practice problems solved

Dynamic programming

step 35
  1. Grid DP: when the table is the same shape as the problem

    Intermediate12 min read

    A grid you can only cross one way is where dynamic programming is easiest to see, because the table of answers has the same shape as the board and you can watch it fill.

    0 of 3 practice problems solved

Greedy

step 36
  1. Greedy: when the obvious move is the right one

    Intermediate11 min read

    Taking the best step available right now is usually wrong. The interesting part is the small number of problems where it is provably right, and how to tell.

    0 of 4 practice problems solved