llms.txt
Compare with other sites' llms.txt files →# Algorhythm
> An algorithmic-thinking gym. Interactive animations that teach the mental
> model behind algorithm patterns — so you spend hours less staring at
> problems and more time recognizing the trick.
Algorhythm is a free, open-source pattern library covering 26 algorithm
patterns most common in coding interviews — 12 multi-sub-pattern families
plus 14 standalone patterns. Each pattern ships with:
- A one-sentence essence (the trick, distilled).
- A pseudocode signature.
- "When to recognize this pattern" cues.
- Common pitfalls.
- 4 LeetCode practice problems, ordered by difficulty.
- An interactive animation that visualizes a representative algorithm
on real input, step-by-step. Editable input, scrubbable timeline.
All content available in English, Simplified Chinese, and Japanese. The
URLs below default to English; swap `/en/` for `/zh/` or `/ja/` for the
other locales.
## Families (each links to a multi-sub-pattern collection)
- [Two Pointers](https://algo-rhythm.dev/en/two-pointers/): Two indices over the same sequence, maintaining an invariant.
- [Dynamic Programming](https://algo-rhythm.dev/en/dp/): A table fills itself, one cell at a time; each entry composes from a constant number of earlier ones.
- [Prefix Sum](https://algo-rhythm.dev/en/prefix-sum/): Pay O(N) once to make every range query O(1) — and flip it inside-out for cheap range updates.
- [Shortest Path](https://algo-rhythm.dev/en/shortest-path/): Find the shortest route from a source; mechanism splits by what the edges carry.
## Two Pointers sub-patterns
- [Converging Pointers](https://algo-rhythm.dev/en/two-pointers/converging/): Sorted array, answer is a pair — pointers walk from both ends, the comparison forces each move.
- [Fast / Slow](https://algo-rhythm.dev/en/two-pointers/fast-slow/): Two cursors at different speeds — in-place modification or cycle detection.
- [Sliding Window](https://algo-rhythm.dev/en/two-pointers/sliding-window/): Grow the window from the right until the invariant breaks; shrink from the left until it holds again.
## Dynamic Programming sub-patterns
- [1-D Linear](https://algo-rhythm.dev/en/dp/1d/): Each dp[i] answers a smaller version of the same problem, composing from O(1) earlier indices.
- [2-D Grid](https://algo-rhythm.dev/en/dp/2d/): State has two indices — a grid, or two interacting sequences.
- [Knapsack](https://algo-rhythm.dev/en/dp/knapsack/): Each item triggers a choose-or-skip decision under a capacity constraint.
- [Interval](https://algo-rhythm.dev/en/dp/interval/): State is a range (i, j) and the answer composes from inner intervals.
- [State-Machine](https://algo-rhythm.dev/en/dp/state-machine/): Each index can be in one of several finite states with rules for transitioning between them.
## Prefix Sum sub-patterns
- [1-D Prefix Sum](https://algo-rhythm.dev/en/prefix-sum/1d/): Build the cumulative table once; every range-sum query is a single subtraction.
- [2-D Prefix Sum](https://algo-rhythm.dev/en/prefix-sum/2d/): Rectangle queries on a matrix via inclusion-exclusion on four corners.
- [Difference Array](https://algo-rhythm.dev/en/prefix-sum/diff/): The dual — cheap range updates, single read of every cell at the end.
- [Prefix Sum + Hash Map](https://algo-rhythm.dev/en/prefix-sum/hash/): Count subarrays summing to k by storing seen prefix values.
## Shortest Path sub-patterns
- [Unweighted BFS](https://algo-rhythm.dev/en/shortest-path/bfs/): When every edge costs the same, BFS visits in distance order — first visit = shortest path.
- [Dijkstra](https://algo-rhythm.dev/en/shortest-path/dijkstra/): Non-negative weights, priority-queue greedy.
- [Bellman-Ford](https://algo-rhythm.dev/en/shortest-path/bellman-ford/): V−1 relax rounds; handles negative weights and detects negative cycles.
- [Floyd-Warshall](https://algo-rhythm.dev/en/shortest-path/floyd/): All-pairs DP via intermediates; O(V³).
## Single patterns
- [Binary Search Variants](https://algo-rhythm.dev/en/binary-search/): Three boundary conditions (exact, leftmost, rightmost); the field's largest unsolved teaching problem.
- [Backtracking](https://algo-rhythm.dev/en/backtracking/): Decision-tree expansion — try, recurse, undo. The same recursion frame builds every candidate.
- [Monotonic Stack](https://algo-rhythm.dev/en/monotonic-stack/): A stack that only ever holds a sorted prefix; next-greater / histogram class.
- [BFS / DFS on Grids](https://algo-rhythm.dev/en/bfs-dfs/): A queue gives breadth, a stack gives depth — same algorithm, two traversal orders.
- [Heap & Priority Queue](https://algo-rhythm.dev/en/heap/): Track the k most extreme; the root of a min-heap of size k is the worst of the k.
- [Tree Traversal](https://algo-rhythm.dev/en/tree-traversal/): Every traversal visits every node once — only timing relative to children's recursion differs.
- [Linked List Manipulation](https://algo-rhythm.dev/en/linked-list/): Three pointers — prev, curr, next — rewrite a list in place without losing the tail.
- [Union Find](https://algo-rhythm.dev/en/union-find/): Maintain a partition of N elements into disjoint components; merge in near-constant time.
- [Trie](https://algo-rhythm.dev/en/trie/): A tree of characters; shared prefixes share paths.
- [Topological Sort](https://algo-rhythm.dev/en/topological-sort/): Linearize a DAG so every edge points left-to-right; each node enters the order after all prerequisites.
- [Merge Intervals](https://algo-rhythm.dev/en/merge-intervals/): Sort by start, then sweep — each interval either extends the current merge or commits it and starts a new one.
- [Bit Manipulation](https://algo-rhythm.dev/en/bit-manipulation/): Treat integers as their bits, not their values — XOR a number against itself zeros it, against zero keeps it.
- [Cyclic Sort](https://algo-rhythm.dev/en/cyclic-sort/): When values live in [0, N] or [1, N], the index is the pointer — swap to home or sign-flip to mark seen.
- [Sweep Line](https://algo-rhythm.dev/en/sweep-line/): Turn each interval into +1 / −1 endpoint events, sort by position, then sweep — the running active count's peak is the answer.
- [Greedy](https://algo-rhythm.dev/en/greedy/): At every step, take the locally optimal commit — and prove it never gets undone. A family of three sub-patterns: interval scheduling (sort by end, take greedily), jump game (track farthest reachable index), gas station (reset start on cumulative failure).
- [Interval Scheduling](https://algo-rhythm.dev/en/greedy/interval-scheduling/): Sort intervals by end, then greedily take each one whose start clears the last taken end — the exchange argument proves earliest-end is always safe.
- [Jump Game](https://algo-rhythm.dev/en/greedy/jump-game/): Scan the array tracking the farthest reachable index; if the scan ever passes that frontier, the end is unreachable.
- [Gas Station](https://algo-rhythm.dev/en/greedy/gas-station/): Walk the route once, tracking a running tank; the instant it dips below zero, every starting prefix in that segment is dead — reset start past the failure.
- [Segment Tree](https://algo-rhythm.dev/en/segment-tree/): Build a balanced binary tree where each node holds the aggregate of a range — any range decomposes into O(log N) canonical pieces, so range queries and point updates are both O(log N) instead of O(N).
- [Fenwick Tree](https://algo-rhythm.dev/en/fenwick-tree/): A 1-indexed array where each cell holds the sum of a power-of-two-sized range — length equals the lowest set bit. Point updates walk up by adding the lowest set bit; prefix sums walk down by subtracting it. Both O(log N), ten lines of code, with no tree to allocate.
- [String Matching](https://algo-rhythm.dev/en/string-matching/): Find a pattern P inside a text T in O(N + M) instead of O(N·M). Family of 3 sub-patterns sharing one trick — precompute structural info from the pattern so the scan can skip work on every mismatch.
- [KMP — failure function](https://algo-rhythm.dev/en/string-matching/kmp/): Build the longest proper prefix-that-is-also-a-suffix for every prefix of the pattern, then jump on mismatch instead of re-scanning. Deterministic O(N + M).
- [Rabin-Karp — rolling hash](https://algo-rhythm.dev/en/string-matching/rabin-karp/): Hash compares numbers, not characters. Slide the window in O(1) per step; verify hash matches to rule out collisions.
- [Z-algorithm](https://algo-rhythm.dev/en/string-matching/z-algorithm/): Z[i] = longest substring starting at i that matches the prefix. Concatenate P + "#" + T and every position where Z[i] = |P| is a match.
- [Matrix](https://algo-rhythm.dev/en/matrix/): The matrix is a 2-D coordinate space; the trick is finding the right walk order. Family of 3 sub-patterns covering rotation, perimeter traversal, and sorted-grid search.
- [Rotate in place](https://algo-rhythm.dev/en/matrix/rotate/): 90° clockwise rotation by composing two passes — transpose (swap m[i][j] with m[j][i] for i<j) then reverse each row. O(N²), no extra grid.
- [Spiral traversal](https://algo-rhythm.dev/en/matrix/spiral/): Walk the perimeter of a shrinking rectangle — four boundaries (top, right, bottom, left) that collapse inward each lap.
- [Staircase search](https://algo-rhythm.dev/en/matrix/staircase-search/): Start from the top-right of a row-and-column-sorted matrix; each comparison rules out a full row or column. O(R + C) instead of O(R·C).
- [Math & Number Theory](https://algo-rhythm.dev/en/math/): Recognize the number-theoretic backbone (divisibility, modular, multiplicative structure) and an O(N) loop collapses to O(log N) or O(N log log N).
- [GCD / LCM](https://algo-rhythm.dev/en/math/gcd-lcm/): Euclidean reduction — gcd(a, b) = gcd(b, a % b). Each step the pair shrinks geometrically. LCM via a·b / gcd.
- [Modular power](https://algo-rhythm.dev/en/math/modular-power/): Compute a^n mod M in O(log n) by halving the exponent; the bit pattern of n picks which squarings contribute.
- [Prime sieve](https://algo-rhythm.dev/en/math/prime-sieve/): Sieve of Eratosthenes. For each prime p, walk its multiples and mark them composite. Each composite is marked O(log log N) times amortized.
- [Minimum Spanning Tree](https://algo-rhythm.dev/en/mst/): Cheapest way to connect every node of a weighted graph — exactly N−1 edges, no cycles, minimum total weight. Two sub-patterns.
- [Kruskal](https://algo-rhythm.dev/en/mst/kruskal/): Sort edges by weight, walk through, accept each edge whose endpoints sit in different components (Union-Find); skip the rest — they'd close a cycle. O(E log E). Edge-centric.
- [Prim](https://algo-rhythm.dev/en/mst/prim/): Grow the tree from a seed node. A min-heap of crossing edges yields the lightest one connecting an unvisited node — absorb it, push its new outgoing edges, repeat. O(E log V). Node-centric.
- [Cache Eviction](https://algo-rhythm.dev/en/cache/): A fixed-capacity store with O(1) get and put. Every eviction policy answers the same question — "which entry do I drop when the cache is full?" — with a different data structure. Two sub-patterns.
- [LRU](https://algo-rhythm.dev/en/cache/lru/): Hash map + doubly-linked list. Every get/put bumps the touched node to the head; eviction drops the tail. Both operations O(1). The most-asked cache-design interview problem (LC 146).
- [LFU](https://algo-rhythm.dev/en/cache/lfu/): Hash map + per-frequency bucket lists + a minFreq pointer. The eviction candidate is always the tail of buckets[minFreq]. O(1) get/put with ties broken by LRU within each frequency bucket (LC 460).
## Locale variants
For Simplified Chinese: prefix any URL above with `/zh/` instead of `/en/`.
For Japanese: prefix with `/ja/` instead of `/en/`.