To tackle Data Structures and Algorithms (DSA) problems
effectively, you can follow these strategies and patterns:
1. Understand the Problem
● Read the problem statement carefully.
● Identify inputs and outputs: What are you given, and
what do you need to find?
● Clarify constraints: Understand edge cases, size limits,
and performance expectations.
● Ask questions: If unclear, make assumptions explicitly in
your solution.
2. Choose the Right Data Structure
● Array: Useful for sequential data or when indices matter.
● HashMap/HashSet: For fast lookups or uniqueness
constraints.
● Stack/Queue: Use for problems requiring LIFO/FIFO
behavior.
● Heap/Priority Queue: When you need access to the
smallest or largest element quickly.
● Tree/Graph: For hierarchical or networked relationships.
● Linked List: For dynamic insertions and deletions.
● Sliding Window: When working with subarrays or
substrings.
3. Common Problem-Solving Patterns
Two Pointers
● When to use: Optimizing for space/time complexity in
arrays or strings.
● Examples: Palindrome checking, merging sorted arrays,
finding pairs with a given sum.
Sliding Window
● When to use: Problems involving subarrays or
substrings.
● Examples: Maximum sum of k-length subarray, longest
substring without repeating characters.
Binary Search
● When to use: Searching in sorted data or to
minimize/maximize an objective.
● Examples: Finding the square root, searching in rotated
arrays, allocating tasks.
Divide and Conquer
● When to use: Problems naturally broken into smaller
sub-problems.
● Examples: Merge Sort, Quick Sort, Maximum subarray
sum (Kadane's Algorithm).
Backtracking
● When to use: Generating all possibilities or solving
constraint-based problems.
● Examples: N-Queens, Sudoku solver, Subset generation.
Dynamic Programming (DP)
● When to use: Problems with overlapping sub-problems
and optimal substructure.
● Examples: Knapsack, Longest Increasing Subsequence,
Matrix chain multiplication.
Greedy Algorithms
● When to use: Problems requiring local optimization for a
global solution.
● Examples: Interval scheduling, Huffman encoding, Coin
change problem.
Graph Traversal
● BFS: Shortest path in an unweighted graph.
● DFS: Exploring all possibilities or connected components.
● Examples: Maze problems, connected components,
detecting cycles.
Union-Find
● When to use: Finding connected components or cycle
detection.
● Examples: Kruskal’s algorithm, Connected cities
problem.
Topological Sorting
● When to use: Dependency or order-based problems.
● Examples: Course schedule, task scheduling.
4. Break Down the Problem
● Write a high-level plan for your solution.
● Solve a simpler version or focus on a part of the
problem.
● Create helper functions for repetitive tasks.
5. Optimize and Analyze
● Time complexity: Is your solution efficient? Can you
improve it?
● Space complexity: Are there redundant data structures?
Can you optimize for space?
● Test edge cases: Empty inputs, maximum/minimum
constraints, duplicates, etc.
6. Practice Patterns
● Solve problems that belong to similar patterns until you
recognize them intuitively.
● Start with easy problems and progress to harder ones.
Platforms like LeetCode, Codeforces, and HackerRank
have categorized problems for this.
7. Learn from Mistakes
● Review your approach when you get stuck or after
seeing solutions.
● Focus on why an algorithm works, not just how to
implement it.
Time Allocation Summary:
Time
Step
Investment
Problem Understanding 5–10 minutes
Devising a Strategy 10–15 minutes
Breaking Down the
10–15 minutes
Problem
Writing Pseudocode 10–15 minutes
Coding and Debugging 20–30 minutes
Iterative Optimization 15–20 minutes
Process Review 5–10 minutes
10 DP PATTERNS
1) 1D DP Problems:
->Fibonacci Sequence: Calculating the nth Fibonacci number.
->Climbing Stairs: Number of ways to reach the top of a
staircase.
->Coin Change: Minimum number of coins needed to make a
certain amount.
->House Robber: Maximum amount of money that can be
robbed without robbing two adjacent houses.
2) 2D DP Problems:
->Grid-based Problems: Finding paths in a grid, such as the
minimum path sum or unique paths in a grid.
->Knapsack Problem: Optimizing the value of items to include
in a knapsack without exceeding its capacity.
->Longest Common Subsequence: Finding the longest
subsequence present in both sequences.
->Edit Distance: Minimum number of operations required to
convert one string into another.
3) Substring and Subsequence Problems:
->Longest Increasing Subsequence: Finding the longest
increasing subsequence in an array.
->Longest Palindromic Substring: Finding the longest
palindromic substring in a string.
->Palindrome Partitioning: Partitioning a string such that
every substring is a palindrome.
->String Matching: Problems like finding the number of
distinct subsequences or matching patterns in strings.
4) Partition Problems:
->Partition Equal Subset Sum: Determining if a set can be
partitioned into two subsets with equal sum.
->Palindromic Partitioning: Partitioning a string into the
minimum number of palindromic substrings.
5) Game Theory Problems:
->Stone Game: Determining the winner of a stone game given
optimal moves by both players.
->Nim Game: Determining the winner in a nim game given
the initial state.
6) Interval DP Problems:
->Matrix Chain Multiplication: Finding the most efficient way
to multiply a chain of matrices.
->Burst Balloons: Maximizing the coins collected by bursting
balloons in a certain order.
7) Tree DP Problems:
->Binary Tree Maximum Path Sum: Finding the maximum
path sum in a binary tree.
->Longest Path in a Tree: Finding the longest path between
two nodes in a tree.
8) Bitmask DP Problems:
->Traveling Salesman Problem (TSP): Finding the shortest
possible route that visits each city exactly once and returns to
the origin city.
->Steiner Tree: Finding the minimum tree that spans a given
subset of nodes in a graph.
9) Digit DP:
->Number Of Digit One: Finding number of digit 1 appearing
in all non-neg numbers in a certain range.
10) Others:
->Catalan Numbers: Counting the number of correct
sequences of parentheses.
->Subset Sum Problem: Determining if there's a subset of a
given set with a sum equal to a given target.
Mastering these DP patterns will help you tackle a wide range
of interview questions with confidence.
10 GRAPH PATTERNS
1. Graph Traversal
BFS: LeetCode 127 - Word Ladder
DFS: LeetCode 200 - Number of Islands
2. Shortest Path
Dijkstra’s: LeetCode 743 - Network Delay Time
Bellman-Ford: LeetCode 787 - Cheapest Flights Within K Stops
3. Minimum Spanning Tree (MST)
Kruskal’s: LeetCode 1135 - Connecting Cities With Minimum
Cost
Prim’s: LeetCode 1584 - Min Cost to Connect All Points
4. Topological Sorting
Topological Sort: LeetCode 207 - Course Schedule
Kahn’s Algorithm: LeetCode 210 - Course Schedule II
5. Connected Components
Connected Components: LeetCode 323 - Number of
Connected
6. Components in an Undirected Graph
Cycle Detection: LeetCode 207 - Course Schedule
7. Graph Coloring
Bipartite Check: LeetCode 785 - Is Graph Bipartite?
M-Coloring: LeetCode 1042 - Flower Planting With No
Adjacent
8. Flow Problems
Max Flow: LeetCode 1334 - Find the City With the Smallest
Number of Neighbors at a Threshold Distance
9. Union-Find (Disjoint Set)
Union-Find: LeetCode 684 - Redundant Connection
Cycle Detection: LeetCode 261 - Graph Valid Tree
10. Other Graph Problems which involves bitmasking like
Travelling Salesman Problem using Dynamic Programming