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

Look For

Need fast lookup · Count occurrences · Find duplicates · Check membership

Summary

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

Look For

Sorted array · Find pairs · Container problems · Reverse strings

Summary

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

Look For

Substring or subarray · Contiguous elements · Length constraints

Summary

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.

05 — STACK

Look For

Matching pairs · Brackets or parentheses · LIFO order · Undo operations

Common Problems
Summary

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

Look For

FIFO order · Level-order traversal · Breadth-first search · Task scheduling

Summary

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

Look For

Sequential access · Insertion at head · Reversal needed · Cycle detection

Summary

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

Look For

Tree or graph · All paths · Topological sort · Backtracking

Common Problems
Summary

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

Look For

Shortest path · Level-order · Nearest neighbor · Graph traversal

Summary

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

Look For

Top K elements · Median finding · Task scheduling · Dijkstra's algorithm

Summary

Efficiently retrieve min or max element in O(log n). Use for problems needing sorted access or maintaining a stream of top/bottom elements.