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.

O(n²)O(n log n)O(n)O(log n)O(1)input size n →steps
How the common complexity classes grow. By n = 32, O(n²) is already 32 times O(n).
Big-ONameExampleSteps for n = 1 000n = 1 000 000
O(1)ConstantHash table lookup, array index11
O(log n)LogarithmicBinary search, heap push1020
O(n)LinearScanning a list1 0001 000 000
O(n log n)LinearithmicMerge sort, quicksort (average)10 00020 000 000
O(n²)QuadraticBubble sort, nested loops1 000 00010¹²

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.

Open the catalog

Dynamic array

Values stored side by side in one block of memory that grows by doubling: O(1) access by index and amortized O(1) appends.

Linked list

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.

Queue & deque

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).

Ring buffer

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.

Stack

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.

Bloom filter

A compact bit array that answers "definitely not present" or "probably present" using several hash functions: tiny memory, no false negatives.

Hash table

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.

LRU cache

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).

Balanced search trees

Binary search trees that rebalance themselves with rotations, guaranteeing O(log n) search, insert and delete whatever order keys arrive in.

Binary heap

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).

Binary search tree

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.

Trie

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.

Graph

A set of vertices connected by edges, stored as an adjacency list or matrix: the structure behind maps, networks, dependencies and recommendations.

Union-find

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.

Open the catalog

Binary search

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.

Sliding window

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).

Two pointers

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²).

Breadth-first search

Explore a graph level by level from a start vertex using a queue, finding the shortest path in number of edges to everything reachable.

Depth-first search

Explore a graph by going as deep as possible along each path before backtracking: the basis of cycle detection, topological sort and maze solving.

Dijkstra's shortest path

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.

Minimum spanning tree

Connect every vertex of a weighted graph using the cheapest possible set of edges, with no cycles: Kruskal's and Prim's greedy algorithms.

Topological sort

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.

Divide and conquer

Split a problem into independent smaller parts, solve each recursively, and combine the answers: the idea behind merge sort, quicksort and binary search.

Dynamic programming

Solve problems with overlapping subproblems by computing each one once and reusing the answer, often by filling a table: edit distance, knapsack, routing.

Greedy algorithms

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.

Recursion & backtracking

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.

Open the catalog

Bubble sort

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.

Insertion sort

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.

Selection sort

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.

Heapsort

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.

Merge sort

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.

Quicksort

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.

Counting sort

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.

Radix sort

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.

Timsort

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.