Get Premium

Data structures and algorithms, drawn as they run.

13 topics, one page each. Every page opens with a step-through: the structure on one side, the code highlighting line by line on the other. Then the costs, the signals in a problem that call for it, and the mistakes people make with it.

Pick any topic below and press play. Everything on the coding track is free.

  • lo
  • hi
  • mid
  • answer
pythonruns with the steps
1def search(nums, target):
2 lo, hi = 0, len(nums) - 1
3 while lo <= hi:
4 mid = lo + (hi - lo) // 2
5 if nums[mid] == target:
6 return mid
7 if nums[mid] < target:
8 lo = mid + 1
9 else:
10 hi = mid - 1
11 return -1
12
13def first_true(lo, hi, ok):
14 while lo < hi:
15 mid = lo + (hi - lo) // 2
16 if ok(mid):
17 hi = mid # keep mid
18 else:
19 lo = mid + 1
20 return lo
01 / 10Sorted inputcandidates 8

Eight sorted values and a target of 23. A linear scan costs up to eight comparisons. Sorted order buys something stronger: one comparison can rule out half the array at once.

Read Binary Search

The track, in order

Each topic builds on the ones before it. Colours group related ideas.

  • Arrays and sequences
  • Lookup and priority
  • Pointers and stacks
  • Trees and graphs
  • Search and optimisation
  1. 1Lookup and priority7 min

    Hash Tables

    An array you index by content instead of position. The one structure that turns a nested loop into a single pass, and the one whose O(1) comes with conditions.

    O(1)Lookup / insert / delete

  2. 2Arrays and sequences6 min

    Two Pointers

    Two indices walking the same array under a rule that lets each move retire part of the search. The technique that collapses a nested loop into one pass — when the data has an order to exploit.

    O(n)Opposite ends (converge)

  3. 3Arrays and sequences6 min

    Sliding Window

    A contiguous span with two edges that only ever move forward. Turns "check every subarray" into one pass — as long as the window's validity is monotone.

    O(n)Variable window (longest / shortest run)

  4. 4Arrays and sequences6 min

    Binary Search

    Halve the candidate set on every comparison. Trivial to describe, easy to write wrong, and far more general than "find a value in a sorted array".

    O(log n)Search a sorted array

  5. 5Pointers and stacks5 min

    Stacks

    Last in, first out — and the structure behind matching, undo, recursion, and the monotonic trick that answers "next greater" for every element in one pass.

    O(1)Push / pop / peek

  6. 6Pointers and stacks5 min

    Linked Lists

    Nodes connected by references, with no index and no locality. Almost every question is pointer surgery: rewire the links without losing the rest of the list.

    O(n)Access by position

  7. 7Trees and graphs6 min

    Trees and Traversals

    Recursive structure, recursive solutions. Most tree questions are one traversal with the work moved to the right point in the recursion — and the cost is decided by the height, not the count.

    O(n)Any traversal (DFS or BFS)

  8. 8Lookup and priority5 min

    Heaps

    A tree stored in an array that keeps only one promise: the best element is at the front. Cheaper than sorting, and the right answer whenever a problem says top-k, median, or merge.

    O(1)Peek the minimum

  9. 9Lookup and priority5 min

    Tries

    A tree keyed by characters, where the path spells the word. The structure that answers prefix questions a hash map cannot answer at all.

    O(L)Insert / search a word

  10. 10Trees and graphs6 min

    Graphs

    Nodes and edges, and two traversals that answer most questions about them. The hard part is usually recognising that a problem is a graph at all.

    O(V + E)BFS / DFS (adjacency list)

  11. 11Search and optimisation6 min

    Backtracking

    Depth-first search over decisions, with the decision undone on the way back out. The answer to every "generate all…" and "find an arrangement that works" question.

    O(2ⁿ · n)All subsets

  12. 12Search and optimisation6 min

    Dynamic Programming

    Recursion that refuses to solve the same sub-problem twice. Exponential becomes polynomial the moment the repeated work is written down and reused.

    O(n)1-D table (house robber, climbing stairs)

  13. 13Arrays and sequences6 min

    Intervals

    Ranges with a start and an end. Sort them the right way and almost every question becomes one linear pass — the difficulty is choosing which end to sort by.

    O(n log n)Merge overlapping intervals