Algorithm Patterns
Most developers learn algorithms by memorising individual solutions — a fragile approach that collapses when faced with a novel problem.
Strong engineers learn patterns.
A pattern is a reusable problem‑solving framework that transfers across many seemingly different problems. It captures the underlying structure, the key constraints, and the mechanical steps that lead to a correct and efficient solution.
Algorithm patterns help engineers:
- Recognise problem structures before writing code
- Select suitable approaches with confidence
- Reduce cognitive load by reusing known templates
- Build scalable solutions from proven building blocks
- Solve unfamiliar problems efficiently under time pressure
Mastering patterns is one of the highest‑leverage investments you can make in your algorithm education. It transforms algorithms from a list of disconnected puzzles into an organised engineering toolkit.
Why Patterns Matter
Without patterns:
- Every problem appears unique and demands a fresh approach
- Learning becomes memorisation, which does not scale
- Interview preparation consumes vast time with limited transfer
- Production problems seem disconnected from any structured reasoning
With patterns:
- Similar problems become instantly recognisable by their structural signature
- Solutions become predictable — the right template emerges from the problem’s shape
- Learning accelerates dramatically because new knowledge connects to an existing framework
- You build a durable mental library that grows in value with every new challenge
Pattern‑based learning moves you from solving problems to understanding problem spaces.
Pattern Learning Roadmap
Progress through the patterns in a deliberate order that builds from simple array manipulation to advanced state‑space exploration.
Two Pointers
↓
Sliding Window
↓
Binary Search
↓
Depth-First Search (DFS)
↓
Breadth-First Search (BFS)
↓
Backtracking
↓
Greedy Algorithms
↓
Dynamic Programming
↓
Graph Patterns
↓
Advanced State Space Patterns
Two Pointers
What it teaches: Moving two indices through sorted or structured data to achieve linear‑time solutions for pairing, partitioning, and detection tasks.
Typical use cases: Pair sums in sorted arrays, linked list cycle detection, in‑place array rearrangement, removing duplicates.
Sliding Window
What it teaches: Maintaining a dynamic subarray or substring while expanding and contracting its bounds to optimise aggregate computations.
Typical use cases: Subarray with given sum, longest substring without repeating characters, rate limiting logic, stream analytics.
Binary Search
What it teaches: Halving the search space by exploiting monotonicity; applying the pattern not only to sorted arrays but also to answer‑space search in optimisation problems.
Typical use cases: Searching in sorted arrays, finding minimal feasible solution, capacity planning, index lookups in B‑trees.
Depth-First Search (DFS)
What it teaches: Exhaustive depth‑oriented exploration of trees and graphs using recursion or an explicit stack. Foundation for backtracking and many graph algorithms.
Typical use cases: Tree traversals, cycle detection, dependency resolution, topological sorting, puzzle solving.
Breadth-First Search (BFS)
What it teaches: Level‑order exploration that finds shortest unweighted paths and processes nodes layer by layer.
Typical use cases: Shortest path in unweighted graphs, social network distance, web crawling, influence propagation.
Backtracking
What it teaches: Systematically exploring a combinatorial search space and abandoning partial solutions that cannot satisfy constraints.
Typical use cases: N‑Queens, Sudoku solving, generating subsets and permutations, configuration generation.
Greedy Algorithms
What it teaches: Making locally optimal choices that lead to a global optimum when the problem exhibits optimal substructure and the greedy‑choice property.
Typical use cases: Activity selection, Huffman coding, minimum spanning tree (Kruskal’s), interval scheduling.
Dynamic Programming
What it teaches: Decomposing a problem into overlapping subproblems, defining state and transition, and solving via memoisation or tabulation to avoid redundant computation.
Typical use cases: Knapsack, longest common subsequence, edit distance, resource allocation, cache replacement strategies.
Graph Patterns
What it teaches: Techniques for connectivity, path finding, component identification, and flow analysis that extend DFS/BFS with specialised algorithms (Dijkstra, Bellman‑Ford, Union‑Find, topological sort).
Typical use cases: Network routing, dependency graphs, social graph analysis, circuit design, recommendation modelling.
Advanced State Space Patterns
What it teaches: Combining multiple patterns to search and optimise over complex, multi‑dimensional state spaces — typical in distributed algorithms and AI planning.
Typical use cases: Game playing, constraint optimisation, AI agent planning, configuration tuning.
Core Pattern Categories
Two Pointers
Topics: Opposite‑direction pointers, same‑direction (fast/slow) pointers, partitioning, three‑way partitioning.
Typical problems: Arrays, linked lists, sorted data, in‑place transformation.
Sliding Window
Topics: Fixed‑size window, dynamic window expansion/contraction, frequency tracking with hash maps, subarray optimisation.
Typical problems: String processing, stream aggregation, telemetry windows, subarray constraints.
Binary Search
Topics: Classic index search, search space reduction on monotonic functions, boundary search (first/last occurrence), answer search (min‑max problems).
Typical problems: Sorted arrays, rotated arrays, capacity planning, optimisation with feasibility checks.
Depth-First Search (DFS)
Topics: Pre‑order/in‑order/post‑order tree traversal, recursive graph exploration, cycle detection via recursion stack, backtracking foundation.
Typical problems: Tree serialisation, dependency resolution, connected components, maze exploration.
Breadth-First Search (BFS)
Topics: Level‑order traversal, queue‑based exploration, shortest path in uniform‑weight graphs, multi‑source BFS.
Typical problems: Word ladder, minimum steps problems, web crawling, influence diffusion.
Backtracking
Topics: Combinatorial search tree, state building and reversal, constraint propagation, pruning heuristics.
Typical problems: Subset generation, permutation generation, Sudoku, N‑Queens, crossword construction.
Greedy Algorithms
Topics: Greedy‑choice property identification, sorting‑based selection, activity scheduling, Huffman encoding, fractional knapsack.
Typical problems: Interval scheduling, task assignment, coin change (canonical systems), data compression.
Dynamic Programming
Topics: State design (dimensions and meaning), top‑down memoisation, bottom‑up tabulation, space optimisation, transition formulation.
Typical problems: Sequence alignment, knapsack variants, longest increasing subsequence, matrix chain multiplication, stock trading with constraints.
Graph Patterns
Topics: Union‑Find (disjoint set), Dijkstra, Bellman‑Ford, Floyd‑Warshall, topological sort, minimum spanning trees (Prim/Kruskal), max flow.
Typical problems: Network delay, path with constraints, cycle detection in directed graphs, bipartiteness, strongly connected components.
Pattern Recognition Framework
Every algorithm problem should be approached systematically. Apply this framework before writing any code:
Step 1: Identify Inputs
- Is the data an array, string, tree, graph, or matrix?
- Are elements sorted, unique, weighted?
- Are there cycles, directed edges, or implicit connections?
Step 2: Identify Constraints
- Time and space limitations (e.g., O(n) required for large inputs).
- Special properties: sorted, monotonic, contiguous, hierarchical.
- Presence of optimal substructure or overlapping subproblems.
Step 3: Match Candidate Patterns
- Sorted array + target → Binary Search
- Subarray/substring with constraints → Sliding Window
- Tree or graph exploration → DFS / BFS
- Combinatorial generation / constraint satisfaction → Backtracking
- Optimisation with optimal substructure → Dynamic Programming or Greedy
- Shortest path / connectivity → Graph Patterns
Step 4: Analyse Complexity
- Derive expected time and space complexity for each candidate pattern.
- Consider hidden constants and practical input sizes.
- Choose the pattern that satisfies constraints while remaining implementable.
Most Important Patterns for Engineers
Based on frequency in technical interviews and production design, these ten patterns carry the highest return on effort.
- Hash Table Pattern — O(1) lookup and aggregation; powers caching, indexing, and de‑duplication.
- Two Pointers Pattern — Linear traversal for sorted data; essential for in‑place array manipulation and linked list cycles.
- Sliding Window Pattern — Optimising subarray/substring operations; used in network flow control and telemetry.
- Binary Search Pattern — Logarithmic reduction; foundational for index structures and capacity provisioning.
- DFS Pattern — Systematic tree and graph exploration; underpins dependency resolution and serialisation.
- BFS Pattern — Shortest unweighted paths; core to social graph distance, crawl scheduling, and state‑space search.
- Greedy Pattern — Locally optimal choice; appears in scheduling, compression, and resource allocation.
- Dynamic Programming Pattern — State‑based optimisation; used in edit distance, resource allocation, and cache replacement.
- Graph Traversal Pattern — Extended DFS/BFS with Dijkstra, Union‑Find, topological sort; models real‑world networks.
- State Space Search Pattern — Search over combinatorial or configuration spaces; powers game AI, planning, and optimisation.
Patterns in Real Systems
Algorithm patterns are not confined to interview whiteboards — they form the operational backbone of production systems.
Databases
- B‑trees rely on binary search‑like page navigation and divide and conquer for range queries.
- Query optimisers use greedy algorithms for join order selection and dynamic programming for cost‑based planning.
Search Engines
- Crawling follows a BFS pattern to discover and prioritise pages.
- PageRank uses graph analysis and iterative state propagation resembling dynamic programming.
- Inverted index lookups combine hash table access with merge patterns similar to two‑pointers.
Distributed Systems
- Consensus protocols (Raft, Paxos) implement state machine replication with carefully designed transition logic, analogous to finite‑state DP.
- Consistent hashing uses a binary search structure (ordered ring with logarithmic lookup).
- Partitioning and leader election follow graph connectivity and voting patterns.
Recommendation Systems
- Collaborative filtering uses matrix factorisation techniques built on optimisation patterns.
- Item‑to‑item similarity relies on nearest neighbour search, often implemented with approximate graph algorithms.
- Real‑time candidate generation applies greedy filtering under latency constraints.
AI Systems
- Vector search engines (e.g., FAISS, Annoy) use graph‑based ANN algorithms like HNSW — a multi‑layer BFS/DFS traversal structure.
- Retrieval‑Augmented Generation (RAG) pipelines chain hash‑based lookups with sliding window context assembly.
- AI agent planning is fundamentally a state space search problem, often solved with BFS/DFS variants or heuristic‑guided search.
Recognising these connections turns algorithm pattern knowledge into an engineering superpower — you see the abstraction, and immediately understand how it performs in production.
Recommended Reading Order
For engineers new to patterns, follow this sequence to build breadth and depth efficiently.
- Hash Table Pattern — Because it is ubiquitous, simple, and the basis for many other patterns.
- Two Pointers Pattern — Introduces linear traversal optimisations with minimal complexity overhead.
- Sliding Window Pattern — Extends two‑pointers into dynamic subarray optimisation.
- Binary Search Pattern — Teaches logarithmic thinking and search space reduction.
- DFS Pattern — Opens up tree and graph exploration recursively.
- BFS Pattern — Complements DFS with level‑order and shortest‑path logic.
- Greedy Pattern — Introduces local optimisation and the importance of problem properties.
- Dynamic Programming Pattern — The most powerful and demanding pattern; builds on recursion and decomposition.
- Graph Pattern — Unifies DFS, BFS, and specialised algorithms for networks.
- Advanced Search Patterns — Covers A*, state space search, and combinatorial optimisation.
Common Mistakes
- Memorising solutions instead of patterns — You accumulate fragile, disconnected knowledge that fails under variation.
- Ignoring constraints — Constraints are the strongest signal for pattern selection; overlooking them leads to suboptimal or incorrect designs.
- Choosing patterns too early — Jumping to code before analysing the problem structure often results in shoehorning a familiar pattern onto an incompatible problem.
- Using dynamic programming unnecessarily — Not every optimisation problem requires DP; greedy or simple iteration may suffice with far less complexity.
- Optimising before understanding the problem — Premature pattern application masks the real structure; always map the problem before selecting the tool.
- Neglecting pattern variants — Many patterns have subtle variations (e.g., fast/slow pointers within two‑pointers); missing these limits your pattern recognition range.
From Patterns to Engineering Thinking
Mastering algorithm patterns is a stepping stone to broader engineering competency.
- Problem Solving → Patterns provide a vocabulary to decompose any computational challenge.
- System Design → The same patterns govern caching strategies, index design, and data partitioning.
- Performance Engineering → Pattern analysis naturally extends to latency budgets, throughput estimation, and resource planning.
- Architecture Decision Making → Recognising algorithmic trade‑offs (consistency vs. latency, compute vs. storage) becomes second nature.
Pattern recognition is not just an interview skill — it is the foundation of systematic engineering thinking that separates senior engineers from the rest.
What Comes Next
After internalising algorithm patterns, continue your learning with these deeper topics:
- Dynamic Programming — Refine your ability to define state and transition, and tackle optimisation problems that resist greedy approaches.
- Graph Algorithms — Extend pattern thinking to connectivity, paths, and flows in complex networks.
- System Thinking — Apply pattern intuition to real‑world constraints: caches, disks, network, and concurrency.
- Distributed Algorithms — Scale patterns across multiple machines, studying consensus, replication, and partitioning.
- AI System Algorithms — Explore how patterns power vector search, retrieval pipelines, and agent architectures.
Key Principle
Algorithms are not collections of solutions.
They are collections of patterns.
The engineer who masters patterns can solve problems they have never seen before — in interviews, in production, and in the systems they design.