Mastering Data Structures and Algorithms (DSA) patterns is crucial for succeeding in technical interviews at top tech companies like Google, Amazon, Microsoft, Facebook, Apple, and others. This comprehensive guide covers all essential patterns with extensive LeetCode problem links to help you prepare systematically.
Table of Contents
- Two Pointers Pattern
- Sliding Window Pattern
- Fast & Slow Pointers (Cycle Detection)
- Merge Intervals Pattern
- Cyclic Sort Pattern
- In-place Reversal of LinkedList
- Tree Breadth First Search (BFS)
- Tree Depth First Search (DFS)
- Two Heaps Pattern
- Subsets Pattern
- Modified Binary Search
- Bitwise XOR Pattern
- Top K Elements Pattern
- K-way Merge Pattern
- Topological Sort Pattern
- Dynamic Programming Patterns
- Graph Algorithms
- Backtracking Pattern
- Greedy Algorithms
- Stack and Queue Patterns
Two Pointers Pattern
The two pointers pattern is used when you need to find a pair of elements or compare elements from both ends of a sorted array/string.
When to Use:
- Sorted arrays or strings
- Finding pairs that meet certain criteria
- Removing duplicates
- Comparing elements from both ends
LeetCode Problems:
Easy:
- 1. Two Sum
- 125. Valid Palindrome
- 344. Reverse String
- 345. Reverse Vowels of a String
- 349. Intersection of Two Arrays
- 350. Intersection of Two Arrays II
- 392. Is Subsequence
- 977. Squares of a Sorted Array
Medium:
- 15. 3Sum
- 16. 3Sum Closest
- 18. 4Sum
- 75. Sort Colors
- 80. Remove Duplicates from Sorted Array II
- 167. Two Sum II - Input Array Is Sorted
- 259. 3Sum Smaller
- 713. Subarray Product Less Than K
- 845. Longest Mountain in Array
- 923. 3Sum With Multiplicity
- 986. Interval List Intersections
Hard:
Sliding Window Pattern
Used for problems involving subarrays or substrings with a specific condition, typically to find the optimal window size.
When to Use:
- Finding subarrays/substrings with specific properties
- Maximum/minimum window size problems
- Problems with contiguous sequences
LeetCode Problems:
Easy:
Medium:
- 3. Longest Substring Without Repeating Characters
- 76. Minimum Window Substring
- 159. Longest Substring with At Most Two Distinct Characters
- 209. Minimum Size Subarray Sum
- 340. Longest Substring with At Most K Distinct Characters
- 424. Longest Repeating Character Replacement
- 438. Find All Anagrams in a String
- 567. Permutation in String
- 904. Fruit Into Baskets
- 930. Binary Subarrays With Sum
- 992. Subarrays with K Different Integers
- 1004. Max Consecutive Ones III
- 1208. Get Equal Substrings Within Budget
- 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
Hard:
Fast & Slow Pointers (Cycle Detection)
This pattern uses two pointers moving at different speeds to detect cycles in linked lists or find middle elements.
When to Use:
- Detecting cycles in linked lists
- Finding middle of linked list
- Palindrome linked list problems
- Happy number problems
LeetCode Problems:
Easy:
Medium:
Merge Intervals Pattern
Used for problems dealing with overlapping intervals, merging them, or finding gaps.
When to Use:
- Overlapping intervals
- Scheduling problems
- Range problems
- Calendar applications
LeetCode Problems:
Easy:
Medium:
- 56. Merge Intervals
- 57. Insert Interval
- 253. Meeting Rooms II
- 435. Non-overlapping Intervals
- 452. Minimum Number of Arrows to Burst Balloons
- 616. Add Bold Tag in String
- 759. Employee Free Time
- 763. Partition Labels
- 986. Interval List Intersections
- 1272. Remove Interval
Cyclic Sort Pattern
Used when dealing with arrays containing numbers in a given range, typically to find missing or duplicate numbers.
When to Use:
- Arrays with numbers in specific range [1, n] or [0, n-1]
- Finding missing or duplicate numbers
- Placing numbers in their correct positions
LeetCode Problems:
Easy:
Medium:
- 41. First Missing Positive
- 287. Find the Duplicate Number
- 442. Find All Duplicates in an Array
- 645. Set Mismatch
In-place Reversal of LinkedList
Pattern for reversing linked lists without using extra space.
When to Use:
- Reversing linked lists
- Reversing portions of linked lists
- Rotating linked lists
LeetCode Problems:
Easy:
Medium:
Hard:
Tree Breadth First Search (BFS)
Used for level-order traversal of trees and finding shortest paths in unweighted graphs.
When to Use:
- Level-order traversal
- Finding minimum depth/height
- Connecting nodes at same level
- Binary tree problems requiring level-wise processing
LeetCode Problems:
Easy:
- 100. Same Tree
- 101. Symmetric Tree
- 102. Binary Tree Level Order Traversal
- 104. Maximum Depth of Binary Tree
- 107. Binary Tree Level Order Traversal II
- 111. Minimum Depth of Binary Tree
- 637. Average of Levels in Binary Tree
Medium:
- 103. Binary Tree Zigzag Level Order Traversal
- 116. Populating Next Right Pointers in Each Node
- 117. Populating Next Right Pointers in Each Node II
- 199. Binary Tree Right Side View
- 314. Binary Tree Vertical Order Traversal
- 515. Find Largest Value in Each Tree Row
- 623. Add One Row to Tree
- 987. Vertical Order Traversal of a Binary Tree
- 1161. Maximum Level Sum of a Binary Tree
Tree Depth First Search (DFS)
Used for exploring all paths in a tree, finding paths with specific sums, or tree validation.
When to Use:
- Path sum problems
- Tree validation
- Finding all paths
- Maximum depth problems
LeetCode Problems:
Easy:
Medium:
- 98. Validate Binary Search Tree
- 113. Path Sum II
- 129. Sum Root to Leaf Numbers
- 236. Lowest Common Ancestor of a Binary Tree
- 437. Path Sum III
- 450. Delete Node in a BST
- 538. Convert BST to Greater Tree
- 662. Maximum Width of Binary Tree
- 889. Construct Binary Tree from Preorder and Postorder Traversal
- 1008. Construct Binary Search Tree from Preorder Traversal
Hard:
Two Heaps Pattern
Used when you need to find medians, or divide elements into two groups dynamically.
When to Use:
- Finding median in a stream
- Dividing elements into two equal groups
- Sliding window median problems
LeetCode Problems:
Hard:
Medium:
Subsets Pattern
Used for generating all possible subsets, permutations, or combinations.
When to Use:
- Generating all subsets/combinations
- Backtracking problems
- Power set generation
LeetCode Problems:
Medium:
- 78. Subsets
- 90. Subsets II
- 46. Permutations
- 47. Permutations II
- 77. Combinations
- 39. Combination Sum
- 40. Combination Sum II
- 216. Combination Sum III
- 320. Generalized Abbreviation
Modified Binary Search
Binary search variations for rotated arrays, finding peaks, or search in infinite arrays.
When to Use:
- Rotated sorted arrays
- Finding peak elements
- Search in infinite arrays
- Finding elements in bitonic arrays
LeetCode Problems:
Easy:
- 35. Search Insert Position
- 69. Sqrt(x)
- 278. First Bad Version
- 374. Guess Number Higher or Lower
- 704. Binary Search
Medium:
- 33. Search in Rotated Sorted Array
- 34. Find First and Last Position of Element in Sorted Array
- 74. Search a 2D Matrix
- 81. Search in Rotated Sorted Array II
- 153. Find Minimum in Rotated Sorted Array
- 154. Find Minimum in Rotated Sorted Array II
- 162. Find Peak Element
- 240. Search a 2D Matrix II
- 270. Closest Binary Search Tree Value
- 275. H-Index II
- 302. Smallest Rectangle Enclosing Black Pixels
- 367. Valid Perfect Square
- 441. Arranging Coins
Hard:
Bitwise XOR Pattern
Uses XOR properties to solve problems involving duplicates or missing numbers.
When to Use:
- Finding single number in pairs
- Missing number problems
- Complement problems
LeetCode Problems:
Easy:
Medium:
Top K Elements Pattern
Uses heaps to find top K elements, Kth largest/smallest elements.
When to Use:
- Finding top K elements
- Kth largest/smallest problems
- Frequency-based problems
LeetCode Problems:
Easy:
Medium:
- 215. Kth Largest Element in an Array
- 347. Top K Frequent Elements
- 373. Find K Pairs with Smallest Sums
- 378. Kth Smallest Element in a Sorted Matrix
- 451. Sort Characters By Frequency
- 692. Top K Frequent Words
- 973. K Closest Points to Origin
- 1167. Minimum Cost to Connect Sticks
Hard:
- 23. Merge k Sorted Lists
- 218. The Skyline Problem
- 295. Find Median from Data Stream
- 355. Design Twitter
K-way Merge Pattern
Used for merging K sorted arrays or lists.
When to Use:
- Merging multiple sorted arrays/lists
- Finding smallest range covering elements from K lists
LeetCode Problems:
Medium:
- 21. Merge Two Sorted Lists
- 88. Merge Sorted Array
- 373. Find K Pairs with Smallest Sums
- 378. Kth Smallest Element in a Sorted Matrix
Hard:
Topological Sort Pattern
Used for ordering tasks with dependencies, finding course schedules.
When to Use:
- Task scheduling with dependencies
- Course prerequisites
- Build order problems
- Detecting cycles in directed graphs
LeetCode Problems:
Medium:
- 207. Course Schedule
- 210. Course Schedule II
- 269. Alien Dictionary
- 310. Minimum Height Trees
- 444. Sequence Reconstruction
- 630. Course Schedule III
Dynamic Programming Patterns
DP is used for optimization problems with overlapping subproblems and optimal substructure.
Common DP Patterns:
Linear DP:
Easy:
- 70. Climbing Stairs
- 198. House Robber
- 303. Range Sum Query - Immutable
- 392. Is Subsequence
- 746. Min Cost Climbing Stairs
Medium:
- 55. Jump Game
- 45. Jump Game II
- 62. Unique Paths
- 63. Unique Paths II
- 91. Decode Ways
- 139. Word Break
- 213. House Robber II
- 300. Longest Increasing Subsequence
- 322. Coin Change
- 518. Coin Change 2
2D DP:
Medium:
- 64. Minimum Path Sum
- 72. Edit Distance
- 97. Interleaving String
- 115. Distinct Subsequences
- 120. Triangle
- 221. Maximal Square
- 516. Longest Palindromic Subsequence
- 1143. Longest Common Subsequence
Knapsack Pattern:
Hard:
- 10. Regular Expression Matching
- 32. Longest Valid Parentheses
- 44. Wildcard Matching
- 87. Scramble String
- 123. Best Time to Buy and Sell Stock III
- 188. Best Time to Buy and Sell Stock IV
Graph Algorithms
Essential graph traversal and shortest path algorithms.
DFS/BFS on Graphs:
Medium:
- 133. Clone Graph
- 200. Number of Islands
- 417. Pacific Atlantic Water Flow
- 695. Max Area of Island
- 733. Flood Fill
- 994. Rotting Oranges
- 1091. Shortest Path in Binary Matrix
Union Find:
Medium:
- 128. Longest Consecutive Sequence
- 200. Number of Islands
- 547. Number of Provinces
- 684. Redundant Connection
- 721. Accounts Merge
Shortest Path:
Medium:
Backtracking Pattern
Used for exploring all possible solutions and choosing the optimal one.
When to Use:
- Generating permutations/combinations
- Solving puzzles (N-Queens, Sudoku)
- Path finding with constraints
LeetCode Problems:
Medium:
- 17. Letter Combinations of a Phone Number
- 22. Generate Parentheses
- 39. Combination Sum
- 46. Permutations
- 78. Subsets
- 79. Word Search
- 93. Restore IP Addresses
- 131. Palindrome Partitioning
Hard:
Greedy Algorithms
Greedy algorithms make locally optimal choices at each step, hoping to find a global optimum.
When to Use:
- Optimization problems where greedy choice leads to optimal solution
- Activity selection problems
- Minimum spanning tree
- Huffman coding type problems
LeetCode Problems:
Easy:
Medium:
- 11. Container With Most Water
- 45. Jump Game II
- 55. Jump Game
- 122. Best Time to Buy and Sell Stock II
- 134. Gas Station
- 135. Candy
- 179. Largest Number
- 253. Meeting Rooms II
- 334. Increasing Triplet Subsequence
- 376. Wiggle Subsequence
- 402. Remove K Digits
- 406. Queue Reconstruction by Height
- 435. Non-overlapping Intervals
- 452. Minimum Number of Arrows to Burst Balloons
- 621. Task Scheduler
- 649. Dota2 Senate
- 678. Valid Parenthesis String
- 714. Best Time to Buy and Sell Stock with Transaction Fee
- 738. Monotone Increasing Digits
- 763. Partition Labels
- 767. Reorganize String
- 846. Hand of Straights
- 870. Advantage Shuffle
- 881. Boats to Save People
- 948. Bag of Tokens
- 955. Delete Columns to Make Sorted II
- 1029. Two City Scheduling
- 1054. Distant Barcodes
- 1221. Split a String in Balanced Strings
- 1253. Reconstruct a 2-Row Binary Matrix
- 1282. Group the People Given the Group Size They Belong To
- 1338. Reduce Array Size to The Half
- 1353. Maximum Number of Events That Can Be Attended
Hard:
- 42. Trapping Rain Water
- 84. Largest Rectangle in Histogram
- 316. Remove Duplicate Letters
- 321. Create Maximum Number
- 330. Patching Array
- 502. IPO
- 630. Course Schedule III
- 757. Set Intersection Size At Least Two
- 857. Minimum Cost to Hire K Workers
Stack and Queue Patterns
Stacks and queues are fundamental data structures used in many algorithmic patterns.
Stack Patterns:
Easy:
- 20. Valid Parentheses
- 155. Min Stack
- 225. Implement Stack using Queues
- 232. Implement Queue using Stacks
- 496. Next Greater Element I
Medium:
- 71. Simplify Path
- 150. Evaluate Reverse Polish Notation
- 173. Binary Search Tree Iterator
- 394. Decode String
- 402. Remove K Digits
- 456. 132 Pattern
- 503. Next Greater Element II
- 739. Daily Temperatures
- 853. Car Fleet
- 901. Online Stock Span
- 946. Validate Stack Sequences
- 1019. Next Greater Node In Linked List
- 1047. Remove All Adjacent Duplicates In String
- 1209. Remove All Adjacent Duplicates in String II
- 1249. Minimum Remove to Make Valid Parentheses
- 1472. Design Browser History
- 1497. Check If Array Pairs Are Divisible by k
Hard:
- 42. Trapping Rain Water
- 84. Largest Rectangle in Histogram
- 85. Maximal Rectangle
- 224. Basic Calculator
- 227. Basic Calculator II
- 316. Remove Duplicate Letters
- 321. Create Maximum Number
- 636. Exclusive Time of Functions
- 726. Number of Atoms
- 895. Maximum Frequency Stack
Queue Patterns:
Easy:
Medium:
- 207. Course Schedule
- 210. Course Schedule II
- 279. Perfect Squares
- 346. Moving Average from Data Stream
- 362. Design Hit Counter
- 622. Design Circular Queue
- 641. Design Circular Deque
Advanced Patterns and Miscellaneous
Trie (Prefix Tree):
Medium:
- 208. Implement Trie (Prefix Tree)
- 211. Design Add and Search Words Data Structure
- 677. Map Sum Pairs
- 720. Longest Word in Dictionary
Hard:
- 212. Word Search II
- 421. Maximum XOR of Two Numbers in an Array
- 440. K-th Smallest in Lexicographical Order
- 472. Concatenated Words
- 648. Replace Words
- 676. Implement Magic Dictionary
- 745. Prefix and Suffix Search
Segment Tree / Binary Indexed Tree:
Medium:
Hard:
- 218. The Skyline Problem
- 308. Range Sum Query 2D - Mutable
- 327. Count of Range Sum
- 493. Reverse Pairs
Design Patterns:
Easy:
- 146. LRU Cache
- 155. Min Stack
- 170. Two Sum III - Data structure design
- 173. Binary Search Tree Iterator
Medium:
- 146. LRU Cache
- 208. Implement Trie (Prefix Tree)
- 211. Design Add and Search Words Data Structure
- 284. Peeking Iterator
- 295. Find Median from Data Stream
- 297. Serialize and Deserialize Binary Tree
- 348. Design Tic-Tac-Toe
- 355. Design Twitter
- 380. Insert Delete GetRandom O(1)
- 381. Insert Delete GetRandom O(1) - Duplicates allowed
- 384. Shuffle an Array
- 460. LFU Cache
- 535. Encode and Decode TinyURL
- 588. Design In-Memory File System
- 622. Design Circular Queue
- 641. Design Circular Deque
- 703. Kth Largest Element in a Stream
- 705. Design HashSet
- 706. Design HashMap
- 707. Design Linked List
- 716. Max Stack
Hard:
Math and Number Theory:
Easy:
- 7. Reverse Integer
- 9. Palindrome Number
- 13. Roman to Integer
- 66. Plus One
- 168. Excel Sheet Column Title
- 171. Excel Sheet Column Number
- 202. Happy Number
- 204. Count Primes
- 258. Add Digits
- 263. Ugly Number
- 326. Power of Three
- 342. Power of Four
- 367. Valid Perfect Square
- 414. Third Maximum Number
Medium:
- 2. Add Two Numbers
- 8. String to Integer (atoi)
- 12. Integer to Roman
- 29. Divide Two Integers
- 43. Multiply Strings
- 50. Pow(x, n)
- 60. Permutation Sequence
- 166. Fraction to Recurring Decimal
- 172. Factorial Trailing Zeroes
- 202. Happy Number
- 223. Rectangle Area
- 264. Ugly Number II
- 279. Perfect Squares
- 319. Bulb Switcher
- 365. Water and Jug Problem
- 372. Super Pow
- 396. Rotate Function
- 400. Nth Digit
- 441. Arranging Coins
- 453. Minimum Moves to Equal Array Elements
- 462. Minimum Moves to Equal Array Elements II
Company-Specific Pattern Focus
Google Favorites:
- Dynamic Programming (All variations)
- Graph algorithms (BFS/DFS, Shortest Path)
- Tree problems (especially BST)
- System Design related problems
- String manipulation and parsing
Amazon Favorites:
- Two pointers and sliding window
- Tree and graph traversals
- Dynamic Programming
- Heap/Priority Queue problems
- Design problems
Microsoft Favorites:
- Dynamic Programming
- Graph algorithms
- Tree problems
- String problems
- Design problems
- Backtracking
Facebook (Meta) Favorites:
- Tree and graph problems
- Dynamic Programming
- String manipulation
- Two pointers
- BFS/DFS problems
Apple Favorites:
- Tree problems
- Dynamic Programming
- String problems
- Array problems
- Design problems
Study Strategy and Tips
Phase 1: Foundation (Weeks 1-4)
Focus on basic patterns:
- Two Pointers
- Sliding Window
- Binary Search
- Tree BFS/DFS
- Basic DP
Phase 2: Intermediate (Weeks 5-8)
- Advanced DP patterns
- Graph algorithms
- Heaps and advanced data structures
- Backtracking
- Greedy algorithms
Phase 3: Advanced (Weeks 9-12)
- Complex DP problems
- Advanced graph algorithms
- System design problems
- Company-specific practice
- Mock interviews
Practice Tips:
- Pattern Recognition: Focus on identifying patterns rather than memorizing solutions
- Time Management: Practice with time constraints (45 minutes per problem max)
- Code Quality: Write clean, readable code with proper variable names
- Edge Cases: Always consider edge cases and handle them explicitly
- Communication: Practice explaining your approach clearly
- Multiple Solutions: Try to find multiple approaches for the same problem
- Complexity Analysis: Always analyze time and space complexity
- Mock Interviews: Practice with peers or use platforms like Pramp, InterviewBit
Problem Selection Strategy:
- Start with Easy: Build confidence with easier problems
- Pattern Grouping: Solve 5-10 problems of the same pattern consecutively
- Mixed Practice: After mastering patterns, do random problem solving
- Company Focus: In final weeks, focus on company-specific problems
- Review: Regularly review solved problems to maintain familiarity
Common Mistakes to Avoid:
- Jumping to hard problems too early
- Not understanding the underlying pattern
- Focusing only on getting accepted solution
- Not practicing with time constraints
- Ignoring system design aspects
- Not communicating during problem solving
- Poor time management during interviews
Conclusion
Mastering these DSA patterns is essential for technical interview success. The key is consistent practice, pattern recognition, and understanding when and how to apply each technique. Focus on quality over quantity - it's better to deeply understand 200 problems across all patterns than to solve 500 problems without pattern recognition.
Remember that interviews are not just about getting the right answer, but also about demonstrating your problem-solving approach, code quality, and communication skills. Practice explaining your thought process clearly and writing clean, efficient code.
Good luck with your interview preparation! Keep practicing consistently, and you'll definitely see improvement in your problem-solving abilities.
This guide contains 300+ carefully curated LeetCode problems across all major DSA patterns. Bookmark this page and use it as your comprehensive study guide for technical interviews at top tech companies.
Top comments (0)