15 Algorithm Patterns You Must Know for Coding Interviews in 2026
Coding interview patterns are the secret weapon that separates prepared candidates from those who struggle. Rather than memorizing hundreds of individual problem solutions, learning algorithm patterns gives you a transferable toolkit for solving problems you have never seen before. This guide covers 15 essential coding interview patterns, explaining when to use each one, the key insight behind it, and the types of problems it solves.
If you are building a structured study plan, pair this guide with our LeetCode preparation strategy for a complete approach to coding interview readiness.
Pattern 1: Two Pointers
When to use: Problems involving sorted arrays, finding pairs that satisfy a condition, or comparing elements from both ends of a sequence.
Key insight: By maintaining two pointers that move toward each other or in the same direction, you eliminate the need for nested loops and reduce time complexity from O(n squared) to O(n).
Common problems: Two sum in a sorted array, three sum, container with most water, removing duplicates from a sorted array, trapping rain water.
How it works: Place one pointer at the start and one at the end of the array. Based on the current sum or comparison, move one pointer inward. The sorted property guarantees you do not miss valid pairs.
Pattern 2: Sliding Window
When to use: Problems asking for the longest, shortest, or optimal contiguous subarray or substring that satisfies a given condition.
Key insight: Instead of recalculating from scratch for every possible window, expand the window by moving the right pointer and shrink it by moving the left pointer, maintaining a running state as you go.
Common problems: Longest substring without repeating characters, minimum window substring, maximum sum subarray of size K, fruit into baskets, permutation in string.
How it works: Maintain a window defined by left and right indices. Expand right to include new elements. When the window violates the constraint, shrink from the left until the constraint is satisfied again. Track the optimal answer throughout.
Pattern 3: Fast and Slow Pointers
When to use: Cycle detection in linked lists or arrays, finding the middle of a linked list, or determining if a sequence is cyclic.
Key insight: A fast pointer moving at twice the speed of a slow pointer will eventually meet the slow pointer if a cycle exists. This uses O(1) space compared to hash-set approaches.
Common problems: Linked list cycle detection, find the start of a cycle, happy number, middle of linked list, palindrome linked list.
Pattern 4: Merge Intervals
When to use: Problems involving overlapping intervals, scheduling, or time ranges that need to be combined or analyzed.
Key insight: Sort intervals by start time, then iterate through them. If the current interval overlaps with the previous one, merge them by extending the end time. Otherwise, start a new group.
Common problems: Merge overlapping intervals, insert interval, meeting rooms, minimum number of platforms, interval list intersections.
Pattern 5: Binary Search and Its Variations
When to use: Any problem where you can eliminate half the search space based on a condition. This goes far beyond searching sorted arrays.
Key insight: Binary search applies whenever there is a monotonic property. If you can define a condition that is false for the first part and true for the rest (or vice versa), binary search finds the boundary in O(log n).
Common problems: Search in rotated sorted array, find peak element, find minimum in rotated sorted array, koko eating bananas, capacity to ship packages. The last two examples show binary search on the answer space, a powerful technique many candidates overlook.
Pattern 6: BFS (Breadth-First Search)
When to use: Shortest path in unweighted graphs, level-order traversal, or problems requiring exploration layer by layer.
Key insight: BFS explores all nodes at distance K before any node at distance K+1. This guarantees the first time you reach a node is via the shortest path in an unweighted graph.
Common problems: Binary tree level order traversal, shortest path in a grid, word ladder, rotting oranges, minimum knight moves. Use a queue data structure and track visited nodes to avoid cycles.
Pattern 7: DFS (Depth-First Search)
When to use: Exploring all paths, checking connectivity, finding connected components, or problems that require exhaustive search of a graph or tree.
Key insight: DFS goes as deep as possible before backtracking. It naturally maps to recursion and is ideal for problems where you need to explore entire subtrees or all possible paths. Refer to our data structures cheat sheet for graph representation fundamentals.
Common problems: Number of islands, clone graph, path sum, all paths from source to target, course schedule (cycle detection via DFS).
Pattern 8: Top-K Elements
When to use: Finding the K largest, smallest, most frequent, or closest elements from a collection.
Key insight: A min-heap of size K efficiently tracks the top K largest elements in O(n log K) time, which is better than sorting at O(n log n) when K is much smaller than n.
Common problems: Kth largest element, top K frequent elements, K closest points to origin, find K pairs with smallest sums, reorganize string.
Pattern 9: Dynamic Programming (Memoization and Tabulation)
When to use: Optimization problems with overlapping subproblems and optimal substructure. If the problem asks for the minimum, maximum, or number of ways and the solution to the current state depends on solutions to smaller states.
Key insight: Define the state clearly, write the recurrence relation, identify the base case, and decide between top-down (memoization) or bottom-up (tabulation). Start by solving the problem recursively, then add memoization.
Common problems: Climbing stairs, house robber, coin change, longest increasing subsequence, longest common subsequence, edit distance, 0/1 knapsack.
DP categories to know:
- 1D DP: State depends on one variable (climbing stairs, house robber)
- 2D DP: State depends on two variables (unique paths, edit distance)
- Interval DP: State defined by subarray boundaries (burst balloons)
- DP on strings: Two strings compared character by character (LCS, edit distance)
Pattern 10: Backtracking
When to use: Generating all permutations, combinations, subsets, or solving constraint satisfaction problems like Sudoku or N-queens.
Key insight: Backtracking follows a template: make a choice, recurse into the smaller problem, then undo the choice. It systematically explores the solution space while pruning branches that cannot lead to valid solutions.
Common problems: Permutations, combinations, subsets, combination sum, palindrome partitioning, N-queens, word search.
Pattern 11: Monotonic Stack
When to use: Finding the next greater or smaller element, calculating areas of histograms, or problems where you need to efficiently track elements in increasing or decreasing order.
Key insight: Maintain a stack where elements are always in monotonic order. When a new element violates the order, pop elements and process them. This solves next-greater-element type problems in O(n).
Common problems: Next greater element, daily temperatures, largest rectangle in histogram, stock span problem, trapping rain water.
Pattern 12: Union-Find (Disjoint Set Union)
When to use: Problems involving grouping elements, detecting connected components, or determining if two elements belong to the same group.
Key insight: Union-Find supports near O(1) operations for merging sets and checking membership using path compression and union by rank. It is ideal for dynamic connectivity problems.
Common problems: Number of connected components, redundant connection, accounts merge, number of provinces, earliest moment when everyone becomes friends.
Pattern 13: Topological Sort
When to use: Problems involving dependencies, ordering tasks, or detecting cycles in directed graphs.
Key insight: Topological sort produces a linear ordering of vertices such that for every directed edge from u to v, u comes before v. It can be implemented using BFS (Kahn's algorithm) or DFS. If a valid ordering is impossible, a cycle exists.
Common problems: Course schedule, course schedule order, alien dictionary, task scheduler, build order.
Pattern 14: Trie (Prefix Tree)
When to use: Problems involving prefix matching, autocomplete, dictionary word search, or efficient string storage and retrieval.
Key insight: A trie stores strings character by character in a tree structure. Prefix lookups take O(L) time where L is the prefix length, regardless of how many strings are stored. This is more efficient than hash-based approaches for prefix operations.
Common problems: Implement trie, word search in a board, design autocomplete system, replace words, maximum XOR of two numbers.
Pattern 15: Greedy Algorithms
When to use: Optimization problems where making the locally optimal choice at each step leads to the globally optimal solution.
Key insight: Greedy works when the problem has the greedy choice property: a locally optimal choice never needs to be reconsidered. The challenge is proving (or having intuition) that greedy works for the specific problem.
Common problems: Jump game, gas station, task scheduler, minimum number of arrows to burst balloons, assign cookies, partition labels.
How to Build Pattern Recognition
Recognizing which pattern applies to a new problem is the most valuable skill you can develop. Here is a systematic approach:
- Read the problem and identify the input type: Array, string, tree, graph, or intervals. This immediately narrows the possible patterns.
- Identify what the problem is asking: Shortest path suggests BFS. All combinations suggests backtracking. Optimization with subproblems suggests DP. Contiguous subarray suggests sliding window.
- Check for sorted input or monotonic property: These hint at two pointers or binary search.
- Look for constraints: If n is up to 10, backtracking works. If n is up to 10,000, O(n squared) might pass. If n is up to a million, you need O(n) or O(n log n).
- Practice deliberately: After solving a problem, ask yourself which pattern it used and why that pattern was the right choice.
Combine this pattern knowledge with a structured LeetCode study plan and a strong technical resume built with EasyResume to maximize your chances of landing offers at top tech companies. For more on the complete interview process beyond coding, see our coding interview questions guide.