Pattern Recognition
Most candidates try to solve problems.
Top candidates first identify the pattern, then apply a known solution framework.
LeetCode is not 2000 random problems.
It is ~40 repeatable patterns disguised with different stories.
If you master pattern recognition, you stop guessing and start diagnosing.
The Core Idea
Every problem can be reduced to:
- Input Type
- Constraint Signals
- What Is Being Optimized
- Implicit Data Structure
- Traversal Strategy
- State Relationship
Pattern recognition = mapping these signals → known algorithm families.
Step 1 — Identify the Input Type
The fastest first filter.
| Input Type | Likely Patterns |
|---|---|
| Array | Two pointers, Sliding window, Prefix sum, Binary search |
| String | Sliding window, Hashing, KMP, Trie |
| Linked List | Fast/slow pointers, Reverse, Cycle detection |
| Tree | DFS, BFS, Tree DP |
| Graph | DFS, BFS, Topological sort, Union-Find |
| Matrix | BFS, DFS, Multi-source BFS |
| Stream | Heap, Two heaps |
| Interval list | Greedy, Sorting, Merge intervals |
If you identify input correctly, you eliminate 70% of wrong approaches.
Step 2 — Look for Optimization Keywords
The question almost always tells you the pattern.
Sliding Window Signals
- “longest substring”
- “smallest window”
- “at most k”
- “contiguous subarray”
Think:
Expand → Shrink → Maintain constraint.
Two Pointer Signals
- “sorted array”
- “palindrome”
- “reverse”
- “pair sum”
Think:
Left pointer + Right pointer.
Binary Search Signals
- “sorted”
- “minimum maximum”
- “search space”
- “answer range 1 to 10^9”
If answer space is monotonic → binary search on answer.
Heap Signals
- “k largest”
- “top k”
- “merge k”
- “median in stream”
Think:
PriorityQueue.
Graph Signals
- “dependencies”
- “shortest path”
- “minimum steps”
- “can we reach”
- “cycle detection”
Think:
BFS, DFS, Dijkstra, Union-Find.
DP Signals
- “maximum/minimum”
- “number of ways”
- “subsequence”
- “partition”
- “can we form”
Ask:
Does smaller state combine to form larger state?
Step 3 — Check Constraint Size
Constraints tell you allowed time complexity.
| n Size | Allowed Complexity |
|---|---|
| n ≤ 20 | Exponential possible |
| n ≤ 100 | O(n³) sometimes ok |
| n ≤ 10⁴ | O(n²) borderline |
| n ≤ 10⁵ | O(n log n) or O(n) |
| n ≤ 10⁶ | O(n) only |
If n = 10^5 → brute force is wrong.
Let constraints eliminate bad approaches.
Step 4 — Identify Relationship Type
Ask:
Is this about:
- Contiguous elements?
- Subsequences?
- Connectivity?
- Ordering?
- Frequency?
- Partitioning?
- Range queries?
- Greedy local choice?
Each maps to pattern families.
Pattern Categories (Complete Map)
1 Array Patterns
- Two Pointers
- Sliding Window
- Prefix Sum
- Kadane
- Monotonic Stack
- Binary Search
- Greedy Sorting
2 String Patterns
- Sliding Window
- Hashing
- KMP
- Trie
- Z Algorithm
- DP on Strings
3 Linked List Patterns
- Reverse
- Fast/Slow pointer
- Cycle detection
- Merge lists
4 Tree Patterns
- DFS (Pre/In/Post)
- BFS (Level order)
- Tree height
- Diameter
- LCA
- Tree DP
5 Graph Patterns
- DFS
- BFS
- Topological Sort
- Dijkstra
- Union-Find
- Multi-source BFS
6 DP Families
Linear DP
One dimension.
2D Grid DP
Paths in matrix.
Subsequence DP
LIS, LCS.
Interval DP
Matrix Chain Multiplication.
Partition DP
Subset sum.
Bitmask DP
Small n state compression.
7 Greedy Patterns
- Interval scheduling
- Jump game
- Activity selection
- Merge intervals
Greedy works when:
Local optimal leads to global optimal.
8 Bit Manipulation
- XOR tricks
- Subset generation
- Masking states
- Counting bits
Signals:
- n ≤ 20
- subset
- power of 2
- parity
Step 5 — Translate Story → Mathematical Model
Example:
“You are climbing stairs…”
Translation: Count number of ways → Fibonacci → DP.
Example:
“Minimum steps to reach target”
Translation: Shortest path → BFS.
Example:
“Merge overlapping intervals”
Translation: Sort + Greedy merge.
Pattern Recognition Drill Framework
When reading a question, ask in order:
- What is input type?
- What is n?
- What is being optimized?
- Is order important?
- Is contiguity required?
- Does smaller solution build larger?
- Is graph implicitly hidden?
- Is answer monotonic?
This mental checklist becomes automatic after practice.
Example Walkthrough
Problem:
“Find longest substring without repeating characters.”
- Input: String
- Keyword: longest substring
- Constraint: contiguous
- No duplicates
Pattern: Sliding Window + HashMap.
Problem:
“Find number of islands.”
- Input: grid
- Connectivity
- Count components
Pattern: DFS or BFS on matrix.
Problem:
“Course Schedule.”
- Dependencies
- Can finish?
Pattern: Topological Sort / Cycle detection in directed graph.
Advanced Pattern Recognition Signals
Multi-Source BFS
Signals:
- Multiple starting points
- Spread outward
- Minimum time
Example pattern: Rotting oranges.
Binary Search on Answer
Signals:
- Minimize maximum
- Maximize minimum
- Feasible check function
Example: Split array largest sum.
Monotonic Stack
Signals:
- Next greater
- Next smaller
- Stock span
- Histogram
Two Heaps
Signals:
- Median
- Streaming input
Union-Find
Signals:
- Dynamic connectivity
- Detect cycle
- Merge components
Common Pattern Recognition Mistakes
- Jumping to DP when greedy works.
- Missing that array is sorted → two pointers possible.
- Not checking constraints.
- Ignoring monotonicity → missing binary search.
- Confusing substring vs subsequence.
How to Master Pattern Recognition
- Solve by category, not randomly.
- After solving, ask:
- What was the pattern?
- What were the signals?
- Maintain a pattern notebook.
- Redo same problem after 2 weeks.
- Practice identifying pattern without coding.
Final Principle
Interview success is not about intelligence.
It is about:
- Structured thinking
- Constraint analysis
- Mapping problem → pattern quickly
- Executing cleanly
When you stop “trying approaches” and start recognizing structures,
you move from average candidate → strong candidate.
Premium Content
Unlock Pattern Index and all premium lessons with a subscription.
From ₹199.99/year — See plans