Your pattern gym — train recognition, not memorization
Think of this page as flashcard training for your brain's pattern-recognition circuits. A real interview isn't "write the code for Two Sum" — it's "here's a problem you've never seen; figure out which type of problem it is." These flashcards train exactly that skill: read the problem, say the approach, then reveal.
🎯 How to use: For each card, say your approach and its Big-O out loud before revealing. Then mark "Knew it" or "Revise again". The goal is instant pattern recognition, not memorizing code.
That's a trained skill, not a talent. Read How to Think (the Interview Cheat Code) first — it gives you the clue → pattern decoder table and walks you through the questions to ask yourself on real problems. Then come back and drill.
Complexity cheat-sheet for this bank
| Pattern | Time | Space | Key data structure |
|---|---|---|---|
| Hash map lookup | O(1) avg | O(n) | Hash table |
| Sliding window | O(n) | O(1)–O(k) | Two pointers + hash/set |
| Binary search | O(log n) | O(1) | — |
| Stack / monotonic | O(n) | O(n) | Stack |
| BFS / DFS on graph | O(V+E) | O(V) | Queue / recursion |
| Min/max heap | O(n log k) | O(k) | Heap |
| DP (1-D) | O(n · states) | O(n) | Array |
| Backtracking | O(n · n!) worst | O(n) stack | Recursion |
Code pattern references
The key operations used across all problems below — in all three languages:
# Core data structures
from collections import defaultdict, deque
import heapq
seen = {} # hash map: O(1) lookup/insert
stack = [] # stack: append/pop
queue = deque() # queue: appendleft/popleft
min_heap = [] # min-heap
heapq.heappush(min_heap, val) # O(log n)
top = heapq.heappop(min_heap) # O(log n)
import java.util.*;
// Core data structures in Java
Map<Integer, Integer> map = new HashMap<>(); // O(1) lookup
Deque<Integer> stack = new ArrayDeque<>(); // stack: push/pop
Queue<Integer> queue = new LinkedList<>(); // BFS queue
PriorityQueue<Integer> minHeap = new PriorityQueue<>(); // min-heap
minHeap.offer(val); // O(log n)
int top = minHeap.poll(); // O(log n)
#include <unordered_map>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
unordered_map<int, int> umap; // O(1) average lookup
stack<int> stk; // stack
queue<int> bfsQ; // BFS queue
priority_queue<int, vector<int>, greater<int>> minHeap; // min-heap
minHeap.push(val); // O(log n)
int top = minHeap.top(); minHeap.pop(); // O(log n)
Arrays, strings & hashing
Binary search
Stacks & linked lists
Trees
Graphs
Heaps & intervals
DP
Backtracking & greedy
Think it through
Before revealing — what pattern does this problem belong to, and what's your Big-O?
PROBLEMFor each of the five problems below, decide the pattern BEFORE looking at the answer. Time yourself. (1) 'Find the median from a data stream as numbers are added.' (2) 'Given a grid of 0s and 1s, find the largest connected region of 1s.' (3) 'Return all valid parentheses strings of length 2n.' (4) 'Minimum cost to connect all points in a 2D plane.' (5) 'Longest palindromic substring.'
- 1
Problem 1: Median from stream
“Numbers arrive one by one. You need the median at any point. What structure gives you both the lower and upper halves instantly?”
- 2
Problem 2: Largest connected region
“Grid, connectivity, find regions — what pattern?”
unlocks after the stage above - 3
Problem 3: All valid parentheses strings
“'Return ALL valid strings' — what does 'all' always signal?”
unlocks after the stage above - 4
Problem 4: Minimum cost to connect all points
“Connect all nodes with minimum total edge weight — classic name for this?”
unlocks after the stage above - 5
Problem 5: Longest palindromic substring
“Palindromes are symmetric. What's the O(n) insight instead of checking every substring?”
unlocks after the stage above
Practice — climb the ladder
Climb in order — every rung assumes the one above it. Solve on LeetCode, then tick it here; progress is saved on this device.
Foundational
one pattern per problem, instant recognition- Two SumEasy
Hash map complement lookup — the O(n²)→O(n) upgrade.
Stack — LIFO mirrors nesting.
- Climbing StairsEasy
1-D DP / Fibonacci — 'ways to reach' = DP fingerprint.
Core flashcard set
medium problems where the pattern isn't immediately obviousSliding window — expand right, shrink left on duplicate.
Prefix × suffix pass, no division, O(1) extra.
- Meeting Rooms IIMedium
Min-heap of end times — classic interval sweep.
- Course ScheduleMedium
Topological sort / cycle detection — Kahn's BFS.
Stretch
compound patternsTwo-heap trick — always accessible median.
Min-heap over k heads — O(N log k).
Check yourself — instant pattern recognition
1. 'Given a string, find the longest substring with at most 2 distinct characters.' Which pattern?
2. For 'Kth largest element in a stream', which is correct?
3. Two Sum with a SORTED array — which is more space-efficient than a hash map?