DSA Patterns

Master the essential data structures and algorithms patterns that appear in 90% of coding interviews. Learn when and how to apply each pattern effectively.

23+ Patterns
Comprehensive coverage
Real Applications
Industry examples
MAANG Ready
Interview focused
Filter by Data Structure:
👆
Beginner
14 questions
Two Pointers
This method uses two pointers to traverse an array or a list from different ends or directions. It is particularly useful for ordered data structures where intelligent decisions can be made based on pointer positions. It is used when there's a need to find pairs or sub-arrays that satisfy a certain condition, or to find a specific element in a sorted array, typically by advancing pointers towards opposite ends.
Data Structures
ArrayStringLinked List
Common Applications
Finding a pair of numbers that add up to a target sum in a sorted or unsorted ar...
Finding the longest substring in a string that is a palindrome
+3 more examples
Use Cases:5
Questions:14
👆
Beginner
8 questions
Fast & Slow Pointers
This technique involves using two pointers, a 'slow' pointer moving one step at a time and a 'fast' pointer moving two steps at a time. It is used to solve problems that involve linked lists, arrays, or other data structures where specific iteration is needed. Commonly used to detect cycles, find middle elements, or solve other specific problems related to linked lists.
Data Structures
ArrayStringLinked List
Common Applications
Detecting cycles in a linked list
Finding the middle element of a linked list
+3 more examples
Use Cases:5
Questions:8
👆
Beginner
13 questions
Sliding Window
This pattern involves creating a 'window' within a data structure (like an array or string) and moving it to gather specific information. It's used in scenarios where a subarray or substring meeting certain criteria (e.g., specific sum, pattern, or character) within a specific window size needs to be found. Mostly used in array or list-based problems to find contiguous subsets that fulfill certain conditions.
Data Structures
ArrayStringLinked List
Common Applications
Finding the maximum or minimum sum subarray of a given size
Finding the longest substring with distinct characters
+2 more examples
Use Cases:4
Questions:13
🧩
Intermediate
7 questions
In-place Reversal of a Linked List
This pattern involves reversing the order of elements in a linked list without using any additional data structures, modifying the list with constant auxiliary space. It is generally used when reversing a sequence without using extra space, i.e., with O(1) space complexity.
Data Structures
Linked List
Common Applications
Reversing a linked list of any size
Reversing a linked list from a certain point to the end
+1 more examples
Use Cases:3
Questions:7
🔍
Intermediate
13 questions
Modified Binary Search
This method is a tweaked version of the conventional binary search algorithm, used when the search condition is more complex than simple comparisons (e.g., greater than or less than the middle element). It's used when a simple binary search isn't sufficient, such as finding a number in a bitonic array.
Data Structures
Array
Common Applications
Finding the minimum or maximum element in a rotated sorted array
Finding the first occurrence of an element in a sorted array
+1 more examples
Use Cases:3
Questions:13
🧩
Intermediate
11 questions
Islands (Matrix Traversal)
This pattern is used to solve problems that involve traversing a matrix or grid to find a specific pattern or object. It is used when the problem requires identifying clusters or groups of cells in the matrix that meet certain criteria. It involves traversing a matrix to find 'islands' or contiguous groups of elements.
Data Structures
MatrixQueue
Common Applications
Counting the number of islands in a binary matrix
Finding the longest path in a matrix with given constraints
+2 more examples
Use Cases:4
Questions:11
🔄
Intermediate
9 questions
Merge Intervals
This pattern is a popular method for solving problems that involve merging or overlapping intervals. It is used when the problem requires combining or merging intervals that share a common overlap or intersection. Often used in problems involving time intervals, ranges, or sequences.
Data Structures
ArrayHeap
Common Applications
Given a collection of intervals, merge all overlapping intervals
Determine the minimum number of meeting rooms required to schedule all meetings
+3 more examples
Use Cases:5
Questions:9
â›°ī¸
Beginner
5 questions
Two Heaps
This method is popular for managing and processing elements in two separate heaps simultaneously. It is used when the problem requires maintaining two sets of elements with specific ordering or prioritization properties. It's useful when you need to find median numbers in a sequence, or other similar problems by dividing a set of numbers into two parts.
Data Structures
HeapArray
Common Applications
Find the median of streaming data
Find the kth smallest element in an array
+3 more examples
Use Cases:5
Questions:5
â›°ī¸
Intermediate
11 questions
Top 'K' Elements
This method is popular for finding or managing the K largest or smallest elements from a collection of elements. It is used when the problem requires identifying elements with the highest or lowest values based on a specific criterion. It's commonly used in problems involving sorting, searching, and in heap data structures.
Data Structures
HeapArray
Common Applications
Maximum distinct elements after removing k elements
Find the top 'K' frequent elements in an array
+2 more examples
Use Cases:4
Questions:11
🔄
Intermediate
7 questions
K-way Merge
This method is popular for merging multiple sorted lists or arrays into a single sorted list or array. It is used when the problem requires combining and sorting elements from multiple sources. It's typically used in problems involving lists where merging is required.
Data Structures
ArrayQueueHeap
Common Applications
Merging K sorted arrays into a single sorted array
Find Kth smallest value in M sorted arrays
+3 more examples
Use Cases:5
Questions:7
🔄
Intermediate
7 questions
Cyclic Sort
This method is popular for solving problems that involve sorting an array of numbers in a specific range. It is used when the problem requires arranging the elements of the array in a cyclic manner to achieve the desired order. It's useful in situations where the data involves a finite range of natural numbers.
Data Structures
Array
Common Applications
Sorting an array of distinct numbers
Find the smallest missing positive number
+4 more examples
Use Cases:6
Questions:7
🔍
Intermediate
13 questions
Breadth-First Search (BFS)
BFS is a popular method for solving problems related to traversing or searching in a graph or tree data structure. It is used when the problem requires exploring or visiting all the vertices or nodes level by level (breadth-wise) before moving to the next level.
Data Structures
TreeGraphMatrix+1
Common Applications
Find the shortest path or distance between two BST nodes
Return the level order traversal of a binary tree's nodes' values
+4 more examples
Use Cases:6
Questions:13
🔍
Intermediate
13 questions
Depth-First Search (DFS)
DFS is a popular method for solving problems related to traversing or searching in a graph or tree data structure. It is used when the problem requires exploring or visiting all the vertices or nodes in a depth-wise manner, exploring as far as possible along each branch before backtracking.
Data Structures
TreeGraphMatrix
Common Applications
Find the connected components in a graph
Detect cycles in a graph
+3 more examples
Use Cases:5
Questions:13
â›°ī¸
Advanced
7 questions
Topological Sort
This method is popular for solving problems related to directed acyclic graphs (DAGs). It is used when the problem requires ordering the vertices of a DAG such that for every directed edge (u, v), vertex u comes before vertex v in the ordering. It's used for scheduling problems and in scenarios where order needs to be imposed on how you process nodes.
Data Structures
ArrayHashTableQueue+1
Common Applications
Find the order of tasks to finish all tasks given prerequisites
Alien dictionary (determining if words are sorted lexicographically in an alien ...
+1 more examples
Use Cases:3
Questions:7
🧩
Intermediate
10 questions
Subsets
This method is a common algorithmic approach used to generate or explore all possible subsets of a given set. It is used when the problem involves finding or generating all combinations or subsets of elements from a set. It's helpful for solving problems that require exploring all subsets of a given set.
Data Structures
QueueArrayString
Common Applications
Generate all possible subsets of a given set
Check if there is a subset whose sum is equal to a given sum
+3 more examples
Use Cases:5
Questions:10
🧮
Advanced
7 questions
0/1 Knapsack (Dynamic Programming)
This method is a well-known algorithmic approach used to solve optimization problems, particularly those involving resource allocation or selection. It is used when the problem requires maximizing or minimizing a value while considering constraints on capacity or availability of resources. It typically deals with problems where items have different values and weights, and the goal is to determine the maximum value that can be carried.
Data Structures
ArrayHashTable
Common Applications
Find the maximum value by selecting a subset of items to fit into a knapsack wit...
Determine the optimal allocation of resources to maximize overall benefit or val...
+2 more examples
Use Cases:4
Questions:7
đŸ”ĸ
Intermediate
8 questions
Bitwise XOR
This method is used to solve various problems that involve bitwise operations and manipulation of binary representations of numbers. It is used when the problem requires performing operations or extracting information by manipulating individual bits using XOR logic. It's used when we need to manipulate and compare bits directly.
Data Structures
ArrayBits
Common Applications
Find a single element that appears once in an array where every other element ap...
Find two numbers that appear once when all others are repeated
+2 more examples
Use Cases:4
Questions:8
â†Šī¸
Advanced
14 questions
Backtracking
Backtracking is a general algorithmic technique that explores all possible solutions by building candidates one step at a time, and 'backtracks' when a solution cannot be completed or a wrong path is taken. It's typically used for solving complex combinatorial problems, puzzles, and games.
Data Structures
ArrayGraphString+1
Common Applications
Sudoku Solver
Factor Combinations
+4 more examples
Use Cases:6
Questions:14
📚
Intermediate
8 questions
Monotonic Stack
This pattern involves using a stack to maintain a monotonic (either entirely non-increasing or non-decreasing) order of elements. It's often used for solving problems where you need to find the next greater or smaller elements relative to a current element.
Data Structures
Stack
Common Applications
Next Greater Element
Next Smaller Element
+1 more examples
Use Cases:3
Questions:8
🧩
Intermediate
6 questions
Multi-threaded
This pattern involves designing algorithms that can execute multiple threads in parallel. It's used in situations where a task can be divided into independent sub-tasks that can execute concurrently.
Data Structures
Common Applications
Invert Binary Tree
Binary Search Tree Iterator
+1 more examples
Use Cases:3
Questions:6
🧩
Intermediate
9 questions
Fibonacci Numbers
The Fibonacci Numbers method is a mathematical sequence frequently used in DSA to solve various problems. It is used when the problem involves calculating or generating the Fibonacci sequence or utilizing the properties of Fibonacci numbers.
Data Structures
ArrayHashTable
Common Applications
Calculate the nth Fibonacci number
Climbing a staircase with 1 or 2 steps
+1 more examples
Use Cases:3
Questions:9
🧩
Intermediate
8 questions
Palindromic Subsequence
This method is a technique used in DSA to solve problems involving identifying and manipulating palindromic subsequences within a given string or sequence. It is used when the problem requires finding or manipulating palindromic subsequences with specific properties or constraints.
Data Structures
ArrayHashTable
Common Applications
Longest Palindromic Subsequence
Count the total number of distinct palindromic subsequences that can be formed u...
+1 more examples
Use Cases:3
Questions:8
🧩
Intermediate
7 questions
Longest Common Substring
This method is used in DSA to solve problems involving finding the longest common substring between two or more strings. It is used when the problem requires identifying the longest continuous sequence of characters common to multiple strings.
Data Structures
ArrayHashTable
Common Applications
Finding the longest common substring between two strings
Use Cases:1
Questions:7