DSA Patterns
Learn each pattern once, then solve problems right here with a built-in JavaScript and Python runner.
1. Two Pointers
Walk two indices toward (or along) each other to exploit sorted order or symmetry.
- Valid Palindrome (Easy)
- Valid Palindrome II (Easy)
- Two Sum II – Sorted Input (Medium)
- 3Sum (Medium)
- 3Sum Closest (Medium)
- 4Sum (Medium)
- Container With Most Water (Medium)
- Sort Colors (Medium)
- Boats to Save People (Medium)
- Move Zeroes (Easy)
- Remove Duplicates from Sorted Array (Easy)
- Trapping Rain Water (Hard)
- Reverse String (Easy)
- Squares of a Sorted Array (Easy)
- Merge Sorted Array (Easy)
- Reverse Words in a String (Medium)
- Rotate Array (Medium)
- Next Permutation (Medium)
2. Sliding Window
Maintain a contiguous window and its state incrementally instead of recomputing.
- Best Time to Buy and Sell Stock (Easy)
- Maximum Average Subarray I (Easy)
- Longest Substring Without Repeating Characters (Medium)
- Longest Repeating Character Replacement (Medium)
- Permutation in String (Medium)
- Find All Anagrams in a String (Medium)
- Minimum Size Subarray Sum (Medium)
- Subarray Product Less Than K (Medium)
- Fruit Into Baskets (Medium)
- Sliding Window Maximum (Hard)
- Minimum Window Substring (Hard)
- Max Consecutive Ones III (Medium)
- Longest Subarray of 1's After Deleting One Element (Medium)
- Get Equal Substrings Within Budget (Medium)
- Grumpy Bookstore Owner (Medium)
- Number of Substrings Containing All Three Characters (Medium)
3. Prefix Sum
Precompute running totals to answer any range question in O(1).
- Find Pivot Index (Easy)
- Range Sum Query - Immutable (Easy)
- Product of Array Except Self (Medium)
- Subarray Sum Equals K (Medium)
- Contiguous Array (Medium)
- Subarray Sums Divisible by K (Medium)
- Continuous Subarray Sum (Medium)
- Range Sum Query 2D - Immutable (Medium)
- Running Sum of 1d Array (Easy)
- Find the Highest Altitude (Easy)
- Minimum Value to Get Positive Step by Step Sum (Easy)
- Corporate Flight Bookings (Medium)
- Number of Ways to Split Array (Medium)
4. Hashing
Trade memory for speed: hash maps and sets turn O(n²) searches into O(n) lookups.
- Two Sum (Easy)
- Contains Duplicate (Easy)
- Valid Anagram (Easy)
- Ransom Note (Easy)
- Isomorphic Strings (Easy)
- Group Anagrams (Medium)
- Top K Frequent Elements (Medium)
- Longest Consecutive Sequence (Medium)
- Majority Element (Easy)
- First Unique Character in a String (Easy)
- Word Pattern (Easy)
- Longest Palindrome (Easy)
- Find All Numbers Disappeared in an Array (Easy)
- 4Sum II (Medium)
- Insert Delete GetRandom O(1) (Medium)
- Majority Element II (Medium)
- Encode and Decode Strings (Medium)
5. Stack & Monotonic Stack
LIFO for matching and nesting; monotonic stacks for 'next greater' questions.
- Valid Parentheses (Easy)
- Min Stack (Medium)
- Evaluate Reverse Polish Notation (Medium)
- Decode String (Medium)
- Asteroid Collision (Medium)
- Next Greater Element I (Easy)
- Next Greater Element II (Medium)
- Daily Temperatures (Medium)
- Online Stock Span (Medium)
- Car Fleet (Medium)
- Largest Rectangle in Histogram (Hard)
- Backspace String Compare (Easy)
- Simplify Path (Medium)
- Remove K Digits (Medium)
- Minimum Remove to Make Valid Parentheses (Medium)
- Basic Calculator II (Medium)
- Sum of Subarray Minimums (Medium)
- Basic Calculator (Hard)
- Maximal Rectangle (Hard)
- Maximum Frequency Stack (Hard)
6. Binary Search
Halve the search space using a monotonic condition — on arrays or on the answer itself.
- Binary Search (Easy)
- Search Insert Position (Easy)
- Search a 2D Matrix (Medium)
- Find First and Last Position of Element in Sorted Array (Medium)
- Find Peak Element (Medium)
- Koko Eating Bananas (Medium)
- Capacity To Ship Packages Within D Days (Medium)
- Single Element in a Sorted Array (Medium)
- Find Minimum in Rotated Sorted Array (Medium)
- Search in Rotated Sorted Array (Medium)
- Time Based Key-Value Store (Medium)
- Split Array Largest Sum (Hard)
- Median of Two Sorted Arrays (Hard)
- First Bad Version (Easy)
- Sqrt(x) (Easy)
- Valid Perfect Square (Easy)
- Find K Closest Elements (Medium)
- Search in Rotated Sorted Array II (Medium)
- Kth Smallest Element in a Sorted Matrix (Medium)
- Minimum Number of Days to Make m Bouquets (Medium)
- Find in Mountain Array (Hard)
- Search a 2D Matrix II (Medium)
7. Linked List
Pointer surgery, dummy heads, reversal and merging.
- Reverse Linked List (Easy)
- Merge Two Sorted Lists (Easy)
- Linked List Cycle (Easy)
- Remove Duplicates from Sorted List (Easy)
- Intersection of Two Linked Lists (Easy)
- Remove Nth Node From End of List (Medium)
- Swap Nodes in Pairs (Medium)
- Add Two Numbers (Medium)
- Reorder List (Medium)
- Reverse Linked List II (Medium)
- Rotate List (Medium)
- Copy List with Random Pointer (Medium)
- LRU Cache (Medium)
- Reverse Nodes in k-Group (Hard)
- Merge k Sorted Lists (Hard)
- Remove Linked List Elements (Easy)
- Odd Even Linked List (Medium)
- Partition List (Medium)
- Sort List (Medium)
- Design Linked List (Medium)
- Flatten a Multilevel Doubly Linked List (Medium)
8. Trees & BST
Recursion with the right return value, BST ordering, and level-by-level BFS.
- Invert Binary Tree (Easy)
- Maximum Depth of Binary Tree (Easy)
- Diameter of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Same Tree (Easy)
- Symmetric Tree (Easy)
- Subtree of Another Tree (Easy)
- Path Sum (Easy)
- Lowest Common Ancestor of a BST (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Binary Tree Right Side View (Medium)
- Count Good Nodes in Binary Tree (Medium)
- Validate Binary Search Tree (Medium)
- Kth Smallest Element in a BST (Medium)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Flatten Binary Tree to Linked List (Medium)
- Binary Tree Maximum Path Sum (Hard)
- Serialize and Deserialize Binary Tree (Hard)
- Binary Tree Inorder Traversal (Easy)
- Minimum Depth of Binary Tree (Easy)
- Convert Sorted Array to Binary Search Tree (Easy)
- Average of Levels in Binary Tree (Easy)
- Path Sum II (Medium)
- Sum Root to Leaf Numbers (Medium)
- Populating Next Right Pointers in Each Node (Medium)
- Delete Node in a BST (Medium)
- Insert into a Binary Search Tree (Medium)
- House Robber III (Medium)
- All Nodes Distance K in Binary Tree (Medium)
- Binary Search Tree Iterator (Medium)
- Path Sum III (Medium)
- Count Complete Tree Nodes (Easy)
9. Tries
Prefix trees for fast word and prefix lookups.
- Implement Trie (Prefix Tree) (Medium)
- Design Add and Search Words Data Structure (Medium)
- Replace Words (Medium)
- Word Search II (Hard)
- Longest Word in Dictionary (Medium)
- Map Sum Pairs (Medium)
- Search Suggestions System (Medium)
- Maximum XOR of Two Numbers in an Array (Medium)
10. Heap & Priority Queue
Always grab the min or max fast: top-k, k-way merge, scheduling, streaming medians.
- Kth Largest Element in a Stream (Easy)
- Last Stone Weight (Easy)
- K Closest Points to Origin (Medium)
- Kth Largest Element in an Array (Medium)
- Top K Frequent Words (Medium)
- Task Scheduler (Medium)
- Reorganize String (Medium)
- Find Median from Data Stream (Hard)
- Sort Characters By Frequency (Medium)
- Single-Threaded CPU (Medium)
- Maximum Subsequence Score (Medium)
- IPO (Hard)
- Smallest Range Covering Elements from K Lists (Hard)
- Design Twitter (Medium)
11. Graphs (BFS / DFS)
Grids and networks: traversal, shortest paths, topological order, MSTs.
- Number of Islands (Medium)
- Max Area of Island (Medium)
- Clone Graph (Medium)
- Rotting Oranges (Medium)
- 01 Matrix (Medium)
- Surrounded Regions (Medium)
- Pacific Atlantic Water Flow (Medium)
- Course Schedule (Medium)
- Course Schedule II (Medium)
- Graph Valid Tree (Medium)
- Network Delay Time (Medium)
- Cheapest Flights Within K Stops (Medium)
- Min Cost to Connect All Points (Medium)
- Word Ladder (Hard)
- Reconstruct Itinerary (Hard)
- Swim in Rising Water (Hard)
- Flood Fill (Easy)
- Find if Path Exists in Graph (Easy)
- Keys and Rooms (Medium)
- All Paths From Source to Target (Medium)
- Shortest Path in Binary Matrix (Medium)
- Is Graph Bipartite? (Medium)
- Path With Minimum Effort (Medium)
- Minimum Height Trees (Medium)
- Word Ladder II (Hard)
- Shortest Bridge (Medium)
- Critical Connections in a Network (Hard)
- Alien Dictionary (Hard)
12. Backtracking
Choose, explore, un-choose — systematically enumerate subsets, permutations and boards.
- Subsets (Medium)
- Subsets II (Medium)
- Combination Sum (Medium)
- Combination Sum II (Medium)
- Combinations (Medium)
- Permutations (Medium)
- Permutations II (Medium)
- Letter Combinations of a Phone Number (Medium)
- Generate Parentheses (Medium)
- Word Search (Medium)
- Palindrome Partitioning (Medium)
- N-Queens (Hard)
- Sudoku Solver (Hard)
- Combination Sum III (Medium)
- Restore IP Addresses (Medium)
- Letter Case Permutation (Medium)
- Partition to K Equal Sum Subsets (Medium)
- Beautiful Arrangement (Medium)
- Word Break II (Hard)
13. Greedy
Make the locally optimal choice — when you can prove it's globally optimal.
- Maximum Subarray (Medium)
- Jump Game (Medium)
- Jump Game II (Medium)
- Gas Station (Medium)
- Hand of Straights (Medium)
- Merge Triplets to Form Target Triplet (Medium)
- Partition Labels (Medium)
- Valid Parenthesis String (Medium)
- Candy (Hard)
- Assign Cookies (Easy)
- Lemonade Change (Easy)
- Maximum Units on a Truck (Easy)
- Best Time to Buy and Sell Stock II (Medium)
- Two City Scheduling (Medium)
- Minimum Number of Refueling Stops (Hard)
14. Intervals
Sort by start or end, then sweep and merge.
- Insert Interval (Medium)
- Merge Intervals (Medium)
- Non-overlapping Intervals (Medium)
- Meeting Rooms (Easy)
- Meeting Rooms II (Medium)
- Minimum Number of Arrows to Burst Balloons (Medium)
- Car Pooling (Medium)
- Minimum Interval to Include Each Query (Hard)
- Summary Ranges (Easy)
- Interval List Intersections (Medium)
- My Calendar I (Medium)
- Remove Covered Intervals (Medium)
- Data Stream as Disjoint Intervals (Hard)
- My Calendar II (Medium)
15. 1-D Dynamic Programming
Define the state, the recurrence and the base case — then fill a table.
- Climbing Stairs (Easy)
- Min Cost Climbing Stairs (Easy)
- House Robber (Medium)
- House Robber II (Medium)
- Longest Palindromic Substring (Medium)
- Palindromic Substrings (Medium)
- Decode Ways (Medium)
- Coin Change (Medium)
- Coin Change II (Medium)
- Maximum Product Subarray (Medium)
- Word Break (Medium)
- Longest Increasing Subsequence (Medium)
- Partition Equal Subset Sum (Medium)
- Combination Sum IV (Medium)
- Perfect Squares (Medium)
- Delete and Earn (Medium)
- Fibonacci Number (Easy)
- N-th Tribonacci Number (Easy)
- Best Time to Buy and Sell Stock with Transaction Fee (Medium)
- Wiggle Subsequence (Medium)
- Integer Break (Medium)
- Last Stone Weight II (Medium)
- Stone Game (Medium)
- Minimum Cost For Tickets (Medium)
- Best Time to Buy and Sell Stock III (Hard)
- Best Time to Buy and Sell Stock IV (Hard)
- Russian Doll Envelopes (Hard)
16. 2-D Dynamic Programming
Grids and pairs of strings: dp[i][j] from its neighbors.
- Unique Paths (Medium)
- Unique Paths II (Medium)
- Minimum Path Sum (Medium)
- Longest Common Subsequence (Medium)
- Longest Palindromic Subsequence (Medium)
- Best Time to Buy and Sell Stock with Cooldown (Medium)
- Target Sum (Medium)
- Interleaving String (Medium)
- Maximal Square (Medium)
- Edit Distance (Medium)
- Distinct Subsequences (Hard)
- Burst Balloons (Hard)
- Regular Expression Matching (Hard)
- Triangle (Medium)
- Minimum Falling Path Sum (Medium)
- Ones and Zeroes (Medium)
- Longest String Chain (Medium)
- Count Square Submatrices with All Ones (Medium)
- Dungeon Game (Hard)
- Cherry Pickup (Hard)
- Wildcard Matching (Hard)
- Maximum Length of Repeated Subarray (Medium)
- Pascal's Triangle (Easy)
17. Bit Manipulation
XOR tricks, masks and counting bits.
- Single Number (Easy)
- Number of 1 Bits (Easy)
- Counting Bits (Easy)
- Reverse Bits (Easy)
- Missing Number (Easy)
- Single Number II (Medium)
- Sum of Two Integers (Medium)
- Bitwise AND of Numbers Range (Medium)
- Power of Two (Easy)
- Hamming Distance (Easy)
- Add Binary (Easy)
- Single Number III (Medium)
- Gray Code (Medium)
- Divide Two Integers (Medium)
- Reverse Integer (Medium)
18. Fast & Slow Pointers
Two speeds through a sequence: cycles, middles and duplicates in O(1) space.
- Middle of the Linked List (Easy)
- Happy Number (Easy)
- Palindrome Linked List (Easy)
- Linked List Cycle II (Medium)
- Find the Duplicate Number (Medium)
- Circular Array Loop (Medium)
19. Matrix
2-D array traversal, in-place transforms and grid simulation.
- Set Matrix Zeroes (Medium)
- Spiral Matrix (Medium)
- Rotate Image (Medium)
- Valid Sudoku (Medium)
- Game of Life (Medium)
- Longest Increasing Path in a Matrix (Hard)
- Transpose Matrix (Easy)
- Matrix Diagonal Sum (Easy)
- Reshape the Matrix (Easy)
- Spiral Matrix II (Medium)
- Diagonal Traverse (Medium)
- Plus One (Easy)
20. Union-Find
Track connected components under merges in near-constant time.
- Number of Connected Components in an Undirected Graph (Medium)
- Number of Provinces (Medium)
- Redundant Connection (Medium)
- Accounts Merge (Medium)
- Most Stones Removed with Same Row or Column (Medium)
- Satisfiability of Equality Equations (Medium)
- Number of Operations to Make Network Connected (Medium)
- Smallest String With Swaps (Medium)
- Evaluate Division (Medium)
- Redundant Connection II (Hard)