Data structures & algorithms
The building blocks of every program: how data is organized, how it's searched and sorted, and what each choice costs. One page each, with diagrams, step-by-step visualizations and TypeScript code.
Choosing the right data structure is often the biggest performance decision in a piece of code. A lookup in a hash table takes about the same time with ten items or ten million; the same lookup in a list grows with every item you add.
Each page explains the idea, shows it in a diagram, lets you step through it on real data, gives the Big-O costs and a tested TypeScript implementation, and says when to choose something else.
| Big-O | Name | Example | Steps for n = 1 000 | n = 1 000 000 |
|---|---|---|---|---|
O(1) | Constant | Hash table lookup, array index | 1 | 1 |
O(log n) | Logarithmic | Binary search, heap push | 10 | 20 |
O(n) | Linear | Scanning a list | 1 000 | 1 000 000 |
O(n log n) | Linearithmic | Merge sort, quicksort (average) | 10 000 | 20 000 000 |
O(n²) | Quadratic | Bubble sort, nested loops | 1 000 000 | 10¹² |
Data structures
How programs organize data: arrays, lists, hash tables, trees, heaps and graphs, each with a diagram, Big-O costs and a TypeScript implementation.
Values stored side by side in one block of memory that grows by doubling: O(1) access by index and amortized O(1) appends.
A chain of nodes where each holds a value and a pointer to the next: O(1) inserts and removals at known positions, O(n) to find anything.
A queue adds at the back and removes from the front, first in, first out; a deque does both at either end. All in O(1).
A fixed-size array whose read and write positions wrap around the end, giving an O(1) queue with no allocation: ideal for streams, logs and recent history.
A last-in, first-out collection: push onto the top, pop from the top, both O(1). Behind function calls, undo, parsing and depth-first search.
A compact bit array that answers "definitely not present" or "probably present" using several hash functions: tiny memory, no false negatives.
Store key–value pairs in an array of buckets chosen by a hash of the key, so lookups, inserts and deletes take constant time on average.
A fixed-size cache that evicts the least recently used item, built from a hash table plus a doubly linked list so every operation is O(1).
Binary search trees that rebalance themselves with rotations, guaranteeing O(log n) search, insert and delete whatever order keys arrive in.
A complete binary tree stored in an array where every parent is smaller than its children, giving the minimum in O(1) and inserts and removals in O(log n).
A binary tree where every left subtree holds smaller keys and every right subtree larger ones: O(log n) search, insert and ordered traversal when balanced.
A tree of characters where each path from the root spells a key, so prefix searches like autocomplete take time proportional to the prefix, not the dataset.
A set of vertices connected by edges, stored as an adjacency list or matrix: the structure behind maps, networks, dependencies and recommendations.
Track which items belong to the same group as groups merge: near-constant-time union and find with path compression and union by rank.
Algorithms
Searching, graph algorithms and the problem-solving techniques behind them, with step-through visualizations and TypeScript code.
Find a value in a sorted array by repeatedly halving the range that could contain it: at most about 20 comparisons for a million items.
Keep a contiguous range that grows on the right and shrinks on the left, updating a running result as it moves, to solve substring and subarray problems in O(n).
Walk two indexes through an array, often from both ends towards the middle, to solve pair and partition problems in one O(n) pass instead of O(n²).
Explore a graph level by level from a start vertex using a queue, finding the shortest path in number of edges to everything reachable.
Explore a graph by going as deep as possible along each path before backtracking: the basis of cycle detection, topological sort and maze solving.
Find the cheapest path from one vertex to all others in a graph with non-negative edge weights, by always settling the closest unfinished vertex next.
Connect every vertex of a weighted graph using the cheapest possible set of edges, with no cycles: Kruskal's and Prim's greedy algorithms.
Order the vertices of a directed acyclic graph so every edge points forward: the order to run tasks, build packages or apply migrations so dependencies come first.
Split a problem into independent smaller parts, solve each recursively, and combine the answers: the idea behind merge sort, quicksort and binary search.
Solve problems with overlapping subproblems by computing each one once and reusing the answer, often by filling a table: edit distance, knapsack, routing.
Build a solution by always taking the choice that looks best right now, never reconsidering: fast and simple, and optimal only for problems with the right structure.
Solve a problem by solving smaller versions of itself; backtracking builds solutions one choice at a time and undoes choices that lead to dead ends.
Sorting algorithms
From bubble sort to Timsort: how each algorithm works, step by step, what it costs, and which one your language actually uses.
Repeatedly swap adjacent values that are out of order until a full pass makes no swaps: simple to understand, O(n²), and almost never the right choice.
Take each value and slide it left into its place among the already-sorted values: O(n²) in general, but very fast on small or nearly sorted arrays.
Find the smallest remaining value and swap it into place, one position at a time: always O(n²) comparisons, but at most n − 1 swaps.
Turn the array into a max-heap, then repeatedly move the largest value to the end: guaranteed O(n log n) with O(1) extra memory, but not stable.
Split the array in half, sort each half, then merge them: a guaranteed O(n log n), stable sort, and the basis for sorting data too big for memory.
Pick a pivot, partition the array into smaller and larger values around it, then sort each side the same way: O(n log n) on average, in place.
Count how many times each key value occurs, then write the values back in order: O(n + k) time for small integer keys, with no comparisons at all.
Sort integers digit by digit, from least to most significant, using a stable counting sort on each digit: O(d · n) for d-digit keys.
The merge sort and insertion sort hybrid behind Python, Java and V8's built-in sorts: stable, O(n log n), and very fast on partly ordered data.