Learn and practice
Patterns
Learn to recognize a reusable approach, state its invariant, and understand when it applies. Each guide includes worked code and counterexamples.
Sliding window: fixed and variable windows
Derive a window invariant, update state in constant time, and recognize when negative values or a different condition break the approach.
PatternsTwo pointers: sorted pairs, partitions, and in-place work
Understand why moving a pointer is safe. Work through sorted Two Sum, palindrome checks, and duplicate removal.
PatternsDynamic programming: states, transitions, and evaluation order
Build a recurrence from choices, define a complete state, and compare memoization with tabulation through a worked non-adjacent-sum problem.
PatternsPrefix sums: range queries and signed subarray counts
Build prefix arrays and frequency maps, count target-sum subarrays with negative values, and test boundary and numeric assumptions.
PatternsBinary search: lower bounds, upper bounds, and first true
Use a half-open boundary invariant to handle duplicates, missing answers, monotone predicates, and binary-search edge cases.
PatternsBacktracking: subsets, permutations, and pruning
Model a decision tree, restore shared state, copy independent results, and justify pruning with worked subset and permutation code.
PatternsMonotonic stacks: next greater values and stock spans
Trace unresolved indexes, choose strict or non-strict comparisons, and prove linear total work with tested next-greater and span implementations.
PatternsHeaps and priority queues: keep the K largest stream values
Implement a numeric min-heap, choose the right top-K orientation, and separate heap order, sorted output, and update costs.
PatternsTries for prefix search: insert, count, and delete words
Build an iterative Unicode-aware trie with prefix counts, duplicate handling, safe deletion, explicit text contracts, and operation-cost analysis.
PatternsUnion-find: weighted components, path halving, and connectivity
Implement disjoint sets with weighted unions, trace component counts, explain amortized costs, and distinguish connectivity from paths and deletions.
PatternsGreedy algorithms: interval scheduling, exchange proofs, and counterexamples
Prove earliest-finish interval scheduling, implement a nonmutating selector, reject tempting rules with counterexamples, and test against exhaustive subsets.