1. Sliding Window
Use when: finding subarrays/substrings that satisfy a condition. Fixed window: maximum sum of k consecutive elements. Variable window: smallest subarray with sum ≥ target. Template: expand right pointer, shrink left when condition met. Key problems: Longest Substring Without Repeating Characters, Minimum Window Substring, Maximum Sum Subarray of Size K. This pattern turns O(n²) brute force into O(n) by avoiding redundant computation.
2. Two Pointers
Use when: searching pairs in sorted arrays, or comparing elements from both ends. Opposite direction: Two Sum (sorted), Container With Most Water, Valid Palindrome. Same direction: Remove Duplicates, Merge Sorted Arrays. Fast/slow: Linked List Cycle, Find Middle of List. Two pointers work because sorted order lets you eliminate possibilities — if sum is too large, move the right pointer left.
3. BFS & DFS
BFS (Breadth-First Search): Use for shortest path in unweighted graphs, level-order traversal, and multi-source problems (rotting oranges). Always uses a queue. DFS (Depth-First Search): Use for path finding, connected components, and exhaustive search. Uses recursion or stack. Key problems: Number of Islands, Word Search, Course Schedule. BFS finds shortest paths; DFS explores all possibilities.
4. Binary Search
Beyond sorted arrays: use when the search space has a monotonic property. Classic: find target in sorted array. Advanced: find minimum in rotated sorted array, search in 2D matrix. Search space: Koko Eating Bananas, Split Array Largest Sum.
4. Binary Search
Beyond sorted arrays: use when the search space has a monotonic property. Classic: find target in sorted array. Advanced: find minimum in rotated sorted array, search in 2D matrix. Search space: Koko Eating Bananas, Split Array Largest Sum. Template: left = 0, right = max, while left < right, check mid. The key insight: if you can frame the problem as "is X possible?" with a monotonic answer, binary search works.
The key insight: if you can frame the problem as "is X possible?" with a monotonic answer, binary search works.5. Dynamic Programming
Use when: overlapping subproblems + optimal substructure. Identify the state (what changes between subproblems) and the transition (how states relate). 1D: Climbing Stairs, House Robber, Coin Change. 2D: Longest Common Subsequence, Edit Distance, Grid Paths. Start with recursion + memoization, then convert to tabulation. See our DSA roadmap for a structured DP learning plan.
6. Backtracking
Use when: generating all valid combinations, permutations, or subsets. Template: choose, explore, unchoose. Key problems: Permutations, Combinations, N-Queens, Sudoku Solver, Word Search. Pruning is critical — skip branches early that can't lead to valid solutions. Backtracking is DFS with the ability to undo choices. Time complexity is often exponential, but pruning makes it practical.
7. Heap / Priority Queue
Use when: finding top K elements, merging K sorted lists, or streaming median. Min-heap: Kth Largest Element, Merge K Sorted Lists. Max-heap: Task Scheduler, Reorganize String. Two heaps: Find Median from Data Stream. Heaps provide O(log n) insertion and O(1) access to min/max. They're underused by candidates — many medium problems have elegant heap solutions.
8-15. More Essential Patterns
8. Monotonic Stack: Next Greater Element, Daily Temperatures, Largest Rectangle in Histogram. 9. Union-Find: Connected components, redundant connections. 10. Trie: Autocomplete, word search in grid. 11. Intervals: Merge Intervals, Insert Interval, Meeting Rooms. 12. Prefix Sum: Subarray Sum Equals K, Range Sum Query. 13. Topological Sort: Course Schedule, Alien Dictionary. 14. Bit Manipulation: Single Number, Counting Bits. 15. Greedy: Jump Game, Gas Station, Activity Selection.
Master these 15 patterns and you'll have the tools to solve virtually any coding interview problem. Practice 5-8 problems per pattern, review weekly, and use structured learning to stay consistent.