Check your interview readinessStart Tech Assessment

LeetCode Patterns

The ~15 patterns that cover most interview problems - how to recognize each one and the template to solve it.

12 min readUpdated Jul 2026By the TopCoding team

There are thousands of LeetCode problems and you will never finish them. You don't need to. Interview problems are variations on roughly fifteen recurring patterns - learn to recognise each one and you can solve problems you've never seen, which is the entire point.

~15
Patterns that cover the large majority of interview problems
150+
Problems in our curated topic path, mapped to these patterns
8
Languages our reference solutions are written in

Why patterns beat grinding

Grinding random problems trains recall - you get good at problems you've seen. Patterns train recognition - you get good at mapping a new problem to a technique you already know. Interviews are always new problems, so recognition is the skill that transfers.

The mental move
Before writing any code, ask: "what shape is this?" Sorted input and a pair? Two pointers. Contiguous subarray? Sliding window. Dependencies? Topological sort. Naming the pattern first turns a blank page into a template.

The 15 core patterns

For each pattern: the signals that it applies, and a few canonical problems to drill it. If you can solve two or three problems per row here, you cover most of what interviews throw at you.

PatternRecognise it when…Practice on
Two pointersSorted array/string, pair or triplet with a target, in-place partitionTwo Sum II, 3Sum, Container With Most Water
Sliding windowLongest/shortest contiguous subarray or substring meeting a conditionLongest Substring Without Repeating, Min Window Substring
Fast & slow pointersCycle detection, find middle, happy-number style loopsLinked List Cycle, Find the Duplicate Number
Merge intervalsOverlapping intervals, scheduling, merging rangesMerge Intervals, Meeting Rooms II
Binary searchSorted data, or "search on the answer" for a monotonic functionSearch in Rotated Array, Koko Eating Bananas
BFSShortest path / level-order on unweighted graphs and treesBinary Tree Level Order, Rotting Oranges
DFS / backtrackingExplore all paths, permutations, combinations, subsetsSubsets, Word Search, N-Queens
Top-K / heap"K largest/smallest/most frequent", running mediansTop K Frequent Elements, Kth Largest
Hash map / setO(1) lookup, counting, dedup, groupingTwo Sum, Group Anagrams, Longest Consecutive
Dynamic programmingOptimal substructure + overlapping subproblemsCoin Change, House Robber, Edit Distance
GreedyLocally optimal choice yields global optimumJump Game, Gas Station
Topological sortOrdering with dependencies / DAGsCourse Schedule, Alien Dictionary
Union-findConnected components, cycle detection in undirected graphsNumber of Provinces, Redundant Connection
TriePrefix search, autocomplete, word dictionariesImplement Trie, Word Search II
Monotonic stackNext greater/smaller element, spans, histogramsDaily Temperatures, Largest Rectangle in Histogram

How to recognize a pattern

Patterns announce themselves through keywords and input shape. Train these triggers until they're automatic:

  • "Sorted" + pair/triplet/target β†’ two pointers or binary search.
  • "Contiguous" / "substring" / "window" β†’ sliding window.
  • "All combinations / permutations / subsets" β†’ backtracking.
  • "Shortest path" on unweighted graph β†’ BFS.
  • "Top / Kth / most frequent" β†’ heap.
  • "Number of ways" / "min/max cost" with choices β†’ dynamic programming.
  • "Order with prerequisites" β†’ topological sort.
  • "Next greater/smaller" β†’ monotonic stack.

Complexity cheat sheet

Interviewers always ask for time and space complexity. Know the defaults cold so you can state them without thinking:

ApproachTimeSpace
Hash map lookupO(n)O(n)
Two pointers / sliding windowO(n)O(1)
Binary searchO(log n)O(1)
SortingO(n log n)O(1) - O(n)
BFS / DFS on graphO(V + E)O(V)
Heap of size kO(n log k)O(k)
Backtracking (subsets)O(2ⁿ)O(n) depth
DP (2D table)O(nΒ·m)O(nΒ·m)
Don't stop at brute force
State the brute-force complexity, then improve it out loud - "this is O(nΒ²), but a hash map drops it to O(n)". That narration is a scored signal in the FAANG loop.

A study plan that works

Depth over breadth. Two focused weeks on patterns beat two months of random grinding:

  • Week 1 - one pattern per day: learn the template, solve 3-4 problems, write the complexity from memory.
  • Week 2 - mixed sets with no pattern label, so you practise recognition, not recall - exactly like the real thing.
  • Ongoing - re-solve any problem you couldn't crack within a week; spaced repetition is what makes patterns stick.
Practice on a curated path
TopCoding maintains a topic-ordered path of 150+ problems mapped to these patterns, with reference solutions in 8 languages and mock interviews to pressure-test recognition. Book a free call to get the path matched to your target companies.

Sources & further reading

  1. 1LeetCode - problem set & discussion β€” LeetCode
  2. 2NeetCode - patterns and curated roadmap β€” NeetCode
  3. 3Big-O cheat sheet β€” bigocheatsheet.com
  4. 4Cracking the Coding Interview β€” Gayle Laakmann McDowell