One of the biggest struggles in mastering Data Structures and Algorithms (DSA) is not writing code — it’s recognizing patterns in problems. Once you can spot the underlying pattern, solving the problem becomes much easier.
This guide is a cheat sheet + strategy blog to help you identify DSA problem types and choose the right approach.
🔑 General Rules to Spot Patterns
1. If the input array is sorted →
- Try Binary Search
- Or Two Pointers
2. If asked for all permutations/subsets →
- Use Backtracking
3. If the problem involves a Tree →
- Use DFS or BFS
4. If the problem involves a Graph →
- Again, DFS or BFS (with variations like Union-Find, Topological Sort, etc.)
5. If the problem involves Linked Lists →
- Think of Two Pointers (slow/fast pointer tricks)
6. If recursion is not allowed →
- Use a Stack to simulate recursion
7. If in-place solution is required →
- Use swapping, or
- Store extra info inside existing pointers
8. If asked for maximum/minimum subarray/subset/option →
- Use Dynamic Programming (Kadane’s, Knapsack, etc.)
9. If asked for top/least K items →
- Use Heap or QuickSelect
10. If asked for common strings/words →
* Use **HashMap** or **Trie**
11. Else:
* Use **Map/Set** for O(1) lookup with O(n) space
* Or **Sort** input for O(n log n) time and O(1) space
🧠 The Two-Pointer Pattern
🔍 How to Identify
- Finding pairs, triplets, or subarrays.
- Problem mentions sorted array.
- Problem wants in-place solution (O(1) space).
- Need O(n) or O(n log n), brute force O(n²) is too slow.
🚀 Approaches
- Opposite Ends Approach
-
i = 0
,j = n-1
- Move inward based on condition 👉 Examples: Two Sum (sorted), Container With Most Water, Valid Palindrome
- Same Direction Approach (Slow/Fast Pointers)
- Both start at 0, fast pointer explores, slow pointer lags behind 👉 Examples: Remove Duplicates, Linked List Cycle, Sliding Window variations
💡 Tip: When modifying in-place, slow pointer usually marks “where to write”.
📝 Pointer Movement Table
Condition | Action |
---|---|
Sum too small | Move left++ |
Sum too big | Move right-- |
Found valid pair | Move both inward |
Skip duplicates | Move pointer at dup |
⚡ The Sliding Window Pattern
🔍 How to Identify
-
Problems asking for subarray/substring with constraints:
- Maximum/minimum sum/length/count
- K distinct elements
- Character replacements
🚀 Strategies
Expanding Window (for max problems):
Grow window until condition is invalid, then shrink.Shrinking Window (for min problems):
Shrink while condition holds, track min length.
📝 Quick Rules
- Use if inside for loop when at most one adjustment is needed per step (soft constraint).
- Use while loop when multiple shrinks may be required (strict constraint).
👉 Examples: Longest Substring Without Repeating Characters, Minimum Window Substring
🌳 Tree & Graph Patterns
- DFS → when recursion or full exploration is needed.
- BFS → when solving shortest path (unweighted) or level-order.
- Union-Find → when dealing with connectivity/cycles.
- Topological Sort → for dependency/ordering problems.
👉 Examples:
- Tree Traversals (Inorder/Preorder/Postorder)
- Shortest Path in Graph (BFS, Dijkstra)
- Detecting Cycles (DFS, Union-Find)
🧮 Dynamic Programming (DP)
🔍 How to Identify
- Problem asks for minimum/maximum something.
- Overlapping subproblems appear.
- Brute force recursion seems exponential.
🚀 Common DP Problems
- Kadane’s Algorithm (max subarray sum)
- Knapsack variations
- Fibonacci/Climbing Stairs
- Longest Common Subsequence / Substring
👉 Tip: Always try recursion first → then memoize → then bottom-up.
🏗️ Heaps and Priority Queues
- Use when asked for top K elements, running median, or minimum/maximum stream values.
- Heap provides O(log n) insertion and O(1) top element.
👉 Examples: Kth Largest Element, Merge K Sorted Lists, Sliding Window Maximum
🔡 Strings & Hashing
- HashMap / Set → when you need to check existence/count (anagrams, duplicates).
- Trie → when asked for prefix/suffix queries, autocomplete, or dictionary problems.
👉 Examples: Group Anagrams, Word Search II, Longest Prefix
🧭 Checklist for Any DSA Question
When you see a new problem, ask:
- Is the input sorted? → Try binary search / two pointers.
- Is it about subarray/substring? → Try sliding window.
- Is it about permutations/subsets? → Try backtracking.
- Is it about a tree/graph? → Try DFS/BFS.
- Is it about max/min result? → Try DP.
- Is it about Kth element/top items? → Try heap/QuickSelect.
- Is it about strings/prefixes? → Try HashMap/Trie.
- Else → Try Map/Set or Sorting.
🎯 Final Tip
👉 Brute force first, optimize later.
Most optimizations are about recognizing the pattern and then replacing nested loops or recursion with the right technique.
By building this pattern recognition skill, you’ll start to “see through” DSA problems instead of brute-forcing every new one. That’s the mindset of a strong problem solver.
Top comments (0)