Problem-solving patterns
Learn the signal, invariant and edge case behind each technique.
Linear Scan
Inspect each item once in input order.
Frequency Counting
Convert repeated values into counts indexed by key.
Complement Lookup
Use an equation to look up the missing partner.
Prefix Sums
Store the total before each boundary to answer range sums.
Running Optimum
Keep the best candidate ending here and the best seen globally.
Growing Sorted Region
Keep a known region sorted and extend its boundary one item or pass at a time.
Divide and Conquer
Split, solve smaller disjoint instances and combine their results.
Opposing Pointers
Eliminate pairs by moving one endpoint inward.
Read and Write Cursors
Read the input once while writing a valid output prefix in place.
Fixed-Width Window
Update the summary of exactly k consecutive elements.
Variable-Width Window
Expand and shrink a span while maintaining a validity rule.
Exact Binary Search
Halve a sorted candidate interval after each unsuccessful probe.
Boundary Search
Find the first position that satisfies a monotone predicate.
Binary Search on the Answer
Search candidate answer values using a monotone feasibility check.
Pointer Rewiring
Change a chain by preserving the old successor before linking elsewhere.
Fast and Slow Pointers
Move references at different rates through one chain.
Matching Stack
Use the latest unfinished opener to interpret the next closer.
Monotonic Stack
Keep unresolved indices in monotone value order.
Recursive Reduction
Define a base case and reduce the measure on each call.
Choose, Explore, Restore
Build one path at a time and undo its last choice before visiting a sibling.
Tree Traversal
Visit a node before, between, or after its child subtrees.
Priority Heap
Keep the next highest-priority item at the root.
Breadth-First Search
Use a queue to explore a graph one distance layer at a time.
Depth-First Search
Follow one branch before returning for alternatives.
Topological Ordering
Repeatedly schedule a vertex whose remaining prerequisites are complete.
Dynamic Programming
Cache a well-defined state and combine smaller subproblems.
Grid Dynamic Programming
Build a table by combining valid predecessor cells.
Greedy Choice
Commit to a locally best action backed by a proof that an optimum remains reachable.
Interval Merge
Sort by starts and maintain the union of intervals processed so far.
XOR Cancellation
Pair equal values so their contributions cancel under XOR.
Bit Masking
Select specific binary positions with shifts and a mask.