Common Interview Patterns — Core Concepts
Why patterns matter
Coding interviews test problem-solving, not memorization. But there is a secret: most problems are variations of a small set of patterns. Companies like Google, Meta, and Amazon draw from the same well. Recognizing the pattern transforms a 45-minute struggle into a 15-minute solution.
The core patterns
1. Hash map frequency counter
When to use: Counting occurrences, finding duplicates, checking if two things are anagrams or permutations.
Build a dictionary of counts, then query it. This replaces nested loops (O(n²)) with single passes (O(n)).
Signal words: “frequency,” “count,” “anagram,” “duplicate,” “most common.”
2. Two pointers
When to use: Sorted arrays, pair finding, partitioning, palindrome checking.
Place one pointer at the start and one at the end (or two at the start). Move them toward each other based on a condition. Reduces O(n²) brute force to O(n).
Signal words: “sorted array,” “pair that sums to,” “palindrome,” “in-place.”
3. Sliding window
When to use: Finding subarrays or substrings that meet a condition — longest, shortest, containing certain elements.
Maintain a window defined by left and right boundaries. Expand the right side to include more; shrink the left side when the condition breaks.
Signal words: “subarray,” “substring,” “contiguous,” “maximum length,” “minimum window.”
4. Binary search
When to use: Searching in sorted data, finding boundaries, optimization problems with monotonic conditions.
Not just for “find this number.” Binary search works on any problem where you can answer yes/no and the answers change from yes to no (or vice versa) at some boundary.
Signal words: “sorted,” “minimum that satisfies,” “search space,” “O(log n).“
5. BFS/DFS (graph traversal)
When to use: Trees, graphs, grids, anything with connected components, shortest path, traversal order.
BFS (queue) finds shortest paths in unweighted graphs. DFS (stack/recursion) is better for exhaustive search, path finding, and tree problems.
Signal words: “connected,” “shortest path,” “level order,” “island,” “grid.”
6. Stack
When to use: Matching brackets, nearest greater element, expression evaluation, undo operations.
Process elements left to right, pushing onto a stack and popping when a condition is met. The stack maintains context about “what came before.”
Signal words: “valid parentheses,” “next greater,” “monotonic,” “nested.”
7. Dynamic programming
When to use: Optimization problems with overlapping subproblems and optimal substructure.
Break the problem into subproblems. Store results to avoid recomputation. Start by defining the recurrence relation, then decide between top-down (memoization) or bottom-up (tabulation).
Signal words: “minimum cost,” “number of ways,” “maximum profit,” “can you reach.”
8. Backtracking
When to use: Generating all valid combinations, permutations, or configurations that satisfy constraints.
Try a choice, recurse, undo the choice (backtrack), try the next option. Prune branches that cannot lead to valid solutions.
Signal words: “all combinations,” “generate,” “permutations,” “N-Queens,” “Sudoku.”
9. Greedy
When to use: Problems where local optimal choices lead to global optimal — interval scheduling, minimum coins (sometimes), activity selection.
Make the best choice at each step without looking back. Works when you can prove that greedy choice plus optimal solution to the remaining subproblem equals global optimal.
Signal words: “interval,” “schedule,” “minimum number of,” “maximum events.”
10. Heap / Priority Queue
When to use: Finding k-th largest/smallest, merging sorted lists, scheduling by priority.
Use Python’s heapq module. A min-heap gives you the smallest element in O(1) and insertion/removal in O(log n).
Signal words: “k-th largest,” “top k,” “merge k sorted,” “median.”
Pattern recognition strategy
When you see a new problem:
- Read for signal words — they often point directly to a pattern.
- Check the constraints — n ≤ 10⁵ suggests O(n log n) or better. n ≤ 20 suggests exponential/backtracking is fine.
- Ask: is the input sorted? → Two pointers or binary search.
- Ask: does it involve subarrays/substrings? → Sliding window.
- Ask: does it involve a graph or grid? → BFS/DFS.
- Ask: does it ask for all combinations? → Backtracking.
- Ask: does it ask for min/max with choices? → DP or Greedy.
Common misconception
“You need to solve hundreds of LeetCode problems to pass interviews.” In reality, deeply understanding these 10 patterns and solving 3-5 problems per pattern (30-50 total) builds stronger intuition than grinding 500 problems without recognizing the underlying structure. Quality over quantity.
One thing to remember: The hardest part of any interview problem is not writing the code — it is recognizing which pattern to apply. Spend your study time on pattern recognition, not memorizing solutions.
See Also
- Python Leetcode Problem Solving How to get better at coding puzzles without losing your mind — a beginner's survival guide to LeetCode.
- Ci Cd Why big apps can ship updates every day without turning your phone into a glitchy mess — CI/CD is the behind-the-scenes quality gate and delivery truck.
- Containerization Why does software that works on your computer break on everyone else's? Containers fix that — and they're why Netflix can deploy 100 updates a day without the site going down.
- Python 310 New Features Python 3.10 gave programmers a shape-sorting machine, friendlier error messages, and cleaner ways to say 'this or that' in type hints.
- Python 311 New Features Python 3.11 made everything faster, error messages smarter, and let you catch several mistakes at once instead of stopping at the first one.