Algorithm patterns are reusable approaches to solving specific types of problems. Recognizing these patterns can help you quickly identify and solve similar problems.
This section covers the following algorithm patterns:
- Two Pointers - Using two pointers to traverse data
- Sliding Window - Efficiently processing contiguous subarrays
- Fast & Slow Pointers - Detecting cycles and finding middle elements
- Merge Intervals - Dealing with overlapping intervals
- Cyclic Sort - Efficiently sorting numbers in a given range
- In-place Reversal of Linked List - Reversing linked lists without extra space
- Tree BFS/DFS - Breadth-first and depth-first tree traversals
- Topological Sort - Ordering tasks with dependencies
- Binary Search - Efficiently searching sorted arrays
- Subsets - Generating all possible subsets, permutations, and combinations
- Modified Binary Search - Variations of binary search
- Top K Elements - Finding the top/smallest K elements
- K-way Merge - Merging K sorted arrays
- Greedy Algorithms - Making locally optimal choices
- Backtracking - Building solutions incrementally and undoing choices
| If the problem involves... | Consider using... |
|---|---|
| Sorted array, find a pair with target sum | Two Pointers |
| Find subarrays with a given condition | Sliding Window |
| Linked list with cycle or finding middle | Fast & Slow Pointers |
| Overlapping intervals | Merge Intervals |
| Array with numbers in range [1...n] | Cyclic Sort |
| Reversing a linked list | In-place Reversal |
| Level-by-level tree processing | Tree BFS |
| Exploring all paths in a tree | Tree DFS |
| Tasks with dependencies | Topological Sort |
| Sorted array, efficient search | Binary Search |
| Generate all combinations/permutations | Subsets |
| Find a specific element in a sorted array | Modified Binary Search |
| Find the K largest/smallest elements | Top K Elements |
| Merge K sorted arrays/lists | K-way Merge |
| Optimization problems with local choices | Greedy Algorithms |
| Finding all possible solutions | Backtracking |
-
Analyze the input data structure:
- Is it an array, linked list, tree, or graph?
- Is the data sorted or unsorted?
-
Understand the problem constraints:
- Are there specific time/space complexity requirements?
- Is in-place modification required?
-
Look for key phrases:
- "Find a pair/triplet" → Two Pointers
- "Subarray/substring with condition" → Sliding Window
- "Detect cycle" → Fast & Slow Pointers
- "Merge overlapping intervals" → Merge Intervals
- "Numbers in range 1 to n" → Cyclic Sort
- "Level order traversal" → Tree BFS
- "All paths from root to leaf" → Tree DFS
- "Task scheduling with dependencies" → Topological Sort
- "Efficiently search in sorted array" → Binary Search
- "All subsets/combinations/permutations" → Subsets
- "K largest/smallest elements" → Top K Elements
- "Locally optimal choices" → Greedy Algorithms
- "All possible solutions" or "Generate all valid combinations" → Backtracking
-
Consider the problem's objective:
- Finding elements vs. transforming data
- Counting vs. optimizing
- Checking existence vs. finding all instances