14 Coding Patterns That Solve 80% of Interview Problems
Instead of memorising 500 LeetCode solutions, learn 14 patterns. Recognise the pattern and you can derive the solution from first principles.
The best way to prepare for coding interviews isn't to solve 500 LeetCode problems. It's to recognise the 14 patterns that most problems reduce to.
The 14 Patterns
| # | Pattern | Example Problems |
|---|---|---|
| 1 | Two Pointers | Two Sum (sorted), Container With Most Water |
| 2 | Sliding Window | Longest Substring Without Repeating Characters |
| 3 | Fast & Slow Pointers | Linked List Cycle, Find Middle of List |
| 4 | Merge Intervals | Merge Intervals, Insert Interval |
| 5 | Cyclic Sort | Find All Missing Numbers, Find Duplicate Number |
| 6 | In-Place Linked List Reversal | Reverse Linked List, Reverse Sub-list |
| 7 | Tree BFS | Binary Tree Level Order Traversal |
| 8 | Tree DFS | Path Sum, All Root-to-Leaf Paths |
| 9 | Two Heaps | Find Median from Data Stream |
| 10 | Subsets (Backtracking) | Subsets, Permutations, Combinations |
| 11 | Modified Binary Search | Search in Rotated Array, Find Min in Rotated Array |
| 12 | Top-K Elements | K Largest Elements, K Most Frequent |
| 13 | K-Way Merge | Merge K Sorted Lists |
| 14 | Dynamic Programming | Coin Change, Longest Common Subsequence |
How to identify patterns
Sliding Window β "subarray/substring with constraint" + contiguous elements
Two Pointers β sorted array + target sum, or palindrome/reverse check
Tree DFS β process individual node, return something to parent (path problems)
Tree BFS β level-by-level, find something at a specific depth
Top-K β K largest/smallest, and sort is too slow β min-heap of size K
DP β count ways / optimal value + overlapping subproblems
Example: how to read a problem
"Find the length of the longest substring without repeating characters"
- "substring" β contiguous β sliding window
- "without repeating" β constraint on window contents
- Template: expand right pointer, shrink left pointer when constraint violated
Once you see the pattern, the code writes itself.
Related Posts
The Sliding Window Pattern: One Template, Many Problems
Turn O(nΒ²) substring and subarray problems into O(n). The fixed and variable sliding window templates in Java, with six worked interview problems.
Big O Notation: The Developer's Practical Guide
Big O is how you talk about algorithm efficiency in interviews. Here's what each notation means and how to analyse code on the spot.