Algorithm mechanisms, visualized
Most problems are a handful of mechanisms in disguise. Learn the signals that point to each one, then run it and change the input.
Sliding Window
Turn nested-loop scans of contiguous ranges into linear time.
Two Pointers
Walk a sorted array from both ends to skip impossible work.
Binary Search
Halve the range that can hold the answer, on a sorted array or a range of answers.
Dynamic Programming
Break a problem into overlapping subproblems and reuse the answers.
Backtracking
Explore every choice, undo, and prune the dead ends.
Graph Traversal (BFS & DFS)
Visit every node once: breadth-first for shortest paths, depth-first for structure.
Topological Sort
Order tasks so every dependency comes before what needs it.
Heap / Priority Queue
Always pull the smallest or largest element in log time.
Monotonic Stack
Keep a stack in sorted order to find next-greater elements in one pass.
Prefix Sums
Precompute running totals to answer range queries in O(1).
Hashing & Frequency Maps
Trade memory for O(1) lookups, counts, and dedupe.
Fast & Slow Pointers
Two speeds through a sequence to find cycles and midpoints.