Leetcode Pattern Recognition Guide
Leetcode Pattern Recognition Guide
(Version 1)
Print this out, keep it by you, but not during your interview 😉
Step 1: Check The Constraints
Small n (≤ 20):
● Brute force approaches are viable
● Backtracking and recursion
● Exponential time complexity (2^n, n!) is acceptable
● Try all possible combinations/permutations
Large n (≥ 10^7):
●❌ No linear time solutions
● O(log n) solutions only
● Binary search
● Mathematical formulas
● O(1) constant time approaches
2D Grid/Matrix:
● DFS/BFS for "islands" problems
● Union Find for connected regions
● Dynamic programming for path problems
● Consider: 4-directional or 8-directional movement
Sorted Array:
● Two pointers technique
● Binary search
● Greedy approach
String:
● Two pointers for palindromes
● Sliding window for substrings
● Trie for word problems
● Stack for parentheses/brackets
Linked List:
● Two pointers (fast/slow)
● Dummy node techniques
● Cycle detection
Step 3: Analyze Output Format
List of Lists (combinations, subsets, paths):
● Backtracking is almost always the answer
● Generate all possibilities
● Use recursion with choice/no-choice pattern
Stack Keywords:
● "Parentheses" or "brackets"
● "Valid expression"
● "Nested structure"
● "Undo operations"
HashMap Keywords:
● "Count frequency"
● "Find duplicates"
● "Anagram"
Trie Keywords:
● "Word search"
● "Word prefixes"
Greedy Keywords:
● "Minimum operations"
Bit Manipulation:
● "XOR" operations
● "Single number" problems
● “Power of 2”
Math/Geometry:
● "Greatest/Least Common Denominator"
● "Prime numbers"
● "Angle calculations"
● “Coordinate”
Game Theory:
● "Optimal strategy"
● "Win/lose scenarios"
● "Minimax"
Sliding Window:
● "Substring" with conditions
● "Subarray" with fixed/variable size
● "Maximum/minimum window"
● "Contains all"