How to Recognise Coding Patterns
A decision guide that maps problem signals to the right pattern — and a repeatable process for any interview question.
There are thousands of coding-interview problems but only a couple of dozen patterns behind them. Strong candidates don't memorise problems; they recognise the pattern from a few signals in the statement, then adapt a template they know cold.
This track covers each pattern with: when to use it, a reusable template in Java, Python and TypeScript, worked examples, and a practice list ordered by difficulty.
The signal → pattern map
| If the problem mentions… | Think | Lesson |
|---|---|---|
| Sorted array, pair/triplet with a target sum, in-place partition | Two pointers | Two pointers |
| Linked list cycle, middle, "happy number" | Fast & slow pointers | Fast & slow |
| Longest/shortest contiguous subarray or substring with a constraint | Sliding window | Sliding window |
| Range sums, "subarray sum equals k" | Prefix sum + hash map | Prefix sum |
| Overlapping intervals, meetings, merging ranges | Merge intervals | Merge intervals |
| Array of numbers in range 1..n, find missing/duplicate | Cyclic sort | Cyclic sort |
| Next greater/smaller element, histogram, stock span | Monotonic stack | Monotonic stack |
| Sorted / rotated array, "minimum x that works" | Modified binary search | Binary search |
| Reverse a linked list or part of it | In-place reversal | List reversal |
| Tree level by level, minimum depth | Tree BFS | Tree BFS |
| Tree paths, subtree properties, LCA | Tree DFS | Tree DFS |
| Grid islands, connectivity, shortest path in a maze | Graph BFS/DFS | Graph patterns |
| Prerequisites, dependency order | Topological sort | Topological sort |
| Dynamic connectivity, grouping equivalent items | Union-Find | Union-Find |
| Prefixes, dictionary of words, autocomplete | Trie | Trie |
| Median of a stream, balance two halves | Two heaps | Two heaps |
| Top / smallest / most frequent k | Heap of size k | Top K |
| Merge k sorted lists/arrays | K-way merge (heap) | K-way merge |
| All combinations, permutations, subsets; constraint puzzles | Backtracking | Subsets & backtracking |
| Find the unique/missing number, pairs cancel | Bitwise XOR | Bitwise XOR |
| Scheduling, intervals, "minimum number of…" with a clear local choice | Greedy | Greedy |
| Choose items under a capacity/budget, subset sums | 0/1 or unbounded knapsack | Knapsack DP |
| Two strings: LCS, edit distance, interleaving | DP on sequences | DP on sequences |
| Grid paths, palindromic substrings, burst balloons | DP on grids & intervals | Grids & intervals |
Constraints tell you the complexity
| n up to | Feasible complexity | Likely technique |
|---|---|---|
| 10–12 | O(n!) | Permutations / brute force |
| 20–25 | O(2ⁿ) | Subsets, bitmask DP |
| 100–500 | O(n³) | Floyd-Warshall, interval DP |
| 1,000–5,000 | O(n²) | 2D DP, nested loops |
| 10⁵–10⁶ | O(n log n) or O(n) | Sorting, heaps, two pointers, sliding window, hashing |
| 10⁹+ | O(log n) or O(1) | Binary search, math |
A repeatable process (the "UMPIRE" loop)
- Understand. Restate the problem. Ask about input sizes, duplicates, negatives, empty input, sortedness, and what to return. Work a tiny example by hand.
- Match. Use the signal table and the constraints to shortlist patterns. Say your reasoning out loud.
- Plan. Start with brute force and its complexity, then optimise ("we're re-scanning the window → sliding window"). Agree on the approach before coding.
- Implement. Write clean code with good names. Use the template; handle edge cases.
- Review. Trace your code on the example and on an edge case (empty, one element, all equal).
- Evaluate. State time and space complexity, and mention trade-offs or follow-ups.
How to practise
- Study one pattern at a time: read the lesson, then solve its practice problems without looking at solutions for 25–30 minutes each.
- After solving, write down the signal that should have triggered the pattern.
- Revisit problems after 3 days and after 2 weeks (spaced repetition).
- Mix patterns once each is familiar — recognising which pattern applies is the real skill.
Key takeaways
- Recognise patterns from signals in the statement, not from memorised problems.
- Use the input constraints to infer the target complexity.
- Follow a consistent process: understand, match, plan, implement, review, evaluate.