The 16 coding-interview patterns — runnable, tested, and visualized.
Every key problem from the classic "Grokking the Coding Interview" pattern catalog, implemented in clean JavaScript with self-tests, plus an interactive study guide and an animated algorithm visualizer. Learn to recognize the pattern from the problem statement, then reuse a template you already know cold.
🔗 Live demos: Interactive Study Guide · Algorithm Visualizer — no install needed.
Most interview problems are variations of a small set of underlying techniques. Instead of grinding hundreds of random problems, learn the 16 patterns, the triggers that identify them in a problem statement, and one solid template each — then every new problem becomes "which pattern is this?"
patterns/01..16-*.js |
One file per pattern: clean solutions to its key problems, each with self-tests at the bottom. 147 tests, all passing. |
study-guide.html |
Interactive single-file study guide — open it in a browser. Search, difficulty filter, dark mode, per-pattern progress tracking, an animated "how it works" panel per pattern, and expandable drill rows that reveal the exact tested solution code. |
visualizer/ |
Step-through algorithm animator for all 16 patterns — open visualizer/index.html, pick a pattern, press Play or use arrow keys. Light and dark mode. |
lib/structures.js |
Shared building blocks: ListNode, TreeNode, MinHeap/MaxHeap, and tiny test helpers. |
tools/build-guide.js |
Regenerates the study guide's drill panels from patterns/*.js (idempotent). |
git clone https://github.com/abdullasulaiman/swe-interview-playbook.git
cd swe-interview-playbook
node run-all.js # run every pattern's self-tests
node patterns/01-sliding-window.js # run a single pattern's tests
open study-guide.html # interactive study guide
open visualizer/index.html # animated visualizerNo dependencies — plain Node.js (any recent version) and a browser.
Every solution is exported, so you can require and experiment:
const { longestSubstringKDistinct } = require('./patterns/01-sliding-window');
longestSubstringKDistinct('araaci', 2); // 4| # | Pattern | Recognize it when the prompt says… | Core idea | Time |
|---|---|---|---|---|
| 01 | Sliding Window | contiguous subarray/substring, "size k", longest/shortest, "at most K distinct" | grow right edge, shrink left on violation | O(N) |
| 02 | Two Pointers | sorted; pair/triplet/quad sum; remove/dedupe in place | converge from both ends | O(N)–O(N³) |
| 03 | Fast & Slow Pointers | linked list cycle, find middle, palindrome list, happy number | 1× vs 2× speed meet in a cycle | O(N) |
| 04 | Merge Intervals | intervals, overlap, meetings/rooms/appointments | sort by start, sweep once | O(N log N) |
| 05 | Cyclic Sort | numbers in range [1..n], find missing/duplicate, "O(n) no extra space" | swap each value to its home index | O(n) |
| 06 | In-place LinkedList Reversal | reverse list/sub-list in place, groups of k, rotate | three-pointer re-linking | O(N) |
| 07 | Tree BFS | level by level, level order, min depth, right view, connect siblings | queue + freeze levelSize |
O(N) |
| 08 | Tree DFS | root-to-leaf path, path sum, count paths, diameter, max path sum | recurse + carry state / backtrack | O(N) |
| 09 | Two Heaps | median of stream/window, partition into small/large halves | max-heap low half, min-heap high half | O(log N) insert |
| 10 | Subsets | all subsets/permutations/combinations, generate parentheses | extend every existing partial result | O(N·2ᴺ) |
| 11 | Modified Binary Search | sorted + find element/ceiling/next, rotated sorted, "O(log n)" | halve the search space | O(log N) |
| 12 | Bitwise XOR | "every number appears twice except…", missing number, complement | a^a=0, a^0=a cancels pairs |
O(N) |
| 13 | Top 'K' Elements | "top / smallest / largest / most frequent K", "K closest" | heap of size K | O(N log K) |
| 14 | K-way Merge | "merge K sorted lists", "Kth smallest across M lists", smallest range | min-heap over K list heads | O(N log K) |
| 15 | 0/1 Knapsack (DP) | subset under a capacity/target, equal partition, "can we make sum S" | DP table over (items, capacity) | O(N·C) |
| 16 | Topological Sort | dependencies/prerequisites, "can all finish", build an order, alien dictionary | Kahn's algorithm (emit in-degree 0) | O(V+E) |
- Clarify — restate the problem; ask about size, sorting, ranges, duplicates, edge cases. (This often reveals the pattern.)
- Brute force — name the naive approach and its cost in one sentence.
- Name the pattern — "this is a sliding-window problem because…".
- Dry-run — trace the template on a tiny example before coding.
- Code — type the pattern's skeleton, then fill in the condition.
- Verify & cost — test empty / single / all-duplicates, then state O(time)/O(space).
swe-interview-playbook/
├── README.md
├── run-all.js # runs every pattern's self-tests
├── study-guide.html # interactive study guide (open in a browser)
├── lib/
│ └── structures.js # ListNode, TreeNode, MinHeap/MaxHeap, test helpers
├── patterns/
│ ├── 01-sliding-window.js
│ └── … through 16-topological-sort.js
├── tools/
│ └── build-guide.js # regenerates study-guide drill panels from patterns/
└── visualizer/ # step-through algorithm animator (all 16 patterns)
Found a cleaner solution or a missing classic problem? PRs welcome — keep the style: one pattern per file, solution + self-test, exported via module.exports.