The 14 LeetCode Patterns You Need to Know

A concise breakdown of the 14 algorithmic patterns that cover the majority of coding interview problems.

Why patterns matter more than problem count

Solving 500 LeetCode problems without recognizing patterns is like memorizing dictionary entries without learning grammar. Patterns give you a framework to approach new problems you have never seen before — which is exactly what happens in real interviews.

These 14 patterns cover roughly 80% of the algorithmic problems you will encounter at companies like Google, Amazon, Meta, and Microsoft. Master them and you will recognize the approach within the first minute of reading a problem.

1. Sliding Window

Use when you need to find a subarray or substring that satisfies a condition. The window expands or shrinks as you iterate through the array.

When to use it

Problems involving contiguous subarrays or substrings with a constraint: maximum sum subarray of size K, longest substring without repeating characters, smallest subarray with a given sum.

Key insight

Instead of recalculating for every possible subarray (O(n^2)), maintain a running calculation and slide the window forward. Time complexity drops to O(n).

2. Two Pointers

Use two pointers that move toward each other or in the same direction to solve problems on sorted arrays or linked lists.

When to use it

Pair sum in a sorted array, removing duplicates in place, squaring a sorted array, comparing strings with backspaces, container with most water.

Key insight

When the input is sorted (or can be sorted), two pointers eliminate the need for a hash map or nested loop. One pointer starts at the beginning, the other at the end.

3. Fast and Slow Pointers

Also called the tortoise and hare algorithm. One pointer moves at 1x speed, the other at 2x speed.

When to use it

Detecting cycles in a linked list, finding the middle of a linked list, determining if a number is happy, finding the start of a cycle.

Key insight

If there is a cycle, the fast pointer will eventually catch up to the slow pointer. If there is no cycle, the fast pointer reaches the end first.

4. Merge Intervals

Deal with overlapping intervals by sorting and merging.

When to use it

Merging overlapping intervals, inserting a new interval, finding intersections between two interval lists, minimum meeting rooms.

Key insight

Sort intervals by start time. Then iterate and merge whenever the current interval overlaps with the previous one (current.start <= previous.end).

5. Cyclic Sort

When you have an array containing numbers in a given range (1 to n), cyclic sort places each number at its correct index in O(n).

When to use it

Find the missing number, find all missing numbers, find the duplicate number, find the corrupt pair.

Key insight

For each position, swap the current number to its correct position. After the sort, any number not at its correct index reveals the answer.

6. In-Place Reversal of a Linked List

Reverse a linked list (or part of it) by manipulating pointers without extra space.

When to use it

Reverse a linked list, reverse a sub-list, reverse every K-element sub-list, reverse alternating K-element sub-lists.

Key insight

Use three pointers: previous, current, and next. At each step, point current.next to previous, then advance all three pointers.

7. Tree BFS (Breadth-First Search)

Process a tree level by level using a queue.

When to use it

Level order traversal, zigzag traversal, minimum depth of a tree, level averages, connect level-order siblings, right view of a tree.

Key insight

Add the root to a queue. For each level, process all nodes currently in the queue and add their children. The queue size at the start of each iteration equals the level size.

8. Tree DFS (Depth-First Search)

Traverse a tree by going deep before going wide, using recursion or an explicit stack.

When to use it

Path sum, all paths for a sum, path with maximum sum, diameter of a tree, count paths for a sum, tree boundary traversal.

Key insight

At each node, decide whether to include it in the result based on a running state (sum, path, depth). Recurse on left and right children. The base case is a null node or a leaf node.

9. Two Heaps

Use a max-heap and a min-heap together to efficiently track the median or partition elements.

When to use it

Find the median of a number stream, sliding window median, maximize capital (select projects with constraints).

Key insight

The max-heap stores the smaller half, the min-heap stores the larger half. The median is always at the top of one or both heaps. Rebalance after each insertion.

10. Subsets

Generate all subsets (or permutations, or combinations) using BFS or backtracking.

When to use it

All subsets, subsets with duplicates, permutations, string permutations by changing case, balanced parentheses, unique generalized abbreviations.

Key insight

Start with an empty set. For each new element, create new subsets by adding it to every existing subset. For duplicates, only add to subsets created in the previous step.

11. Modified Binary Search

Adapt binary search for variations beyond simple sorted array lookup.

When to use it

Search in a rotated sorted array, find the ceiling/floor of a number, find the minimum difference element, bitonic array maximum, search in a sorted infinite array.

Key insight

The core idea is always the same: eliminate half the search space at each step. The modification is in how you decide which half to eliminate.

12. Top K Elements

Use a heap to efficiently find the K largest, smallest, or most frequent elements.

When to use it

Top K numbers, Kth smallest number, K closest points to the origin, connect ropes with minimum cost, top K frequent numbers.

Key insight

To find the K largest, use a min-heap of size K. Every element that is larger than the heap's minimum replaces it. At the end, the heap contains the K largest elements.

13. K-Way Merge

Merge K sorted arrays or lists using a min-heap.

When to use it

Merge K sorted lists, Kth smallest number in M sorted lists, Kth smallest number in a sorted matrix, smallest number range covering elements from K lists.

Key insight

Push the first element from each list into a min-heap. Pop the smallest, push the next element from that same list. Repeat until all elements are processed.

14. Topological Sort

Order elements in a directed acyclic graph (DAG) such that for every directed edge from A to B, A comes before B.

When to use it

Task scheduling, course schedule, alien dictionary, sequence reconstruction, minimum height trees.

Key insight

Use Kahn's algorithm: compute in-degrees, start with nodes that have zero in-degree, process them and reduce the in-degree of their neighbors. Repeat until the queue is empty.

How to practice with these patterns

Do not try to learn all 14 at once. Focus on one pattern per day or every two days. Solve 3-5 problems per pattern, starting with easy and working up to medium. Once you can recognize which pattern a problem needs within the first minute, you are ready.

For real-time help during interviews, tools like Phantom Coder can analyze the problem on your screen and suggest the right pattern and approach — invisible to screen sharing and interview platforms.

Ready to ace your next interview?

Phantom Coder gives you real-time AI assistance, invisible to screen sharing.

Download for macOS — $99/mo