0 XP · 0 scenes

LeetCode Patterns: 14 Patterns That Cover Most Interview Problems

By Vivek Kumar Bhardwaj · Updated · 7 min read

Solving problems one by one does not scale. Most interview problems are a small number of patterns in different clothes. If you learn to recognise the pattern from the wording, a new problem becomes a familiar one. Here are 14 patterns, how to spot each, and problems to practise it on. Each problem page has hints, the brute force, the smarter idea and code in Python and Java.

The patterns

  1. Hashing. You need fast lookup, counting or a "have I seen this" check. Try: Two Sum, Valid Anagram, Longest Consecutive Sequence.
  2. Two pointers. A sorted array, a pair that meets a condition, or a palindrome. Try: 3Sum, Container With Most Water.
  3. Sliding window. A contiguous subarray or substring with a condition that changes as you slide. Try: Longest Substring Without Repeating Characters, Minimum Window Substring.
  4. Binary search. Sorted input, or a yes/no answer that flips at one point (search on the answer). Try: Search in Rotated Sorted Array, Koko Eating Bananas.
  5. Linked list tricks. Fast and slow pointers, reversing, merging. Try: Linked List Cycle, Reverse Linked List, Merge Two Sorted Lists.
  6. Stack and monotonic stack. Nested structure, or "next greater element". Try: Valid Parentheses, Daily Temperatures.
  7. Tree traversal (DFS and BFS). Do the small job at a node and ask the children the same question. Try: Invert Binary Tree, Level Order Traversal, Lowest Common Ancestor.
  8. Heap and top K. The k-th largest, the k closest, or merging sorted sources. Try: Kth Largest Element in an Array, Merge k Sorted Lists, Find Median from Data Stream.
  9. Intervals. Sort by start, then sweep. Try: Merge Intervals, Meeting Rooms II.
  10. Greedy. A locally best choice that is provably safe. Try: Jump Game, Gas Station.
  11. Backtracking. List all combinations, subsets or arrangements: choose, explore, undo. Try: Subsets, Permutations, Combination Sum.
  12. Graph search (BFS and DFS). Grids, connectivity, dependencies. Try: Number of Islands, Rotting Oranges, Course Schedule.
  13. Dynamic programming. The answer depends on smaller versions of the same problem. Try: Climbing Stairs, House Robber, Coin Change.
  14. Trie and union find. Prefix lookups and grouping items that connect. Try: Implement Trie, Number of Provinces.

How to practise so it sticks

  • Spend 20 to 30 minutes on a problem before you open a hint. Being stuck is where the learning happens.
  • After you see the solution, write down the pattern name and the clue in the problem that points to it.
  • Redo the problem after 1 day, 7 days and about 30 days, without looking.
  • Mix patterns once you know them, so you practise recognising, not just applying.

The full DSA roadmap groups all problems by pattern in a 30-week plan, with a tracker for solved problems and revision.