Most coding interview questions are variations on a small set of patterns. Once you recognize which pattern a problem falls into, you already know the general approach and can focus on the details.
This guide covers the 10 patterns that show up most often in technical interviews for internships and new grad roles. For each one, you will find an explanation of when to use it, how it works, and the types of problems where it applies.
1. Sliding window
Use this when you need to find a subarray or substring that meets some condition (longest, shortest, contains certain characters, etc.). The idea is to maintain a "window" over the data and expand or shrink it as needed, rather than checking every possible subarray.
How it works: Start with two pointers at the beginning. Expand the right pointer to grow the window. When the window violates the constraint, shrink it by moving the left pointer. Track the best result as you go.
Typical problems:
- Longest substring without repeating characters
- Minimum window substring
- Maximum sum subarray of size k
- Longest substring with at most k distinct characters
Time complexity: Usually O(n), since each element is visited at most twice (once by each pointer).
2. Two pointers
Use this when working with sorted arrays or linked lists where you need to find pairs or subarrays that satisfy a condition. Two pointers moving toward each other (or in the same direction) can often replace a nested loop.
How it works: Place one pointer at the start and one at the end. Depending on the current result, move one pointer inward. For same-direction variants, both pointers start at the beginning and move at different speeds.
Typical problems:
- Two Sum (sorted array)
- 3Sum
- Container with most water
- Remove duplicates from sorted array
- Linked list cycle detection (fast/slow pointers)
3. Binary search
Binary search is not just for "find this element in a sorted array." The deeper pattern is: whenever you have a monotonic condition (something that goes from false to true, or true to false), you can binary search for the transition point.
How it works: Maintain a low and high bound. Check the midpoint. Based on the result, eliminate half the search space. Repeat until the bounds converge.
Typical problems:
- Search in rotated sorted array
- Find first/last occurrence of a value
- Koko eating bananas (binary search on answer)
- Median of two sorted arrays
- Find minimum in rotated sorted array
Key insight: "Binary search on the answer" is a common interview technique. Instead of searching through the input, you binary search through the possible range of answers and check if each candidate answer is feasible.
4. BFS and DFS (graph/tree traversal)
BFS (breadth-first search) and DFS (depth-first search) are the building blocks for almost every graph and tree problem. The choice between them depends on what you're looking for.
Use BFS when: You need the shortest path in an unweighted graph, or you need to process nodes level by level.
Use DFS when: You need to explore all paths, detect cycles, or the problem involves backtracking.
Typical problems:
- Number of islands
- Binary tree level order traversal (BFS)
- Clone graph
- Course schedule (cycle detection)
- Word ladder (BFS for shortest transformation)
- All paths from source to target (DFS)
5. Backtracking
Backtracking is DFS with pruning. You explore candidates for a solution, and as soon as you determine that a candidate cannot lead to a valid solution, you abandon it and try the next one. It is the go-to approach for combinatorial problems.
How it works: Build the solution incrementally. At each step, try all valid options. If you reach a dead end, undo the last choice and try the next option.
Typical problems:
- Generate all permutations / combinations
- N-Queens
- Sudoku solver
- Word search in a grid
- Subsets / power set
Tip: The time complexity for backtracking is often exponential, and that is expected. The interviewer knows this. Focus on writing clean recursive code with proper base cases and pruning.
6. Dynamic programming
Dynamic programming (DP) applies when a problem has overlapping subproblems and optimal substructure. In plain terms: if you can break the problem into smaller problems, and those smaller problems get solved multiple times, DP lets you solve each one only once and reuse the result.
Two approaches:
- Top-down (memoization) - Write the recursive solution first, then cache results so you don't recompute them.
- Bottom-up (tabulation) - Fill in a table from the smallest subproblems up to the full problem.
Typical problems:
- Climbing stairs
- Longest common subsequence
- 0/1 Knapsack
- Coin change
- Edit distance
- Longest increasing subsequence
How to recognize DP problems: If the brute force involves making choices at each step and the number of unique states is manageable, it is probably DP. The words "minimum," "maximum," "number of ways," and "is it possible" are strong signals.
7. Greedy
Greedy algorithms make the locally optimal choice at each step, hoping it leads to a globally optimal solution. They are simpler than DP, but they only work when the problem has the "greedy choice property" (a local optimum leads to a global optimum).
Typical problems:
- Activity selection / meeting rooms
- Jump game
- Gas station
- Assign cookies
- Task scheduler
Tip: If you think a problem might be greedy, try to find a counterexample. If you can't, it's probably safe to go greedy. In interviews, briefly explain why the greedy approach works for this specific problem.
8. Heap / priority queue
Use a heap when you repeatedly need the smallest or largest element from a collection, especially when the collection changes over time. Heaps give you O(log n) insert and O(1) access to the min or max.
Typical problems:
- Kth largest element in an array
- Merge k sorted lists
- Top k frequent elements
- Find median from data stream (two heaps)
- Meeting rooms II (minimum conference rooms)
9. Trie
A trie (prefix tree) is a tree structure for storing strings where each node represents a character. It is the go-to data structure when you need prefix-based operations: autocomplete, spell checking, or searching for words with a given prefix.
Typical problems:
- Implement trie (insert, search, startsWith)
- Word search II (trie + backtracking)
- Design autocomplete system
- Longest word in dictionary
10. Monotonic stack
A monotonic stack maintains elements in either increasing or decreasing order. It is useful for "next greater element" or "next smaller element" problems, where you need to look ahead or behind in an array efficiently.
How it works: Iterate through the array. Before pushing a new element, pop all elements from the stack that violate the monotonic property. The popped elements have found their "answer" (the current element).
Typical problems:
- Next greater element
- Daily temperatures
- Largest rectangle in histogram
- Trapping rain water
How to practice effectively
- Learn the pattern, then do problems. Don't grind problems randomly. Study a pattern, do 3-5 problems that use it, then move to the next one.
- Identify the pattern before coding. When you see a new problem, spend a minute deciding which pattern applies before writing any code. This skill is what separates good interviewees from great ones.
- Time yourself. In a real interview, you have 20-30 minutes per problem. Practice under time pressure.
- Explain your approach out loud. The interviewer cares as much about your thought process as your code. Practice verbalizing why you chose a specific pattern.
- Don't memorize solutions. If you can't re-derive a solution from the pattern, you haven't learned it. Understanding beats memorization every time.
Quick reference: pattern recognition
| If the problem says... | Think about... |
|---|---|
| "Contiguous subarray" or "substring" | Sliding window |
| "Sorted array" + "find pair" | Two pointers |
| "Minimum/maximum" + sorted or monotonic | Binary search |
| "Shortest path" (unweighted) | BFS |
| "All combinations" or "all permutations" | Backtracking |
| "Number of ways" or "min cost" | Dynamic programming |
| "Top k" or "kth largest" | Heap |
| "Prefix" or "autocomplete" | Trie |
| "Next greater/smaller element" | Monotonic stack |