Algorithm Patterns
10 core patterns for recognizing and solving interview problems
Algorithm Patterns
Pattern recognition is how experts solve problems fast. Instead of inventing a new approach for every problem, they recognize the structure and apply a known technique.
Here are 10 essential patterns. Each includes what to look for, where it appears, and when to reach for it.
01 — HASH MAP / SET
Need fast lookup · Count occurrences · Find duplicates · Check membership
Use a hash map or set to achieve O(1) average lookup, insertion, and deletion. Essential for problems requiring fast membership testing or counting distinct elements.
02 — TWO POINTERS
Sorted array · Find pairs · Container problems · Reverse strings
Move two pointers from opposite ends or at different speeds to solve problems in O(n) time without extra space. Perfect for sorted arrays and problems requiring pair comparisons.
03 — SLIDING WINDOW
Substring or subarray · Contiguous elements · Length constraints
Maintain a window of elements that expands and contracts. Converts nested loop O(n²) problems into O(n) by reusing computations from the previous window.
04 — BINARY SEARCH
Sorted data · Find target · Minimize or maximize · Search space halving
Divide search space in half each iteration to achieve O(log n). Works on sorted arrays and problem domains where you can eliminate half the remaining options at each step.
05 — STACK
Matching pairs · Brackets or parentheses · LIFO order · Undo operations
Last-in-first-out data structure. Use for problems requiring matching (brackets, tags), function call stacks, or operations where recent items matter most.
06 — QUEUE
FIFO order · Level-order traversal · Breadth-first search · Task scheduling
First-in-first-out data structure. Essential for BFS algorithms and any problem where you process elements in the order they were added.
07 — LINKED LIST
Sequential access · Insertion at head · Reversal needed · Cycle detection
Nodes containing data and pointers. Better than arrays for frequent insertions/deletions at known positions. Enable in-place operations without shifting elements.
08 — DEPTH-FIRST SEARCH
Tree or graph · All paths · Topological sort · Backtracking
Explore deeply before backtracking. Use recursion or an explicit stack. Essential for finding all paths, detecting cycles, and problems requiring exhaustive search.
09 — BREADTH-FIRST SEARCH
Shortest path · Level-order · Nearest neighbor · Graph traversal
Explore level by level using a queue. Finds shortest paths in unweighted graphs and processes nodes closest to the start first.
10 — HEAP / PRIORITY QUEUE
Top K elements · Median finding · Task scheduling · Dijkstra's algorithm
Efficiently retrieve min or max element in O(log n). Use for problems needing sorted access or maintaining a stream of top/bottom elements.