Common Interview Patterns Cheatsheet

intermediate patterns cheatsheet interview

This is a cheatsheet. Bookmark it, print it, tape it to the wall. The fastest way to solve interview problems isn’t memorizing solutions — it’s recognizing which pattern applies. Once we know the pattern, the solution flows naturally.

The Master Pattern Table

Problem Signal → Pattern Mapping
Two Pointers
Sorted array + find pair • Reverse in-place • Palindrome check • Remove duplicates from sorted
Keywords: "sorted", "pair", "in-place", "two sum"  |  TC: O(n)
Sliding Window
Contiguous subarray/substring + max/min/count • "At most k distinct" • Fixed-size window sum
Keywords: "subarray", "substring", "contiguous", "window"  |  TC: O(n)
Hash Map / Set
Count frequency • Find duplicates • Two sum (unsorted) • Group anagrams • O(1) lookup needed
Keywords: "frequency", "count", "duplicate", "anagram"  |  TC: O(n), SC: O(n)
Prefix Sum
Range sum queries • Subarray sum equals k • "Sum between index i and j"
Keywords: "subarray sum", "range sum", "cumulative"  |  TC: O(n) build, O(1) query
Binary Search
Sorted array search • "Minimize the maximum" • "Find boundary" • Rotated sorted array
Keywords: "sorted", "log n", "minimize maximum", "search space"  |  TC: O(log n)
BFS
Shortest path (unweighted) • Level-order traversal • "Minimum steps" • Grid shortest path
Keywords: "shortest", "minimum steps", "level by level", "nearest"  |  TC: O(V + E)
DFS
Tree traversal • Connected components • "Does a path exist?" • Island counting • Cycle detection
Keywords: "path", "connected", "island", "tree", "explore all"  |  TC: O(V + E)
Dynamic Prog.
"How many ways" • "Min/max cost to reach" • Overlapping subproblems • Can't use greedy
Keywords: "ways", "minimum cost", "maximum profit", "can we reach"  |  TC: O(n^2) or O(n*m)
Greedy
Interval scheduling • Jump game • "Optimal local choice leads to global optimum" • Activity selection
Keywords: "schedule", "intervals", "jump", "gas station"  |  TC: O(n log n) usually
Backtracking
"All possible combinations/permutations" • Subsets • N-Queens • Sudoku • Word search
Keywords: "all possible", "generate all", "combinations", "permutations"  |  TC: O(2^n) or O(n!)
Monotonic Stack
Next greater/smaller element • Largest rectangle in histogram • Stock span • Daily temperatures
Keywords: "next greater", "next smaller", "span", "histogram"  |  TC: O(n)
Heap / Priority Q
Kth largest/smallest • Merge k sorted lists • Top k frequent • Running median • Task scheduling
Keywords: "kth", "top k", "merge k sorted", "median stream"  |  TC: O(n log k)
Union-Find
Connected components • "Are X and Y connected?" • Kruskal's MST • Accounts merge • Redundant connection
Keywords: "connected", "union", "group", "component"  |  TC: O(α(n)) per operation
Trie
Autocomplete • Prefix matching • Word dictionary • Spell checker • "Starts with" queries
Keywords: "prefix", "dictionary", "autocomplete", "starts with"  |  TC: O(L) per operation

Quick Decision Flowchart

Not sure which pattern to use? Walk through this:

Pattern Selection Flowchart
1.
Is it about a sorted array or search space?
→ Binary Search. If "minimize the max" → Binary Search on Answer.
2.
Is it about a contiguous subarray or substring?
→ Sliding Window. If it involves sums → maybe Prefix Sum too.
3.
Is it about pairs in a sorted array?
→ Two Pointers.
4.
Is it about a tree or graph?
→ BFS for shortest path / level-order. DFS for everything else. Union-Find for connectivity.
5.
Does it say "all possible" or "generate all"?
→ Backtracking.
6.
Does it ask "how many ways" or "min/max cost"?
→ Dynamic Programming. Try greedy first -- if it fails, it's DP.
7.
Does it involve "next greater" or "next smaller"?
→ Monotonic Stack.
8.
Does it say "kth largest" or "top k"?
→ Heap / Priority Queue.
9.
Does it involve lookups, counting, or grouping?
→ Hash Map / Set. When in doubt, a hash map is almost always useful.

Common Combinations

Sometimes one pattern isn’t enough. Here are combos that show up frequently:

Problem TypeCombo
Subarray sum equals kPrefix Sum + Hash Map
Minimum window substringSliding Window + Hash Map
Meeting rooms IIIntervals + Sweep Line (or Heap)
Course scheduleGraph + Topological Sort (BFS/DFS)
Word search in gridDFS + Backtracking
Kth smallest in sorted matrixBinary Search + Heap
Accounts mergeUnion-Find + Hash Map
Autocomplete with rankingTrie + Heap

Time Complexity Quick Reference

PatternTypical TimeTypical Space
Two PointersO(n)O(1)
Sliding WindowO(n)O(k)
Hash MapO(n)O(n)
Prefix SumO(n) build, O(1) queryO(n)
Binary SearchO(log n)O(1)
BFS/DFSO(V + E)O(V)
DP (1D)O(n)O(n)
DP (2D)O(n * m)O(n * m)
BacktrackingO(2^n) or O(n!)O(n)
Heap operationsO(n log k)O(k)
Union-FindO(n * a(n))O(n)
TrieO(L) per opO(total chars)

General Interview Tips for DSA

Before coding:

  • Repeat the problem back to the interviewer. Make sure we understand edge cases.
  • Ask about constraints. Array size tells us the expected complexity (n <= 10^5 means O(n log n) is fine, n <= 20 means O(2^n) might be expected).
  • Walk through 1-2 examples by hand. This often reveals the pattern.

During coding:

  • Start with brute force and optimize. Say “the brute force is O(n^2), but we can do better with…” Interviewers love hearing the thought process.
  • Name variables clearly. left, right, seen, result — not i, j, x, y.
  • Talk while we code. Silence is the enemy in interviews.

After coding:

  • Trace through the code with a small example. Catch bugs before the interviewer does.
  • State the time and space complexity without being asked.
  • Mention edge cases: empty input, single element, all duplicates, negative numbers.

The meta-strategy:

  • If stuck for more than 2 minutes, say so. “I’m considering a few approaches…” is better than awkward silence.
  • If we recognize the pattern but can’t remember the exact implementation, describe the approach. Interviewers give partial credit for understanding.
  • If the interviewer gives a hint, take it. Don’t fight it. They’re trying to help us succeed.

In simple language, the interviewer isn’t testing if we’ve memorized LeetCode solutions. They’re testing if we can break a problem down, pick the right tool, and implement it cleanly. The patterns above are our toolbox. The more we practice recognizing which tool fits, the faster we’ll solve new problems we’ve never seen before.