Choosing the right technique is just as important as selecting the right data structure. In DSA interviews, the efficiency of your solution often depends on identifying the underlying pattern and mapping it to the correct algorithmic strategy. Here’s how you can approach it systematically.
Ask These Questions First
- Is the brute force approach too slow? Can I optimize it?
- Does the problem mention constraints like “maximum sum”, “number of ways”, or “minimum operations”?
- Are overlapping subproblems or repeated states involved?
- Is the problem recursive by nature? (e.g., “divide”, “split”, “choices”)
- Am I dealing with a sliding window, sorted array, or greedy choice?
- Is this a graph, and if so, directed/undirected, weighted/unweighted?
Major DSA Techniques
1. Brute Force
This is the most straightforward approach — try all possible combinations, permutations, or paths.
Use When:
- You are unsure of an optimal solution and want a baseline.
- Constraints are small (e.g., N ≤ 10).
Examples: Generating all subsets, trying all permutations, recursive tree building.
2. Two Pointer Technique
Use two pointers (start and end) to iterate over an array in opposite directions or in a window.
Use When:
- Array is sorted.
- You need to find pairs/triplets with specific properties (sum, difference).
Examples: Two sum in sorted array, remove duplicates, merging two arrays.
3. Sliding Window
Maintain a fixed-size or variable-size window and slide it across the array to optimize time complexity.
Use When:
- You need the max/min/sum of a subarray of fixed or dynamic size.
- You’re processing contiguous segments efficiently.
Examples: Longest substring without repeating characters, max sum subarray of size k.
4. Recursion
Break down the problem into subproblems and solve them recursively.
Use When:
- The problem can be divided into similar subproblems.
- There's a clear decision tree or branching logic.
Examples: Generating combinations, solving mazes, backtracking.
5. Dynamic Programming (DP)
Used when a problem can be divided into overlapping subproblems with optimal substructure.
Use When:
- Same subproblems are solved repeatedly (recursive + memoization).
- You’re asked for “counting”, “maximum/minimum”, or “number of ways”.
Examples: Fibonacci, 0/1 knapsack, longest common subsequence, coin change.
6. Greedy
Make the locally optimal choice at each step, hoping it leads to a globally optimal solution.
Use When:
- The problem has an “optimal substructure” and a greedy property.
- You’re minimizing or maximizing something like time, cost, or intervals.
Examples: Activity selection, Huffman encoding, Dijkstra’s algorithm (with priority queue).
7. Binary Search
Efficient way to find elements or answers in sorted/search space.
Use When:
- Array or solution space is sorted or monotonic.
- You’re asked for smallest/largest possible answer (search on answer).
Examples: Search in rotated sorted array, Koko Eating Bananas, upper/lower bound.
8. Backtracking
Explore all possibilities and backtrack when a path is invalid or a goal is reached.
Use When:
- You need all combinations or permutations.
- There are constraints that must be enforced.
Examples: Sudoku solver, N-Queens, subset sum.
9. Divide and Conquer
Divide the problem into independent subproblems, solve them, and combine results.
Use When:
- Subproblems are independent.
- You want to optimize recursion (e.g., merge sort, quick sort).
Examples: Merge sort, Quick sort, Maximum subarray (Kadane’s as DP version).
10. Graph Traversals (DFS/BFS)
Used for traversing graph nodes systematically using Depth-First or Breadth-First strategies.
Use When:
- You’re given nodes, edges, and asked about paths, reachability, or components.
Examples: Clone graph, detect cycles, shortest path (unweighted).
Final Tips
- Start with brute force, identify inefficiencies, then optimize.
- Look for patterns in constraints (small N, keywords like “minimum”, “count”, etc).
- Be comfortable switching between recursion and iteration.
- Practice problems by technique type to strengthen pattern recognition.
Pro Tip: Don’t just memorize techniques — understand the core intuition. The better you grasp the why, the faster you'll spot the how in interviews.