I know how tough job hunting is—I applied to over 3,000 jobs before landing my internship and full-time role. That’s why I share all my resources for free, hoping even 1% helps your career. I work on this alone with no budget, so if you find this article helpful, please consider supporting me.
By making a donation through buymeacoffee
or subscribing to my Youtube page.
Connect with me on LinkedIn
Interview Prep + Coaching with me : Link
Also, you can use Final Round AI to enhance your job search by having AI build your resume, apply on your behalf and help with mock interviews. Link
Important Tricks and Pattern Recognition Techniques in DSA
Step 1 – Start with Constraints
🔹 Small Input Size (n ≤ 20)
PROBLEM EXAMPLES :
Subsets (78) → Use recursion (take/not-take).
Trick: Think in terms of decision tree.
Permutations (46) → Swap elements during recursion.
Trick: Fix one element at a time, recurse on the rest.
Combination Sum (39) → DFS/backtracking.
Trick: Use “start index” to avoid duplicates.
Word Search (79) → DFS with visited set.
Trick: Backtrack by marking/unmarking cell.
Letter Combinations of a Phone Number (17)
Trick: Map digits to characters, DFS or queue BFS.
🔹 Moderate Input Size (10³ ≤ n ≤ 10⁶)
Two Sum (1) → HashMap in O(n).
Trick: Store complement while scanning.
Best Time to Buy and Sell Stock (121)
Trick: Track running min, update max profit.
Merge Intervals (56) → Sort + Greedy merge.
Trick: Always merge into last interval if overlapping.
Longest Substring Without Repeating Characters (3) → Sliding window.
Trick: Move left pointer only when duplicate found.
Product of Array Except Self (238)
Trick: Prefix product * Suffix product.
Kth Largest Element in an Array (215) → Heap/Quickselect.
Trick: Quickselect = avg O(n), Heap = O(n log k).
🔹 Very Large Input Size (n ≥ 10⁷)
Sqrt(x) (69) → Binary search.
Pow(x, n) (50) → Fast power recursion.
First Bad Version (278) → Binary search.
Find Peak Element (162) → Binary search on slope.
Median of Two Sorted Arrays (4)
Trick: Binary search partition approach (O(log(min(m,n)))).
Step 2 – Decode Input Format
🌳 Trees
Tricks:
Use recursion for DFS, queue for BFS.
For Least Common Ancestor in BST, check value ranges instead of full traversal.
🔗 Graphs
Number of Islands (200) → DFS/BFS.
Clone Graph (133) → DFS + HashMap.
Course Schedule (207) → Toposort/DFS cycle detect.
Redundant Connection (684) → Union-Find.
Graph Valid Tree (261)
Tricks:
Union-Find = best for connectivity.
Cycle detection = DFS with state (visiting, visited).
🟦 2D Grids
Word Search (79)
Surrounded Regions (130)
Rotting Oranges (994)
Unique Paths (62)
Minimum Path Sum (64)
Tricks:
BFS for shortest path.
DP for counting paths.
📊 Sorted Arrays
Binary Search (704)
Search in Rotated Sorted Array (33)
Find First and Last Position (34)
Merge Sorted Array (88)
🔤 Strings
Valid Palindrome (125)
Longest Palindromic Substring (5)
Valid Parentheses (20)
Group Anagrams (49)
Implement Trie (208)
🔗 Linked Lists
• Fast/slow pointers for cycle detection.
• Dummy node tricks for cleaner code.
Reverse Linked List (206)
Linked List Cycle (141)
Merge Two Sorted Lists (21)
Add Two Numbers (2)
LRU Cache (146)
Step 3 – Output Types
List of Lists → Subsets (78), N-Queens (51).
Single Value → Max Subarray (53), Coin Change (322).
Modified Structure → Move Zeroes (283).
Ordered Output → Top K Frequent Elements (347), Course Schedule II (210).
Step 4 – Keyword Triggers
DP → Longest Increasing Subsequence (300), Edit Distance (72).
Two Pointers → Container With Most Water (11), Trapping Rain Water (42).
Heap → Merge K Sorted Lists (23).
Stack → Daily Temperatures (739), Min Stack (155).
Union-Find → Connected Components (323).
Bit Manipulation → Single Number (136).
Sliding Window → Minimum Window Substring (76).
🔥 Extra Tricks (Meta-level):
If the problem says “maximum/minimum” → Think DP or Greedy.
If it says “kth” → Think Heap or Quickselect.
If it says “reachability/connectivity” → Think BFS/DFS/Union-Find.
If input size is huge (≥10⁷) → Avoid O(n); think math, binary search, or precomputation.
Prefix/Suffix arrays → Magic trick for O(n) range-sum and product problems.
Monotonic Stack → For "next greater element" style.
Step 1 – Start with the Constraints
Small Input Size (n ≤ 20)
• Brute force is fine here.
• Backtracking and recursive enumeration shine.
• Exponential complexity (2n, n!) is acceptable.
• Explore all possible combinations or permutations without fear.
Moderate Input Size (103 ≤ n ≤ 106)
• Skip brute force — it’ll be too slow.
• Aim for O(n) or O(n log n) solutions.
• Use techniques like two pointers, greedy algorithms, heaps, or dynamic programming.
Very Large Input Size (n ≥ 107)
• Even O(n) might be too slow.
• Target O(log n) or O(1) solutions.
• Consider binary search, mathematical shortcuts, and precomputed formulas.
Step 2 – Decode the Input Format
Trees (General / Binary / BST)
• Use DFS (all paths, recursive, preorder/inorder/postorder) or BFS (level-order, shortest
path in unweighted trees).
• Pay attention to parent-child relationships and special tree properties.
Graphs (Nodes + Edges)
• BFS → shortest path.
• DFS → connected components.
• Union-Find → connectivity checks, grouping problems.
• Topological sort → dependency resolution.
2D Grids / Matrices
• DFS/BFS for “islands” style problems.
• Union-Find for connected regions.
• DP for pathfinding and counting problems.
• Mind the movement rules (4-dir, 8-dir).
Sorted Arrays
• Binary search.
• Two pointers.
• Greedy choices.
Strings
• Two pointers → palindrome checks.
• Sliding window → substring problems.
• Trie → prefix/word problems.
• Stack → bracket/parentheses validation.
Linked Lists
• Fast/slow pointers for cycle detection.
• Dummy node tricks for cleaner code.
Step 3 – Understand the Output Type
List of Lists (paths, subsets, combinations)
• Backtracking is your friend.
• Generate all choices using recursion (Take, Not Take).
Single Value (max profit, min cost, number of ways)
• Dynamic Programming for optimization.
• Greedy for quick optimal picks.
• Math-based counting when applicable.
Modified Structure (in-place edits)
• Two pointers for space-efficient changes.
Ordered Output (sorted tasks, ranked items)
• Custom sorting.
• Topological sorting.
• Heaps for maintaining order dynamically.
Step 4 – Keyword Triggers for Patterns
• Dynamic Programming → “Number of ways”, “Max/Min sum”, “Can you reach”,
“Longest/Shortest subsequence”, “Optimal solution”.
• Two Pointers → “Palindrome”, “Sorted array”, “Target sum”, “Remove duplicates”.
• Heap → “K largest/smallest”, “Top K elements”, “Median”, “Priority queue”.
• Stack → “Parentheses/brackets”, “Valid expression”, “Nested structure”,
“Undo/Redo”.
• Monotonic Stack → “Next greater/smaller element”.
• HashMap → “Frequency count”, “Find duplicates”, “Anagram check”.
• Trie → “Word search”, “Prefix matching”.
• Greedy → “Minimum operations”.
• Union-Find → “Connected components”, “Number of groups”.
• Binary Search → “Kth element”, “Search in sorted data”, “Minimize maximum”,
“First/last occurrence”.
• Bit Manipulation → “XOR trick”, “Single number”, “Power of 2 check”.
• Math/Geometry → “GCD/LCM”, “Primes”, “Angles”, “Coordinates”.
• Game Theory → “Optimal strategy”, “Win/Lose prediction”, “Minimax”.
• Sliding Window → “Substring match”, “Fixed/variable size subarray”, “Max/Min
window”.