Notes lock ↩
A working notebook. Algorithm problems I've solved and system design case studies I've sketched.
Backtracking — Templates, Variants & Problems
The universal backtracking template and six problem families: subsets, combinations, permutations, parentheses, partitions, and N-Queens. Duplicate pruning is the key invariant.
Binary Search — Templates, Variants & Problems
The four binary search templates: exact match, leftmost index, rightmost index, and predicate/answer-space search. Covers rotated array, peak element, and binary search on the answer.
Bit Manipulation — Templates & Patterns
Bit manipulation patterns: K-th bit ops, count set bits (Kernighan), DP bit count, XOR for single number, bitmap uniqueness, sum without +, and power checks.
Design Data Structures — Templates & Patterns
Design patterns for common interview data structures: MinStack, MaxStack with lazy deletion, HitCounter with circular array, LRU Cache with OrderedDict, and Insert-Delete-GetRandom O(1).
Fenwick Tree (BIT) — Prefix Sums with Point Updates
Binary Indexed Tree: prefix sum query and point update both in O(log n). Uses the lowest set bit of an index to navigate a compact implicit tree.
Graph Algorithms — Templates & Patterns
BFS/DFS templates, Dijkstra, topological sort (DFS + Kahn's), cycle detection, bipartite check, connected components, grid traversal, word search, and abstract graph BFS.
Greedy & Intervals — Templates & Patterns
Greedy patterns: activity selection, interval scheduling, merge intervals, meeting rooms, and jump game. Sort by end time is the core invariant for interval problems.
Heap Patterns — Templates & Problems
Heap patterns beyond the basics: K-th largest with a size-K min-heap, two-heaps for streaming median, QuickSelect for O(n) K-th element, and merge K sorted lists.
LRU cache — clean implementation in 30 lines
Doubly-linked list + hash map. The classic O(1) get/put implementation.
Monotonic Stack — Templates & Patterns
Monotonic stack patterns: next greater element, previous smaller element, circular arrays, largest rectangle in histogram, and trapping rain water.
Monotonic stack — the pattern that unlocks 'next greater element' problems
Notes on the monotonic-stack pattern, the family of problems it solves, and the template I use.
Random Pick with Weight — prefix sum + binary search
Build a prefix-sum array from the weights. A uniform random float in [0, total) lands in exactly one bucket. Find it with bisect_left in O(log n).
Sliding Window — Templates & Patterns
Sliding window patterns: fixed-size window, variable-size two-pointer, prefix sum for subarray sum equals K, and monotonic deque for range maximum/minimum.
String Algorithms — Templates & Patterns
String algorithm patterns: Rabin-Karp rolling hash for pattern matching, KMP failure function, anagram detection with frequency arrays, and palindrome techniques.
Subsets — Backtracking with duplicates
Sort first, then skip duplicate values at the same recursion depth. The guard is index > start, not index > 0 — getting that wrong is the most common bug.
Tree Algorithms — Templates, Traversals & Patterns
Comprehensive tree reference: DFS/BFS traversals, iterative patterns, level-order tricks, LCA, BST validation, path sum, vertical order, and serialize/deserialize.
Trie — Prefix tree
Insert/search/prefix-check all in O(L) where L is word length. Beats a hash set whenever you need prefix queries or shared-prefix compression.
Trie — Template & Patterns
Trie (prefix tree) template: insert, search with wildcards, prefix sum aggregation, and word search patterns.
Two Pointers — Templates & Patterns
Two-pointer patterns: opposite ends (two sum sorted, container with water), same direction (remove duplicates, three sum), and fast/slow pointers (cycle detection, middle of list).
Union-Find — Disjoint Set Union (DSU)
Near O(1) per op with path compression + union by rank. Three variants: standard class, 2D grid (flat ID mapping), and earliest-all-connected detection.
Dynamic Programming — Templates & Patterns
Core DP patterns: 1-D sequences (climb stairs, decode ways, LIS), 2-D grids (min path, triangle), strings (LCS, edit distance), knapsack/subset sum, and buy-sell stocks.
Two Heaps — sliding median and k-th largest
Keep a max-heap for the lower half and a min-heap for the upper half. The median lives at one of the two tops. O(log n) per insertion.
Complexity Reference — All Algorithms at a Glance
One-page cheat sheet of time and space complexity for every algorithm and data structure across all notes: sorting, searching, graphs, trees, dynamic programming, and Python built-ins.
Python Best Practices — Competitive / Interview Cheatsheet
Comprehensive snippets covering custom sort keys, class label sorting, heapq, deque, defaultdict, Counter, SortedDict, dataclass, walrus operator, division gotchas, and backtracking duplicate pruning.