Top 10 DSA Patterns That Cover 90% of Coding Interviews


Top 10 DSA Patterns That Cover 90% of Coding Interviews

Data Structures and Algorithms (DSA) are the backbone of technical interviews, but the sheer number of problem types can be overwhelming. The good news is that most coding interview questions are variations of a few core patterns. If you master these 10 DSA patterns, you’ll be well-equipped to handle 90% of interview questions across top tech companies.

1. Sliding Window

When to Use: Efficiently process subarrays, substrings, or sequences of a fixed or dynamic size.

Common Use Cases:

  • Maximum sum of k consecutive elements
  • Longest substring without repeating characters
  • Minimum window substring

Key Idea: Use two pointers to maintain a moving window and avoid recomputing on every iteration.

def max_subarray_sum(arr, k):
max_sum, window_sum = 0, sum(arr[:k])
for i in range(k, len(arr)):
window_sum += arr[i] - arr[i - k]
max_sum = max(max_sum, window_sum)
return max_sum

2. Two Pointers

When to Use: Search for pairs, or partition arrays efficiently, especially when the input is sorted.

Common Use Cases:

  • Two-sum in a sorted array
  • Remove duplicates
  • Reverse words or arrays in-place

Key Idea: Use two indices that move toward each other to reduce complexity.

def has_pair_with_sum(arr, target):
left, right = 0, len(arr) - 1
while left < right:
total = arr[left] + arr[right]
if total == target:
return True
elif total < target:
left += 1
else:
right -= 1
return False

3. Fast & Slow Pointers (Floyd’s Cycle Detection)

When to Use: Detect cycles in linked lists or arrays.

Common Use Cases:

  • Detect cycle in a linked list
  • Find cycle length
  • Find starting point of the loop

Key Idea: Use two pointers—one moves twice as fast as the other.

def has_cycle(head):
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False

4. Merge Intervals

When to Use: Work with overlapping intervals or time ranges.

Common Use Cases:

  • Merge overlapping intervals
  • Insert an interval
  • Employee free time

Key Idea: Sort by start time, then iterate and merge intervals conditionally.

def merge_intervals(intervals):
intervals.sort(key=lambda x: x[0])
merged = [intervals[0]]
for start, end in intervals[1:]:
if start <= merged[-1][1]:
merged[-1][1] = max(merged[-1][1], end)
else:
merged.append([start, end])
return merged

5. Cyclic Sort

When to Use: Handle problems where numbers are in a specific range (e.g., 1 to n).

Common Use Cases:

  • Find missing numbers
  • Find duplicate numbers
  • Find corrupt pairs

Key Idea: Place each number at its correct index with in-place swaps.

def cyclic_sort(arr):
i = 0
while i < len(arr):
correct = arr[i] - 1
if arr[i] != arr[correct]:
arr[i], arr[correct] = arr[correct], arr[i]
else:
i += 1
return arr

6. In-Place Reversal of Linked List

When to Use: Reverse portions or entire linked lists in O(1) space.

Common Use Cases:

  • Reverse a linked list
  • Reverse a sublist
  • Palindrome check

Key Idea: Reverse pointers while traversing the list.

def reverse_list(head):
prev, current = None, head
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
return prev

7. Tree BFS (Level Order Traversal)

When to Use: Traverse trees level by level or compute depth-related metrics.

Common Use Cases:

  • Level order traversal
  • Zigzag traversal
  • Find depth or average of levels

Key Idea: Use a queue to process one level at a time.

from collections import deque

def level_order(root):
if not root:
return []
result, queue = [], deque([root])
while queue:
level = []
for _ in range(len(queue)):
node = queue.popleft()
level.append(node.val)
if node.left: queue.append(node.left)
if node.right: queue.append(node.right)
result.append(level)
return result

8. DFS Backtracking

When to Use: Explore all paths, combinations, or permutations.

Common Use Cases:

  • Sudoku solver
  • Generate subsets or permutations
  • Word search in grid

Key Idea: Use recursion to explore and backtrack to try alternative solutions.

def subsets(nums):
result = []

def backtrack(start, path):
result.append(path[:])
for i in range(start, len(nums)):
path.append(nums[i])
backtrack(i + 1, path)
path.pop()

backtrack(0, [])
return result

9. Topological Sort (Graphs)

When to Use: Order tasks or nodes with dependencies.

Common Use Cases:

  • Course schedule
  • Task scheduling
  • Build order

Key Idea: Use DFS or Kahn’s algorithm to order vertices respecting directed edges.

from collections import defaultdict, deque

def topological_sort(vertices, edges):
in_degree = {v: 0 for v in range(vertices)}
graph = defaultdict(list)

for u, v in edges:
graph[u].append(v)
in_degree[v] += 1

queue = deque([v for v in in_degree if in_degree[v] == 0])
topo_order = []

while queue:
node = queue.popleft()
topo_order.append(node)
for neighbor in graph[node]:
in_degree[neighbor] -= 1
if in_degree[neighbor] == 0:
queue.append(neighbor)

return topo_order if len(topo_order) == vertices else []

10. Dynamic Programming (DP)

When to Use: Optimize recursive problems by storing intermediate results.

Common Use Cases:

  • Fibonacci sequence
  • Knapsack problem
  • Longest common subsequence
  • Edit distance

Key Idea: Break the problem into subproblems and build up solutions using memoization or tabulation.

def fibonacci(n):
dp = [0, 1]
for i in range(2, n + 1):
dp.append(dp[i - 1] + dp[i - 2])
return dp[n]

Final Thoughts

You don’t need to memorize hundreds of problems to ace coding interviews—mastering these 10 core DSA patterns will cover the majority of challenges you’ll face. Focus on understanding the patterns, practicing problems under each, and being able to recognize which pattern to apply based on the problem’s structure.

Next Steps:

  • Practice 2–3 problems from each pattern on platforms like LeetCode, HackerRank, or Codeforces.
  • Time yourself to simulate real interviews.
  • Understand the why behind each solution—not just the how.

Leave a Comment

Your email address will not be published. Required fields are marked *