DSA patterns
Interview problems repeat a small set of ideas. Learn the 114 patterns below and most new problems turn into "this is a sliding window" or "this is a BFS on a grid". Each pattern lists its practice problems, easiest first.
Arrays & Hashing
Two Sum 3
Moore's Voting Algorithm 2
Kadane's Algorithm 6
- Kadane's Algorithm - Simple
- Find minimum subarray sum
- Kadane's Algo - Circular Array - No Extra Space
- Gas Station
- Maximum Product Subarray
Show 1 moreShow less
Double Reversal Trick 2
Two Pointer 34
- Two Sum II - Input Array Is Sorted
- Valid Triangle Number
- Container With Most Water
- Trapping Rain Water
- Valid Palindrome II
Show 29 moreShow less
- Valid Word Abbreviation
- Merge Sorted Array
- String Compression
- Expressive Words
- Long Pressed Name
- Valid Palindrome
- Backspace String Compare
- Intersection of Two Arrays
- Longest Mountain in Array
- Move Zeroes
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array
- Sort Transformed Array
- Squares of a Sorted Array
- Summary Ranges
- One Edit Distance
- Shortest Word Distance
- Valid Mountain Array
- Boats to Save People
- Shortest Subarray to be Removed to Make Array Sorted
- 4Sum
- Break a Palindrome
- Divide Players Into Teams of Equal Skill
- Is Subsequence
- Maximize Distance to Closest Person
- Reverse Only Letters
- Reverse String
- Reverse Words in a String III
- Sort Array By Parity
Right to Left Traversal 12
In-place Transformations 5
Merge Sort Like Approach 4
Dutch National Flag 1
Prefix Sum Strategy 11
Sliding Window · Fixed 11
Sliding Window · Variable 12
Sliding Window · Hash Map 6
- Number of Substrings Containing All Three Characters
- Minimum Window Substring
- Longest Substring with At Most K Distinct Characters
- Contains Duplicate III
- Maximum Number of Occurrences of a Substring
Show 1 moreShow less
Hashing 18
- Group Anagrams
- First Unique Character in a String
- Number of Matching Subsequences
- Group Shifted Strings
- Max Points on a Line
Show 13 moreShow less
- Valid Anagram
- Invalid Transactions
- Isomorphic Strings
- Number of Black Blocks
- Palindrome Pairs
- Shortest Uncommon Substring in an Array
- Strings Differ by One Character
- Fraction to Recurring Decimal
- Count Elements With Maximum Frequency
- Find The Least Frequent Digit
- Minimum Number of Steps to Make Two Strings Anagram
- Task Scheduler II
- Valid Sudoku
Matrix Traversal 4
Elimination Recurrence 1
Greedy 26
Show 21 moreShow less
- Guess the Word
- Make Lexicographically Smallest Array by Swapping Elements
- Minimum Domino Rotations For Equal Row
- Reorganize String
- Task Scheduler
- Minimum Number of Taps to Open to Water a Garden
- Minimize Rounding Error to Meet Target
- Largest Number
- Maximum Product of Three Numbers
- Advantage Shuffle
- Maximum Distance Between Unequal Words in Array I
- Minimum Absolute Difference
- Minimum Number of Coins to be Added
- Minimum Number of Operations to Make Arrays Similar
- Minimum Operations to Make Array Equal II
- Transform Array to All Equal Elements
- Minimum Total Cost to Make Arrays Unequal
- Queue Reconstruction by Height
- Two City Scheduling
- Maximum Units on a Truck
- Partitioning Into Minimum Number Of Deci-Binary Numbers
Hash Set Lookup 4
Simulation 12
Contribution Technique 2
String Matching (KMP) 2
Math / Number Theory 10
Candidate Generation 2
Fenwick Tree (BIT) 1
Sorting 1
Stack
Stack Basics 20
Show 15 moreShow less
- Evaluate Reverse Polish Notation
- Basic Calculator
- Decode String
- Implement Queue using Stacks
- Basic Calculator II
- Remove All Adjacent Duplicates in String II
- Minimum Remove to Make Valid Parentheses
- Simplify Path
- Basic Calculator IV
- Exclusive Time of Functions
- Mini Parser
- Implement Stack using Queues
- Maximum Score From Removing Substrings
- Basic Calculator III
- Count Collisions on a Road
Monotonic Stack
Monotonic Stack 15
- Next Greater Element I
- Daily Temperatures
- Next Greater Element II
- Largest Rectangle in Histogram
- Sliding Window Maximum
Show 10 moreShow less
Linked List
Dummy Node Pattern 8
- Remove Linked List Elements
- Delete Nodes From Linked List Present in Array
- Merge Two Sorted Lists
- Rotate List
- Swap Nodes in Pairs
Show 3 moreShow less
Slow Fast Pointers 6
Show 1 moreShow less
Front Back Pointer 1
Front Middle Back Pointer 4
Binary Search
Bisect 11
Unique 1-D Binary Search 3
Rotated Array 5
2D Binary Search + Step Search 4
Search on Answer Range 4
Binary Search on Answer Space 14
Binary Search on Partition 1
Recursion & Backtracking
Basic Recursion 7
- Factorial of a number
- Fibonacci Number
- Binary Tree Inorder Traversal (Recursive)
- Basic Backtracking Template
- Binary Tree Paths
Show 2 moreShow less
2D Matrix 1
Pick / Not-Pick 11
2D Matrix Backtracking 3
Choice per Position 8
- Letter Combinations of a Phone Number
- Confusing Number II
- Strobogrammatic Number II
- Palindrome Partitioning
- Brace Expansion
Show 3 moreShow less
Divide and Conquer 2
Digit-by-Digit Construction 1
Meet in the Middle 1
Trees & BST
Recursion 13
Backtracking 3
Traversals 4
Views 7
- Top View of Binary Tree
- Bottom View of Binary Tree
- Left View of Binary Tree
- Binary Tree Right Side View
- Binary Tree Vertical Order Traversal
Show 2 moreShow less
BST Basics 10
Postorder + Multiple Return Values 11
LCA 5
Child + Ancestor Handling 4
Reverse Inorder Traversal 2
Tree Serialization / Deserialization 5
Tree to Lists and vice-versa 2
Miscellaneous BST Questions 5
Heaps
Top K / Kth Largest / Smallest 11
Composite comparator 4
Merge K Sorted 4
Intervals
Basics 14
Line Sweep 6
- Meeting Rooms II
- Car Pooling
- Employee Free Time
- Divide Intervals Into Minimum Number of Groups
- Rectangle Area II
Show 1 moreShow less
Segment Tree 1
Tries
Basics 6
- Implement Trie (Prefix Tree)
- Implement Trie ll
- Design In-Memory File System
- Design Search Autocomplete System
- Design Add and Search Words Data Structure
Show 1 moreShow less
Trie + Backtracking 1
Bitwise Trie 1
Graphs
BFS / DFS Basics 9
Matrix Graphs 6
- Flood Fill
- Maximum Number of Points From Grid Queries
- Contain Virus
- Number of Distinct Islands
- Max Area of Island
Show 1 moreShow less
Connected Components 11
BFS for shortest path 12
DFS - Complement Trick 1
Multi source BFS 7
- Rotting Oranges
- 01 Matrix
- Pacific Atlantic Water Flow
- Escape the Spreading Fire
- Find the Safest Path in a Grid
Show 2 moreShow less
Cycle Detection 5
Topological Sort 11
Dijkstra 6
- Network Delay Time
- Find the City With the Smallest Number of Neighbors at a Threshold Distance
- Cheapest Flights Within K Stops
- Swim in Rising Water
- Minimum Time to Visit a Cell In a Grid
Show 1 moreShow less
Eulerian Path (Hierholzer) 1
Candidate Elimination 1
Minimum Spanning Tree 1
Degree Counting 2
Dynamic Programming
1D DP 20
Show 15 moreShow less
Alternate Selection / Pick / Not pick 2
2D DP 11
2D DP - max/min of last row 3
State Machine DP - Stock Problems 5
Knapsack 10
Strings 15
- Edit Distance
- Longest Common Subsequence
- Longest Palindromic Subsequence
- Minimum Insertion Steps to Make a String Palindrome
- Longest Common Substring
Show 10 moreShow less
Interval DP 5
Bitmask DP 5
Combinatorics 1
Bit Manipulation
Basics 8
- Decimal to binary
- Convert Binary Number in a Linked List to Integer
- K-th Bit is Set or Not
- Odd or Even
- Convert to Base -2
Show 3 moreShow less
Tricks to remember 7
- Number of 1 Bits
- Set the rightmost unset bit
- XOR of 1 to n Numbers
- Minimum Operations to Reduce an Integer to 0
- IP to CIDR