Data Structures
Interactive guides to master the building blocks of algorithms.
Arrays & Strings
Why Learn Data Structures?
The right container changes everything β speed, memory, and eleganceβ
Static Arrays
A row of numbered boxes β fast to grab, fixed in sizeβ
Dynamic Array
An array that grows automatically when it runs out of spaceβ
Two Pointers
Two fingers tracing an array to solve problems in one passβ
Sliding Window
A picture frame scanning over an array to solve sub-array problems in \(O(n)\) timeβ
Prefix Sums
Pre-calculate a running total so you can sum any subarray in instant \(O(1)\) timeβ
Linked Lists
Singly Linked List
A chain of boxes β each one knows only where the next box isβ
Doubly Linked List
A two-way chain β each node knows both its neighbour ahead and the one behindβ
Circular Linked List
A chain with no end β the last link points back to the firstβ
Floyd's Cycle Detection
Catch a loop in a linked list using just two pointers and no extra memoryβ
Reversing a Linked List
Flip the entire chain so the last node becomes first β iteratively or recursivelyβ
Stacks & Queues
Stack
A pile of plates β the last one you put on is the first one you take offβ
Queue
A line of people waiting β the first one in is the first one servedβ
Deque
A line you can join or leave from either end β front or back, your choiceβ
Monotonic Stack
A stack that stays sorted β used to find the next bigger or smaller element fastβ
Monotonic Queue
A queue that tracks the max or min of a sliding window in constant timeβ
Hash Tables
Trees
Binary Trees
A family tree where every parent has at most two childrenβ
Binary Search Tree
A sorted binary tree β left is smaller, right is biggerβ
BST Balancing
An unbalanced BST is just a slow linked list in disguiseβ
AVL Tree
A self-balancing BST that rebalances itself after every insert or deleteβ
Red-Black Tree
A self-balancing BST used inside Java's TreeMap and C++'s std::mapβ
Segment Tree
Answer range queries in \(O(\log n)\) with \(O(\log n)\) point updatesβ
Fenwick Tree (BIT)
Prefix sums in \(O(\log n)\) with elegant bit manipulation β simpler than a segment treeβ
Trie (Prefix Tree)
Store strings by their prefixes β autocomplete in \(O(L)\) timeβ
N-ary Trees
When nodes can have any number of children β file systems, DOM, org chartsβ
Heaps & Priority Queues
Heaps
Always know the smallest (or largest) in \(O(1)\) β with \(O(\log n)\) updatesβ
Heap Sort
Sort in-place with guaranteed \(O(n \log n)\) β no extra memory neededβ
Priority Queue Patterns
K-th largest, merge K lists, top-K frequent β solved with a small heapβ
Two-Heap Pattern
Split the data in half β find the median in \(O(1)\) alwaysβ
Graphs
Graph Representation
How to store a map of cities and roads in memoryβ
Breadth-First Search
Explore a city map ring by ring β nearest neighbors firstβ
Depth-First Search
Follow one road as far as it goes before backtrackingβ
Topological Sort
Order cities so every one-way road points forward, never backwardβ
Union-Find (Disjoint Set Union)
Track connected groups β merge them near-instantlyβ
Minimum Spanning Tree
Connect all cities with the least total road construction costβ
Dijkstra's Algorithm
GPS for graphs β find the cheapest route from one node to all othersβ
Bellman-Ford Algorithm
Shortest paths with negative weights β and negative cycle detectionβ
Floyd-Warshall Algorithm
All-pairs shortest paths in one elegant triple loopβ
Advanced / Specialized
Skip List
A linked list with express lanes β search in \(O(\log n)\) on averageβ
Bloom Filter
Ask 'is this here?' in \(O(1)\) β maybe yes, definitely noβ
LRU Cache
Keep the most recent items fast β evict the forgotten onesβ
Sparse Table
Answer range minimum queries in \(O(1)\) after \(O(n \log n)\) setupβ
Suffix Array & LCP Array
Sort every suffix of a string β unlock powerful substring queriesβ