4 min read
Common DSA Patterns, Explained Simply

The hardest part of a DSA problem is often not writing the code. It is knowing where to start. Use this list to recognise the pattern hiding inside a problem. You do not need to memorise every solution.

1. Sliding window

  • The problem asks about a continuous part of an array or string.
  • Find the longest, shortest, or best subarray that satisfies a condition.

2. Two pointers

  • Scan an array from two positions.
  • This is useful when the array is sorted.
  • It can often solve the problem in one pass.

3. Prefix sum

  • Stores the running total of an array.
P[i] = A[0] + A[1] + ... + A[i]
  • Build it once to quickly find the sum between two indices.
sum(i to j) = P[j] - P[i - 1]
  • If i is 0, the answer is simply P[j].
  • Use it when the data is sorted.
  • It can find an item, boundary, or possible answer quickly.

5. Monotonic stack

  • Keeps values in increasing or decreasing order.
  • Common questions include:
    • What is the next greater element?
    • What is the next smaller element?

6. Top K elements

  • Finds the largest, smallest, or most frequent K items.
  • Use a heap, also called a priority queue.

7. Overlapping intervals

  • Use it when the input contains ranges, such as start and end times.
  • Common problems include:
    • merging intervals
    • inserting a new interval
    • finding interval intersections
    • scheduling meeting rooms

8. Fast and slow pointers

  • Uses two pointers moving at different speeds to detect a cycle.
  • A common example is finding a cycle in a linked list.

9. Linked list in-place reversal

  • Reverses or rearranges linked-list nodes without creating another list.
  • Keep track of the previous, current, and next nodes.

10. Subsets

  • Generates all possible combinations from a set.
  • Use DFS or backtracking.

11. Backtracking

  • Tries many choices and undoes the bad ones.
  • Common examples include:
    • Sudoku
    • N-Queens
    • permutations
    • combinations

12. Dynamic programming

  • Use it when a problem has smaller repeated subproblems.
  • Save an answer and reuse it instead of solving the same part again.
  • Common examples are Fibonacci and 0/1 Knapsack.

13. Binary tree traversal

  • The four common traversals are:
    • inorder
    • preorder
    • postorder
    • level order
  • Choose one based on when you need to process each node.

14. Binary tree DFS

  • Explores a tree branch by branch.
  • It is useful for recursive problems such as finding the longest path.

15. Binary tree BFS

  • Explores a tree level by level.
  • Use a queue.

16. DFS and BFS

  • DFS goes deep into one path before trying another.
  • BFS explores nearby nodes first.
  • Both can search trees and graphs.

17. Matrix traversal

  • Use it when the problem gives you a grid.
  • Common examples are counting islands and finding paths.
  • Use DFS or BFS to move through the cells.

18. Topological sort

  • Use it when some tasks must happen before other tasks.
  • Examples include course prerequisites, module dependencies, and build steps.
  • The graph must be a Directed Acyclic Graph, or DAG.

A simple way to use this list

  • For a continuous range, think sliding window.

  • For sorted input, think binary search or two pointers.

  • For top K items, think heap.

  • For dependencies, think topological sort.

  • For every possible answer, think backtracking.

  • For repeated smaller problems, think dynamic programming.

  • You do not need to force every problem into one pattern. This list is only a starting point.

  • Once you can name the likely pattern, the code becomes much easier to plan.