CS concepts

LeetCode Patterns Cheat Sheet Explained: 7 Patterns and Their Clues

Updated Nov 27, 2026 · 3 min read

Most LeetCode questions give their pattern away in the wording. Find a pair or seen before means hash map; sorted array, pairs or triplets means two pointers; longest substring means sliding window; sorted or rotated means binary search; grid, tree or islands means BFS or DFS; how many ways or the biggest total means dynamic programming; k largest or k most frequent means a heap of size k.

Watch onYouTubeInstagram

Key points

  • ●Read the question for its clue before you think about code.
  • ●Seven patterns cover most Blind 75 problems.
  • ●Hash map turns Two Sum from O(n²) into O(n).
  • ●Binary search halves the search space every step, even on rotated input.
  • ●Top K problems need a heap of size k, not a full sort.
Step-by-step visual sheet: LeetCode Patterns Cheat Sheet
The full visual sheet, made with Edithly.Open the interactive sheet

You don't need 500 problems

Grinding hundreds of problems is the slow way to prepare. The faster way is to learn seven patterns and the words in a question that give each one away. Clue first, pattern second, code last.

The seven patterns at a glance

This is the main table on the visual sheet above:

Pattern Question clue Core idea Blind 75 examples
Hash map find a pair; seen before; count Store what you've seen for O(1) lookups Two Sum (1), Contains Duplicate (217), Group Anagrams (49)
Two pointers sorted array; pairs or triplets; palindrome One pointer at each end, move the one that helps 3Sum (15), Container With Most Water (11), Valid Palindrome (125)
Sliding window longest or shortest substring or subarray Grow the right edge, shrink the left when the rule breaks Longest Substring Without Repeating Characters (3), Best Time to Buy and Sell Stock (121)
Binary search sorted or rotated input; O(log n); minimum value that works Halve the search space every step Find Minimum in Rotated Sorted Array (153), Search in Rotated Sorted Array (33)
BFS / DFS grid; tree; graph; islands; levels Visit neighbours with a queue or recursion; mark visited Number of Islands (200), Clone Graph (133), Maximum Depth of Binary Tree (104)
Dynamic programming number of ways; max or min total; choices at each step Save answers to smaller subproblems and reuse them Climbing Stairs (70), House Robber (198), Coin Change (322), Maximum Subarray (53)
Heap (top K) k largest; k most frequent; merge k sorted Keep a heap of size k Top K Frequent Elements (347), Merge k Sorted Lists (23), Find Median from Data Stream (295)

The array and string four

Hash map. The clue is a pair, a value you've seen before, or a count. Two Sum is the classic: checking every pair is O(n²), but if you store each number in a dictionary as you go, you can ask "have I seen the number I need?" in O(1). One pass, O(n).

Two pointers. The clue is a sorted array, pairs or triplets, or a palindrome. Put one pointer at each end and move the one that helps. In Container With Most Water, that's always the shorter side.

Sliding window. The clue is "longest" or "shortest" with "substring" or "subarray". Grow the right edge one step at a time. When the window breaks the rule, like a repeated letter, shrink the left edge until it's fine again.

Binary search. The clue is sorted or rotated input, a demand for O(log n), or "the minimum value that works". Look at the middle, decide which half can't hold the answer, and throw it away.

The other three

BFS / DFS. The clue is a grid, a tree, a graph, islands or levels. Visit neighbours with a queue (BFS) or with recursion or a stack (DFS), and mark each cell or node as visited so you don't loop forever.

Dynamic programming. The clue is "how many ways", a maximum or minimum total, or a choice at each step. Save the answers to smaller subproblems and build the big answer from them. Maximum Subarray (Kadane's algorithm) is the gentlest place to start.

Heap (top K). The clue is k largest, k most frequent, or merging k sorted lists. Keep a heap that never grows past k items, so you never pay for sorting everything.

How to use the sheet

Before you write a line of code on a new problem, read the question once and look only for clue words. The fast recall list at the bottom of the sheet is built for this:

  • pair, seen before, count: hash map
  • sorted, pairs, triplets, palindrome: two pointers
  • longest or shortest substring or subarray: sliding window
  • sorted, rotated, O(log n), minimum that works: binary search
  • grid, tree, graph, islands, levels: BFS or DFS
  • ways, max or min total, choices: dynamic programming
  • k largest, k most frequent, k-way merge: heap

The trap

A clue is a starting point, not a guarantee. "Pair" points to a hash map, but if the array is already sorted, two pointers does it with no extra memory. When two patterns both fit, pick the one that uses what the question hands you for free, like sorted input.

Frequently asked questions

What are the most important LeetCode patterns?

Seven cover most interview questions: hash map, two pointers, sliding window, binary search, BFS/DFS, dynamic programming and heap (top K). Each one has a clue in the wording that points to it.

How do I know which pattern a LeetCode problem uses?

Look for the clue words. A pair or something seen before: hash map. Sorted array, pairs, triplets or palindrome: two pointers. Longest or shortest substring: sliding window. Sorted, rotated or O(log n): binary search. Grid, tree, islands or levels: BFS or DFS. Number of ways or best total: DP. K largest or k most frequent: heap.

Do I need to solve 500 LeetCode problems?

No. Knowing the patterns carries over to new problems better than memorizing answers. Practice each pattern on a few Blind 75 problems until you can spot it from the wording.

Sliding window vs two pointers: what is the difference?

Both use two indexes. Two pointers usually start at opposite ends of a sorted array and move toward each other. A sliding window has both edges moving the same way: grow the right edge, and shrink the left when the window breaks the rule.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free