MohitDecodes
Back to blogsDSA

The DSA Patterns That Solve Most Coding Interview Questions

August 9, 2026 · 8 min read

Grinding LeetCode problem-by-problem feels productive but doesn't transfer well — solve 200 problems randomly and you'll still freeze on problem 201 if it's phrased differently. What actually transfers is recognizing the underlying pattern. Most interview questions are a handful of patterns wearing different costumes.

1. Two Pointers

Use when you're scanning a sorted array or string from both ends, or need to compare elements without nested loops. Two indices move toward each other (or one moves faster) instead of checking every pair, which is what takes this from O(n²) to O(n).

Recognize it when the problem involves: pairs that sum to a target, palindrome checks, removing duplicates in place.

2. Sliding Window

A specialization of two pointers for subarrays and substrings. Instead of recomputing a sum or count for every possible window, you expand the window by adding one element and shrink it by removing one, reusing the work you already did.

// Longest substring without repeating characters
function longestUnique(s) {
  const seen = new Map();
  let start = 0, best = 0;
  for (let end = 0; end < s.length; end++) {
    const ch = s[end];
    if (seen.has(ch) && seen.get(ch) >= start) {
      start = seen.get(ch) + 1;
    }
    seen.set(ch, end);
    best = Math.max(best, end - start + 1);
  }
  return best;
}

Recognize it when the problem says "longest/shortest/max sum sub-array or sub-string" — that phrasing is almost always sliding window.

3. Fast & Slow Pointers

Two pointers moving through a linked list (or array treated as one) at different speeds. If there's a cycle, the fast pointer eventually laps the slow one. This is how you detect cycles without extra memory.

4. BFS / DFS on Trees and Graphs

Anything involving "levels", "shortest path in an unweighted graph", or "all possible paths" — BFS explores level by level (good for shortest path), DFS goes as deep as possible before backtracking (good for exploring all paths or detecting structure).

5. Dynamic Programming

The pattern to recognize isn't a technique so much as a question: does this problem have overlapping subproblems, where the brute-force recursive solution recomputes the same smaller problem over and over? If yes, cache the result (memoization) or build it bottom-up (tabulation). Classic-Signal: the problem asks for a count, a max/min, or "is it possible" over a sequence of choices.

6. Backtracking

Used for "generate all possible X" problems — permutations, combinations, valid parentheses, N-Queens. You build a solution incrementally and abandon ("backtrack") as soon as you know the current path can't work.

The honest study advice: for each pattern, solve 4-5 problems specifically chosen to use it, until you can spot it from the problem statement alone — not the code. Recognition is the actual skill; the implementation is mechanical once you know which tool you're reaching for.