Most live coding questions are variations of a small set of ideas. Once you can recognize which idea a problem is asking for, the blank editor stops being scary. You are no longer inventing a solution from nothing. You are choosing a tool you have used before.
This article lists twelve patterns that cover the majority of questions in coding rounds, the signals that point to each one, and a practice plan that builds recognition rather than memory.
Why patterns beat problem counts
Solving hundreds of problems without reflection teaches you hundreds of answers. Interviewers tweak inputs, constraints, and wording, and memorized answers break. Patterns transfer. When you finish a problem, the useful question is not "did I pass?" but "what made this a sliding window problem, and how would I spot that faster next time?"
The twelve patterns
1. Two pointers
Signals: a sorted array or string, finding a pair or triplet that meets a condition, removing duplicates in place, checking a palindrome.
Two indexes move toward each other or in the same direction, so you avoid a nested loop. Typical cost drops from quadratic to linear.
2. Sliding window
Signals: "longest" or "shortest" contiguous subarray or substring, "at most k distinct," a running sum over a range.
Keep a window with a left and right edge. Grow the right edge, and shrink the left edge whenever the window breaks the rule. A hash map often tracks what is inside the window.
3. Hash map counting and lookup
Signals: "find if any two," frequency questions, anagrams, first unique element, grouping.
Trading memory for speed is the most common optimization in interviews. If your brute force has a nested loop that searches for something, ask whether a map could remember it instead.
4. Prefix sums
Signals: many range sum queries, "subarray sum equals k," balance or difference over ranges.
Precompute running totals so any range sum is one subtraction. Combined with a hash map of seen prefix sums, it solves a large family of subarray problems in linear time.
5. Binary search, including on the answer
Signals: sorted input, "minimum value such that," "maximum capacity that still works," a monotonic yes or no condition.
Beyond searching arrays, binary search can find the smallest answer that passes a check. If you can write a function works(x) that flips from false to true once, you can binary search on x.
6. Stack and monotonic stack
Signals: matching brackets, "next greater element," evaluating expressions, histogram areas, undo operations.
A monotonic stack keeps elements in increasing or decreasing order and pops when the order breaks. It answers "nearest bigger or smaller to the left or right" for every element in one pass.
7. Heap or priority queue
Signals: "top k," "k closest," merging sorted lists, scheduling, running median.
A heap keeps the smallest or largest element available in logarithmic time. For top k problems, a heap of size k is usually the expected answer.
8. Linked list manipulation
Signals: reverse a list, detect a cycle, find the middle, merge two lists.
Fast and slow pointers find cycles and midpoints. A dummy head node removes most edge cases. Draw the pointers on paper before you code.
9. Tree traversal, depth first and breadth first
Signals: anything with a binary tree or a hierarchy, level by level output, path sums, lowest common ancestor.
Know recursive depth first search cold, and know breadth first search with a queue for level order questions. Most tree problems come down to deciding what each recursive call returns.
10. Graph search
Signals: grids, islands, networks, dependencies, shortest path in an unweighted graph, "can you reach."
Model the problem as nodes and edges, then choose breadth first search for shortest steps, depth first search for connectivity, and a visited set for both. Topological sort handles ordering with dependencies, such as course schedules or build steps.
11. Backtracking
Signals: "all combinations," "all permutations," subsets, placing items under constraints, word search.
Build a candidate step by step, undo the last step, and try the next option. Pruning early is what makes it fast enough.
12. Dynamic programming
Signals: "number of ways," "minimum cost," "maximum profit," choices that depend on earlier choices, overlapping subproblems.
Start with a recursive solution, add memoization, then convert to a table if needed. Define the state in one sentence before writing code, for example "dp[i] is the best answer using the first i items."
How to recognize the pattern in an interview
When the problem appears, run through a short checklist before writing code:
- Is the input sorted, or can I sort it? Think two pointers or binary search.
- Is the question about a contiguous range? Think sliding window or prefix sums.
- Am I searching for something repeatedly? Think hash map.
- Is there a hierarchy or a network? Think tree or graph traversal.
- Do I need all possibilities? Think backtracking.
- Do choices build on smaller versions of the same problem? Think dynamic programming.
- Do I need the best k of something? Think heap.
Say this reasoning out loud. Interviewers give credit for the path, not only the destination.
A four-week practice plan
| Week | Patterns | Goal |
|---|---|---|
| 1 | Hash map, two pointers, sliding window, prefix sums | Solve easy and medium array and string problems in under 30 minutes |
| 2 | Binary search, stacks, heaps, linked lists | Explain the time cost of each structure from memory |
| 3 | Trees and graphs | Write DFS and BFS without hesitation, handle grids |
| 4 | Backtracking and dynamic programming, then mixed review | Define the state and recurrence before coding |
For each problem:
- Try for 25 minutes before reading a solution.
- If you read one, close it and rewrite the solution from scratch the next day.
- Write one sentence on what signal should have pointed you to the pattern.
Common mistakes in live coding rounds
- Coding before clarifying. Ask about input size, empty input, duplicates, and negative numbers.
- Silent thinking. The interviewer cannot help or give credit if they do not know what you are trying.
- Skipping the brute force. Stating a simple solution first shows you understand the problem and gives you a fallback.
- No testing. Walk through one normal case and one edge case before saying you are done.
- Ignoring complexity. Always finish with the time and space cost.
FAQ
How many LeetCode problems do I need to solve?
There is no magic number. Many candidates do well after 80 to 150 problems chosen to cover these patterns and reviewed carefully. Recognition and explanation matter more than the total.
Which language should I use in a coding interview?
Use the language you are fastest and most accurate in, unless the role requires a specific one. Python is popular because it is concise, but a language you know well is better than one that looks shorter.
Are coding patterns enough for senior interviews?
They cover the coding round. Senior loops add system design and deeper project discussions, so balance pattern practice with the system design guide and your project stories.
What if I recognize the pattern but cannot finish?
Explain the approach clearly, write the parts you can, and describe what the rest would do. A correct plan with partial code often scores better than a rushed solution with bugs.
Practice on the stack the job uses
Interviews builds prep quizzes and topics from the company and stack on the role you are applying for, so your practice matches the round you will face. Create a free profile, or look for your next role among Python, Go, and Java openings.