How to Choose the Right Technique to Solve a Given DSA Problem


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

Major DSA Techniques

1. Brute Force

This is the most straightforward approach — try all possible combinations, permutations, or paths.

Use When:

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:

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:

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:

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:

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:

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:

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:

Examples: Sudoku solver, N-Queens, subset sum.

9. Divide and Conquer

Divide the problem into independent subproblems, solve them, and combine results.

Use When:

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:

Examples: Clone graph, detect cycles, shortest path (unweighted).

Final Tips

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.