All posts

DSA

Crack DSA Rounds by Learning Patterns, Not 500 LeetCode Problems

Nagaraju Nali10 min read

Somewhere on the internet there's a spreadsheet of 500 LeetCode problems, and a lot of people preparing for interviews are on problem 87, exhausted, and still freezing on new questions. The problem isn't effort. It's that solving problems one by one teaches you answers, and interviews ask you questions you haven't seen.

The good news: most interview questions are built from a small set of patterns. Learn to recognise the pattern, and a "new" problem turns into a variation of something you already know. This guide covers the six patterns that come up most, how to spot each one from the wording of a question, and a six-week plan to practise them.

Why 500 problems is the wrong goal

  • You forget solutions you memorised. Without the underlying idea, problem 40 is gone by the time you reach problem 200.
  • Interviewers change the details. "Find two numbers that add up to a target" becomes "find two products whose prices fit a budget". Same pattern, different story.
  • Count is the wrong metric. Being able to explain why your solution works, and its time and space complexity, matters more than how many problems you have ticked off.

A better target: about 100-150 problems, chosen by pattern, each one solved, understood and revisited. Most candidates who get offers did far fewer problems than you think - they just did them deliberately.

The 6 patterns behind most interview questions

Six pattern cards, each with the clue that gives it away: two pointers for sorted arrays and pairs, sliding window for contiguous subarrays, hash map for lookups and counting, binary search for sorted data, recursion for problems made of smaller copies of themselves, and BFS/DFS for trees and graphs.
Read the question for these clues before you think about code.

1. Two pointers

Use two indexes that move through an array - often one from each end, or a slow one and a fast one. It turns many O(n²) "check every pair" solutions into a single O(n) pass.

Clues: the array is sorted, you need a pair or triplet, you must work in place, or you are asked to reverse or merge something.

// Is there a pair in a sorted array that adds up to target?
function hasPairWithSum(sorted, target) {
  let left = 0;
  let right = sorted.length - 1;

  while (left < right) {
    const sum = sorted[left] + sorted[right];
    if (sum === target) return true;
    if (sum < target) left++; // need a bigger sum
    else right--;             // need a smaller sum
  }
  return false;
}
// Time O(n), space O(1)

Practise it with Remove Duplicates from Sorted Array, Move Zeroes and Merge Sorted Arrays.

2. Sliding window

Keep a "window" over a run of consecutive elements and slide it forward, adding the new element and removing the old one, instead of recalculating from scratch each time.

Clues: the words "subarray", "substring", "consecutive" or "contiguous", together with "longest", "shortest", "maximum" or "at most k".

Top: a sorted array with a left pointer at the start and a right pointer at the end moving towards each other. Bottom: a window of three elements sliding one step right, adding the new element and dropping the old one.
Two pointers close in from both ends; a sliding window moves forward one step at a time.
// Largest sum of any k consecutive numbers (assumes nums.length >= k)
function maxSumOfK(nums, k) {
  let windowSum = 0;
  for (let i = 0; i < k; i++) windowSum += nums[i];

  let best = windowSum;
  for (let i = k; i < nums.length; i++) {
    windowSum += nums[i] - nums[i - k]; // add the new, drop the old
    best = Math.max(best, windowSum);
  }
  return best;
}
// Time O(n) instead of O(n * k)

Max Consecutive Ones and Best Time to Buy and Sell Stocks are good first problems - both track a running window in a single pass.

3. Hash map and hash set

Trade a little memory for speed: store what you have seen so each lookup takes O(1). This is the single most useful pattern in interviews, and the classic example is Two Sum.

Clues: "have we seen this before?", counting frequencies, finding duplicates, grouping items, or matching pairs in an unsorted array.

// Two Sum: indexes of the two numbers that add up to target
function twoSum(nums, target) {
  const seen = new Map(); // value -> index

  for (let i = 0; i < nums.length; i++) {
    const need = target - nums[i];
    if (seen.has(need)) return [seen.get(need), i];
    seen.set(nums[i], i);
  }
  return [];
}
// Brute force checks every pair: O(n²). This is O(n) time, O(n) space.

Single Number walks through both the hash map solution and a clever O(1)-space XOR trick - a good example of improving an answer step by step.

4. Binary search

Halve the search space on every step, so a million items take about 20 checks. It works on anything sorted - and on "find the smallest value that works" questions, even when there is no array at all.

Clues: sorted input, "find the first/last position", "minimum possible maximum", or a limit that makes O(n) too slow, such as n up to 10⁹.

function binarySearch(sorted, target) {
  let lo = 0;
  let hi = sorted.length - 1;

  while (lo <= hi) {
    const mid = Math.floor((lo + hi) / 2);
    if (sorted[mid] === target) return mid;
    if (sorted[mid] < target) lo = mid + 1;
    else hi = mid - 1;
  }
  return -1;
}
// Time O(log n)

The Time & Space Complexity lesson compares linear and binary search side by side, which is also the best way to understand what O(log n) really means.

5. Recursion (and backtracking)

Solve a problem by solving a smaller copy of it, until you hit a base case you can answer directly. Backtracking is recursion that tries a choice, explores it, and undoes it - the basis of "generate all combinations" questions.

Clues: trees, "all possible" combinations or permutations, or a problem that is naturally defined in terms of itself, like factorial or Fibonacci. Start with Factorial of n and the Fibonacci masterclass, which also shows why naive recursion can be slow.

6. Trees and graphs: BFS and DFS

Almost every tree or graph question is a traversal in disguise. Breadth-first search (BFS) uses a queue and explores level by level - use it for shortest paths and "level order". Depth-first search (DFS) uses recursion or a stack and goes deep first - use it for paths, counting islands and checking properties of every node.

Clues: tree, grid, network, connections, dependencies, "shortest number of steps" (BFS) or "all paths" (DFS).

Spot the pattern from the question

If the question says...Try this first
"sorted array", "pair", "in place"Two pointers
"subarray", "substring", "consecutive", "at most k"Sliding window
"count", "duplicate", "seen before", unsorted pairsHash map / set
"sorted", "first/last position", huge input limitsBinary search
"all combinations", "all subsets", self-similar structureRecursion / backtracking
"tree", "grid", "connected", "fewest steps"BFS / DFS

Stuck on a question? Say the brute-force solution out loud first, with its complexity. Then ask: "what am I recalculating or searching again and again?" The answer usually points straight at one of these six patterns - and interviewers give credit for that reasoning even if you don't finish.

A 6-week practice plan

About one to two hours a day, five days a week. Each week, learn one or two patterns and solve 15-20 problems that use them, easy first.

WeekFocusGoal by the end of the week
1Big O, arrays, strings, basic recursionState the time and space complexity of anything you write
2Two pointers and sliding windowSolve easy array problems in under 20 minutes
3Hash maps and setsTurn O(n²) pair searches into O(n)
4Binary search, stacks and queuesWrite binary search from memory without off-by-one bugs
5Trees: DFS and BFSTraverse a tree recursively and level by level
6Mixed practice and mock interviewsPick the pattern for an unseen problem within 5 minutes

How to practise so it sticks

  1. Try for 20-30 minutes before looking at a hint. Struggling is where the learning happens - but past 30 minutes, read a hint, not the full solution.
  2. Name the pattern after every problem. Write one line: "Sliding window, because it asked for the longest substring."
  3. Re-solve it 3 days later, from scratch. If you can't, you memorised it. Mark it and try again next week.
  4. Talk while you code. In a real interview, silence looks like being stuck. Practise explaining your approach before typing.
  5. Always finish with complexity. "This is O(n) time and O(n) space because of the map" - every single time.

Common questions

Which language should I use for DSA rounds?

The one you're fastest in. For frontend and full-stack roles, JavaScript is completely fine - just know its built-ins well: Map, Set, array methods, and how sort compares values (by default it sorts as strings, so pass a compare function for numbers).

How many problems are enough?

There's no magic number, but 100-150 well-understood problems across these patterns is enough for most product-company interviews. The test is simple: can you solve an unseen medium problem in about 30 minutes and explain it?

Do frontend developers really need DSA?

Usually less than backend roles, but most product companies still include at least one round. Arrays, strings, hash maps and simple recursion cover most frontend DSA questions. The Frontend Developer Roadmap shows where DSA fits alongside everything else.

Start with the patterns, step by step

The DSA course on this site is built in exactly this order - from basics and complexity to two pointers, hashing and recursion - with explanations, code and multiple approaches for each problem.

Keep going: DSA course

Start now