Home Theory8 LeetCode Patterns to solve 2000+ problems

8 LeetCode Patterns to solve 2000+ problems

by nikoo28
0 comments 9 minutes read

There are almost 3,000 problems on LeetCode. Blind 75, Blind 150, and other curated lists promise confidence—but the underlying structure matters more than any single list. After tagging problems by core technique, one discovery stood out: just 8 patterns cover over 2,000 problems. Not 80. Not 20. Eight.

The 8 Patterns at a Glance

#PatternProblemsCumulative
1Two Pointers244244
2Sliding Window164408
3Modified Binary Search256664
4Hash Map / Hash Set4711,135
5Monotonic Stack / Prefix Sum1701,305
6BFS / DFS1671,472
7Memoization (DP)3761,848
8Heap / Priority Queue1562,004

That is 2,004 problems covered with eight techniques. Counts come from community tags and problem metadata on LeetCode—your mileage may vary slightly as tags update, but the order of magnitude holds.

1. Two Pointers

Problems covered: ~244

Two pointers turn many O(n^2) approaches into O(n). You place two index markers and move them based on a condition. Movement stays one-directional, so you avoid redundant work.

How to Recognize It

  • Input is a sorted array or list
  • You need a pair or triplet satisfying a condition
  • In-place operations
  • Keywords: “remove duplicates,” “two numbers that add up to,” “container with most water”

Three Flavors

Opposite direction — One pointer at the start, one at the end; they move toward each other (e.g., two-sum in a sorted array).
Same direction, different speeds — Fast and slow pointers (e.g., cycle detection in a linked list).
Partitioning — One pointer tracks the write position, the other scans (e.g., move zeros to the end).

Practice Problems

2. Sliding Window

Problems covered: ~164 (cumulative with pattern 1: 408)

A sliding window is a contiguous slice of your input. You expand to explore and shrink to restore a condition. Each element is touched at most twice, so you often get O(n) instead of brute-force O(n^2).

Screenshot

How to Recognize It

  • “Contiguous subarray” or “substring
  • Longest or shortest that satisfies…”
  • At most K” distinct elements, characters, or replacements
  • Linear input (array or string)

Fixed vs Dynamic Window

Fixed window — Size is given; slide it across (e.g., maximum average subarray of length k).
Dynamic window — Expand the right edge until a condition breaks, then shrink from the left. Track the best answer as you go.

Practice Problems

3. Modified Binary Search

Problems covered: ~256 (cumulative: 664)

Binary search is not only “find a value in a sorted array.” The powerful form is searching an answer space: you binary-search on the answer itself, not just on array indices.

How to Recognize It

  • Input is sorted (classic case)
  • “Find the minimum value that satisfies…” or “maximum such that…”
  • “Can you do X within Y?” → binary search on Y

If everything below a threshold fails and everything at or above passes, you can binary-search that boundary.

Practice Problems

4. Hash Map / Hash Set

Problems covered: ~471 (cumulative: 1,135)

This pattern appears in roughly one in five LeetCode problems. The idea: trade space for time. Store what you have seen in a hash map for O(1) lookups and drop nested O(n^2) scans.

How to Recognize It

  • “Find duplicates,” “count frequencies,” “group elements”
  • “Does this exist?” or “find the complement
  • Inner loop of brute force is “search for something” → often replace with a map

Bijection (Two-Way Mapping)

Some problems need each key to map to exactly one value and vice versa. Word Pattern is the classic example: one map is not enough if you only check one direction.

Practice Problems

5. Monotonic Stack / Prefix Sum

Problems covered: ~170 (cumulative: 1,305)

Both ideas share one philosophy: preprocess once, answer queries fast.

Prefix Sum

Build a running-sum array; range sums become O(1) via subtraction. Combine prefix sums with a hash map for problems like “number of subarrays summing to k.”
Signals: “subarray sum,” “range sum,” “cumulative.”

Monotonic Stack

Keep the stack in sorted order (increasing or decreasing). Pop elements that break the invariant; popped items often “find” their next greater or next smaller element.
Signals: “next greater element,” “daily temperatures,” “largest rectangle in histogram.”

Practice Problems

6. BFS / DFS

Problems covered: ~167 (cumulative: 1,472)

DFS goes deep first (recursion or explicit stack): paths, cycles, tree properties.
BFS goes level by level (queue): shortest path in unweighted graphs, level-order processing.

How to Recognize It

  • Trees, graphs, grids, matrices
  • “Connected components,” “number of islands,” “shortest path” (often BFS)
  • “Prerequisites,” “dependency order” → topological sort

Quick Rule of Thumb

  • Shortest path or level-by-level → BFS
  • All paths, cycles, deep structure → DFS
  • Dependencies → topological sort (often BFS with in-degree)

Practice Problems

7. Memoization (Dynamic Programming)

Problems covered: ~376 (cumulative: 1,848)

If a problem breaks into overlapping subproblems, cache results instead of recomputing. That is memoization—the heart of most DP on LeetCode.

How to Recognize It

  • “Number of ways,” “minimum cost,” “maximum profit,” “can you reach…”
  • Choices at each step; answer for size n built from smaller sizes

Top-down: recursion + cache. Bottom-up: fill a table iteratively. Both are valid; top-down is often easier to reason about.

DP has sub-patterns (1D/2D, knapsack, intervals)—but recognition always starts with overlapping subproblems and optimal substructure.

Practice Problems

8. Heap / Priority Queue

Problems covered: ~156 (cumulative: 2,004)

A min-heap (or max-heap) gives O(1) access to the extreme element and O(\log n) insert/delete. When you repeatedly need the smallest or largest from a changing set, use a heap.

Screenshot

How to Recognize It

  • “Kth largest” / “Kth smallest”
  • “Top K frequent”
  • “Merge K sorted lists”
  • Streaming median (often two heaps)

Practice Problems

Bonus: What About Greedy?

Greedy does not appear as a separate “mechanical” pattern here on purpose. The other patterns (except hash maps as pure structure) each map to a repeatable template. Greedy is a decision paradigm—the implementation might use sorting, two pointers, heaps, or something else. Roughly 285 LeetCode problems are tagged greedy; mastering the eight patterns above gives you the machinery to implement most of them. The hard part of greedy is often the proof, not the code.

What Now? How to Practice

Knowing the patterns is step one. Practice deliberately:

  1. One pattern at a time — avoid jumping randomly between topics.
  2. Four or five problems per pattern before moving on—start with the links above.
  3. Recognition first — before coding, name the pattern and why it fits.
  4. Track progress — use a structured plan so you are not grinding blindly.

For a guided roadmap that picks problems by pattern, company frequency, and your timeline, see Smart Interview Grind. For all curated links and resources in one place, visit all my helpful resources.

Video Explanation

YouTube player

If you want personalized help choosing problems or structuring prep, schedule a one-on-one session.

More solutions and patterns: GitHub (Java solutions).

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More