Stop Memorizing LeetCode: Master These 16 DSA Patterns
If you are preparing for coding interviews, you have probably solved—or at least seen—hundreds of LeetCode and coding problems.
But here is the problem:
Knowing how to solve 200 problems does not necessarily mean you know how to solve the next problem.
In a coding interview, the interviewer may give you a problem you have never seen before. You cannot rely on remembering the exact solution.
What you need is the ability to recognize the underlying problem-solving pattern.
For example:
See "longest substring" → think Sliding Window
See "sorted array + pair" → think Two Pointers
See "cycle in a linked list" → think Fast & Slow Pointers
See "K largest elements" → think Top K / Heap
See "prerequisites and dependencies" → think Topological Sort
Once you learn to recognize these patterns, many unfamiliar coding problems become much easier to approach.
This guide covers 16 important DSA patterns for coding interviews, with a simple explanation of what each pattern means, how to identify it, and an easy way to remember it.
What Are DSA Patterns?
A DSA pattern is a general problem-solving technique that can be applied to many different coding problems.
Think of a pattern as a blueprint.
You do not memorize the answer to every house. You learn how to recognize when a particular blueprint fits the house you are trying to build.
For coding interviews, this means:
Problem → Recognize the clues → Identify the pattern → Apply the technique → Adapt the solution
The goal is not to memorize 16 solutions.
The goal is to train yourself to ask:
"What pattern does this problem look like?"
1. Sliding Window
What is it?
The Sliding Window pattern is commonly used when a problem asks about a contiguous subarray or substring.
Instead of repeatedly calculating every possible range, you maintain a window and move it through the data.
How to identify it
Look for phrases such as:
Subarray
Substring
Contiguous
Consecutive
Longest/shortest substring
Maximum/minimum sum of a window
Window of size K
Simple example
Suppose you are asked:
Find the maximum sum of any 3 consecutive numbers.
For:
[2, 1, 5, 1, 3, 2]
You can maintain a window of 3 elements:
[2, 1, 5]
Then slide it:
[1, 5, 1]
Then:
[5, 1, 3]
And so on.
You don't need to calculate the entire sum from scratch every time.
How to remember
"Contiguous + moving range = Sliding Window."
Common interview problems
Maximum sum subarray of size K
Longest substring without repeating characters
Minimum Window Substring
Longest substring with K distinct characters
2. Two Pointers
What is it?
The Two Pointers pattern uses two indexes to move through an array or string.
It is particularly useful with sorted arrays or when working from both ends.
How to identify it
Look for:
Sorted array
Find a pair
Two numbers
Compare elements from both ends
Remove duplicates
Reverse something
Left/right positions
Simple example
Given:
[1, 2, 3, 4, 6]
Find two numbers that add up to 6.
Start with:
left = 1
right = 6
Their sum is 7, which is too large, so move the right pointer.
Eventually:
2 + 4 = 6
How to remember
"Two positions moving through the data = Two Pointers."
Common interview problems
Two Sum in a sorted array
3Sum
Container With Most Water
Remove duplicates from sorted array
Valid Palindrome
3. Fast & Slow Pointers
What is it?
This pattern uses two pointers moving at different speeds.
Typically:
Slow moves one step
Fast moves two steps
It is especially useful with linked lists.
How to identify it
Look for:
Linked list
Cycle
Middle of a linked list
Repeated movement
Detect whether something loops
Simple example
Imagine:
1 → 2 → 3 → 4 → 5
The slow pointer moves one node at a time, while the fast pointer moves two.
If the linked list contains a cycle, the fast pointer will eventually meet the slow pointer.
How to remember
"One runs, one walks = Fast & Slow."
Common interview problems
Linked List Cycle
Middle of Linked List
Happy Number
Start of Linked List Cycle
Palindrome Linked List
4. Merge Intervals
What is it?
The Merge Intervals pattern is used when a problem contains ranges that may overlap.
For example:
[1,3] and [2,6]
overlap, so they can be merged into:
[1,6]
How to identify it
Look for:
Intervals
Start and end times
Meetings
Overlapping ranges
Schedules
Time periods
Simple example
Suppose meetings are:
[9,11]
[10,12]
[14,16]
The first two overlap.
After merging:
[9,12]
[14,16]
How to remember
"Ranges that overlap = Merge Intervals."
Common interview problems
Merge Intervals
Insert Interval
Meeting Rooms
Meeting Rooms II
Employee Free Time
5. Cyclic Sort
What is it?
Cyclic Sort is useful when an array contains numbers within a known range, often from 1 to N.
Instead of sorting with a traditional sorting algorithm, you place each number directly into its correct position.
How to identify it
Look for:
Numbers from
1toNMissing number
Duplicate number
All missing numbers
One incorrect number
Array containing values in a limited range
Simple example
Consider:
[3, 1, 5, 4, 2]
The correct arrangement is:
[1, 2, 3, 4, 5]
Each number has a natural position.
For example, 1 belongs at index 0, 2 at index 1, and so on.
How to remember
"Numbers know where they belong = Cyclic Sort."
Common interview problems
Find Missing Number
Find All Missing Numbers
Find Duplicate Number
Find the Corrupt Pair
First Missing Positive
6. In-Place Linked List Reversal
What is it?
This pattern reverses a linked list without creating another linked list.
For example:
1 → 2 → 3 → 4
becomes:
4 → 3 → 2 → 1
How to identify it
Look for:
Reverse linked list
Reverse a portion of a list
Reverse every K elements
Reorder linked list
Linked-list palindrome
How to remember
Think:
"Change the arrows, not the nodes."
You normally work with pointers such as:
previous
current
next
Common interview problems
Reverse Linked List
Reverse Linked List II
Reverse Nodes in K-Group
Reorder List
Palindrome Linked List
7. Tree BFS
What is it?
BFS (Breadth-First Search) processes a tree level by level.
For example:
1
/ \
2 3
/ \ / \
4 5 6 7
BFS visits:
1 → 2 → 3 → 4 → 5 → 6 → 7
How to identify it
Look for:
Level order
Level by level
Each tree level
Zigzag levels
Minimum depth
Right-side view
What data structure is usually used?
Queue
How to remember
"BFS = Broad First = Level by Level."
Common interview problems
Binary Tree Level Order Traversal
Zigzag Level Order Traversal
Minimum Depth of Binary Tree
Binary Tree Right Side View
Average of Levels
8. Tree DFS
What is it?
DFS (Depth-First Search) explores a tree by going as deep as possible before coming back.
It is commonly implemented using:
Recursion
Stack
How to identify it
Look for:
Root-to-leaf paths
Path sum
Tree depth
Tree diameter
Explore every branch
Validate tree structure
Simple idea
For:
1
/ \
2 3
/
4
DFS may explore:
1 → 2 → 4
before returning and exploring the other branch.
How to remember
"DFS = Dive into a branch."
Common interview problems
Maximum Depth of Binary Tree
Path Sum
Binary Tree Paths
Diameter of Binary Tree
Validate Binary Search Tree
9. Two Heaps
What is it?
The Two Heaps pattern uses two heaps to divide data into two parts.
Typically:
Max Heap → smaller half
Min Heap → larger half
This makes it possible to efficiently find the median.
How to identify it
Look for:
Median
Median of a stream
Numbers arriving continuously
Balance two groups
Scheduling based on two sides
How to remember
"Two halves need two heaps."
Common interview problems
Find Median from Data Stream
Sliding Window Median
IPO
Maximum Capital
10. Subsets & Backtracking
What is it?
This pattern is useful when you need to generate different possible choices.
For example, given:
[1, 2, 3]
you may need to generate:
[]
[1]
[2]
[3]
[1,2]
[1,3]
[2,3]
[1,2,3]
This is often solved using backtracking.
How to identify it
Look for:
All possible combinations
All subsets
All permutations
Combinations
Different ways to choose
Generate every possibility
How to remember
"If the question asks for ALL possibilities, think Backtracking."
Common interview problems
Subsets
Permutations
Combinations
Combination Sum
Letter Combinations of a Phone Number
11. Modified Binary Search
What is it?
Binary Search is normally used on sorted data.
But many interview problems modify the normal structure while keeping the underlying idea of eliminating half of the search space.
For example:
[4,5,6,7,0,1,2]
is a rotated sorted array.
You can still use binary-search logic to find the answer efficiently.
How to identify it
Look for:
Sorted array
Rotated sorted array
Search space
Find minimum/maximum
Search in an unknown-sized sorted array
Find a peak
How to remember
"Sorted or almost sorted + search = Think Binary Search."
Common interview problems
Search in Rotated Sorted Array
Find Minimum in Rotated Sorted Array
Find Peak Element
Search a 2D Matrix
Search in an Infinite Sorted Array
12. Bitwise XOR
What is it?
XOR is a bitwise operation represented by:
^
Two useful properties are:
x ^ x = 0
and:
x ^ 0 = x
This makes XOR extremely useful for problems involving pairs, duplicates, and missing values.
Simple example
Suppose:
[4, 1, 2, 1, 2]
XOR all numbers:
4 ^ 1 ^ 2 ^ 1 ^ 2
The matching pairs cancel each other, leaving:
4
How to identify it
Look for:
Every number appears twice except one
Find the unique number
Missing number
Duplicate pairs
Bit manipulation
How to remember
"Same numbers cancel with XOR."
Common interview problems
Single Number
Missing Number
Single Number III
Bitwise complement problems
13. Top K Elements
What is it?
Whenever a problem asks for the top K or bottom K, a Heap/Priority Queue should come to mind.
Examples:
K largest
K smallest
K most frequent
K closest
How to identify it
Look for the words:
K largest, K smallest, K closest, K frequent, Kth largest, Kth smallest
Simple example
Given:
[10, 5, 20, 8, 30]
Find the 2 largest numbers.
Answer:
30, 20
A heap can help solve such problems efficiently.
How to remember
"See K → Think Heap."
Common interview problems
Kth Largest Element
Top K Frequent Elements
K Closest Points to Origin
K Largest Elements
14. K-Way Merge
What is it?
Use K-Way Merge when you have multiple sorted lists or arrays and need to combine or compare them efficiently.
For example:
List 1: 1, 4, 7
List 2: 2, 5, 8
List 3: 3, 6, 9
You want:
1,2,3,4,5,6,7,8,9
A Min Heap is commonly used to keep track of the smallest available element from each list.
How to identify it
Look for:
K sorted arrays
K sorted linked lists
Merge multiple sorted lists
Kth smallest across sorted lists
Multiple sorted sources
How to remember
"Many sorted lists → Merge with a Heap."
Common interview problems
Merge K Sorted Lists
Kth Smallest Number in Sorted Lists
K Smallest Pairs
Smallest Range Covering Elements from K Lists
15. 0/1 Knapsack & Dynamic Programming
What is it?
Dynamic Programming (DP) is used when a problem can be broken into smaller subproblems and those results can be reused.
In 0/1 Knapsack, you generally have two choices for each item:
Take it or don't take it.
For example:
Item A → Take / Skip
Item B → Take / Skip
Item C → Take / Skip
The goal is usually to maximize value or determine whether a target can be reached.
How to identify it
Look for:
Choose or don't choose
Maximum/minimum possible result
Target sum
Capacity
Can we make a particular sum?
Count the number of ways
Repeated subproblems
How to remember
"Make a choice, remember the result."
Common interview problems
0/1 Knapsack
Subset Sum
Equal Subset Partition
Target Sum
Coin Change
House Robber
16. Topological Sort
What is it?
Topological Sort is used when items have dependencies or prerequisites.
Imagine:
Learn Programming
↓
Learn DSA
↓
Learn Algorithms
↓
Coding Interview
You cannot complete a task before its prerequisite.
Topological sorting finds a valid order for these dependent tasks.
How to identify it
Look for:
Prerequisites
Dependencies
Course schedule
Task ordering
Build dependencies
"Must be completed before"
How to remember
"Dependencies → Find the correct order."
Common interview problems
Course Schedule
Course Schedule II
Alien Dictionary
Task Scheduling
Build Order
How to Identify the Right DSA Pattern
This is the most important skill.
When you receive a coding interview problem, don't immediately start coding.
First, look for clues.
Step 1: What type of data are you dealing with?
Ask:
Array?
String?
Linked List?
Tree?
Graph?
Numbers?
Intervals?
The data structure often gives you the first clue.
Step 2: Look for keywords
Certain words should trigger certain patterns.
| Problem clue | Pattern to consider |
|---|---|
| Subarray / substring | Sliding Window |
| Contiguous | Sliding Window |
| Sorted + pair | Two Pointers |
| Cycle | Fast & Slow Pointers |
| Linked list reversal | In-place Reversal |
| Overlapping ranges | Merge Intervals |
| Numbers from 1 to N | Cyclic Sort |
| Level by level | Tree BFS |
| Root-to-leaf | Tree DFS |
| Median | Two Heaps |
| All combinations | Backtracking / Subsets |
| Sorted search space | Binary Search |
| One number appears once | XOR |
| K largest/smallest | Top K / Heap |
| Multiple sorted lists | K-Way Merge |
| Choose / skip | Dynamic Programming |
| Prerequisites | Topological Sort |
A Simple Pattern Recognition Cheat Sheet
When you're stuck in an interview, ask yourself these questions:
Is it a contiguous range?
👉 Sliding Window
Is the array sorted and I'm looking for a pair?
👉 Two Pointers
Is there a cycle?
👉 Fast & Slow Pointers
Are there overlapping time ranges?
👉 Merge Intervals
Are the numbers from 1 to N?
👉 Cyclic Sort
Am I reversing a linked list?
👉 In-Place Reversal
Do I need to process a tree level by level?
👉 Tree BFS
Do I need to explore paths deeply?
👉 Tree DFS
Do I need to continuously find a median?
👉 Two Heaps
Do I need every possible combination?
👉 Backtracking / Subsets
Is the data sorted or nearly sorted?
👉 Binary Search
Do duplicate values cancel out?
👉 Bitwise XOR
Does the question say K largest/smallest/frequent?
👉 Top K / Heap
Do I have multiple sorted lists?
👉 K-Way Merge
Do I have a take-or-skip decision?
👉 Dynamic Programming
Are there prerequisites or dependencies?
👉 Topological Sort
Don't Memorize Solutions—Recognize Patterns
The biggest mistake many coding interview candidates make is trying to memorize solutions problem by problem.
For example:
"I solved this exact LeetCode problem last month, but I don't remember the code."
That doesn't help much when the interviewer changes the question slightly.
Instead, learn the underlying pattern.
Suppose you understand Sliding Window.
You can encounter:
Longest substring
Maximum sum subarray
Minimum window
K distinct characters
These problems may look different, but the underlying approach can be similar.
The same idea applies to the other patterns.
Learn the pattern, understand why it works, and then practice variations.
How to Study These 16 Patterns
Don't try to learn all 16 in one day.
A better approach is to study them in groups.
Group 1: Arrays & Strings
Sliding Window
Two Pointers
Cyclic Sort
Modified Binary Search
Bitwise XOR
Group 2: Linked Lists
Fast & Slow Pointers
In-Place Reversal
Group 3: Intervals & Heaps
Merge Intervals
Two Heaps
Top K Elements
K-Way Merge
Group 4: Trees
Tree BFS
Tree DFS
Group 5: Combinations & Graphs
Subsets / Backtracking
Dynamic Programming
Topological Sort
For each pattern, don't just solve one problem.
Try solving 3–5 problems that use the same pattern but look different.
That's when pattern recognition starts becoming natural.
Final Takeaway
You don't need to memorize every LeetCode problem.
You need to become good at recognizing why a particular approach fits a particular problem.
The 16 patterns in this guide give you a strong foundation:
Sliding Window
Two Pointers
Fast & Slow Pointers
Merge Intervals
Cyclic Sort
In-Place Reversal
Tree BFS
Tree DFS
Two Heaps
Subsets & Backtracking
Modified Binary Search
Bitwise XOR
Top K Elements
K-Way Merge
0/1 Knapsack & Dynamic Programming
Topological Sort
The next time you see a coding problem, don't immediately ask:
"Have I seen this exact problem before?"
Ask:
"What clues does this problem give me, and which pattern matches those clues?"
That shift—from memorizing solutions to recognizing patterns—is one of the most valuable skills you can develop for coding interviews.
Learn the pattern. Recognize the clues. Then write the solution.