DEV Community

RHODY FF
RHODY FF

Posted on

23 DSA Patterns That Cover 90% of Coding Interview Questions

The Problem with LeetCode Grinding

Let me guess your DSA preparation strategy:

  1. Open LeetCode
  2. Sort by "Most Liked"
  3. Solve random problems
  4. Repeat until placement season

The result? 500+ problems solved, yet freezing in interviews because you can't recognize the pattern.

I built W Code to fix this. Today, I'm sharing the pattern framework that has helped students crack TCS, Infosys, and FAANG interviews.

The 23 Core DSA Patterns

Array Patterns

  1. Two Pointers

Example: Two Sum (Sorted Array)

def two_sum(arr, target):
left, right = 0, len(arr) - 1
while left < right:
curr_sum = arr[left] + arr[right]
if curr_sum == target:
return [left, right]
elif curr_sum < target:
left += 1
else:
right -= 1
return [-1, -1]

Use when: Finding pairs, removing duplicates, container with most water

  1. Sliding Window

Example: Maximum Sum Subarray of Size K

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

Use when: Subarrays of fixed/variable size, longest substring problems

  1. Prefix Sum
    Use when: Range sum queries, subarray sum equals K

  2. Kadane's Algorithm
    Use when: Maximum subarray sum

🔗 Linked List Patterns

  1. Fast & Slow Pointers

Example: Detect Cycle

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

Use when: Cycle detection, middle of linked list, nth from end

  1. Reverse Linked List Use when: Palindrome check, reverse in groups

🌲 Tree Patterns

  1. BFS (Level Order)

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

Use when: Level-by-level traversal, zigzag, right view

8.DFS (Recursive)
Use when: Path sum, validate BST, diameter

9.Binary Search on BST
Use when: Search, insert, delete, kth smallest

GRAPH PATTERNS

10.BFS on Graph
Use when: Shortest path (unweighted), connected components

11.DFS on Graph
Use when: Cycle detection, connected components

12.Topological Sort

Python logic (Kahn’s Algorithm):

from collections import deque, defaultdict

def topological_sort(n, edges):
graph = defaultdict(list)
in_degree = [0] * n

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

queue = deque([i for i in range(n) if in_degree[i] == 0])
result = []

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

return result if len(result) == n else []
Enter fullscreen mode Exit fullscreen mode

Use when: Course schedule, task ordering, dependency resolution

13.Union-Find (DSU)
Use when: Connected components, cycle detection in undirected graph

14.Dijkstra’s Algorithm
Use when: Shortest path (weighted)

DYNAMIC PROGRAMMING PATTERNS

15.1D DP

Example: Climbing Stairs

def climb_stairs(n):
if n <= 2:
return n
dp = [0] * (n + 1)
dp[1], dp[2] = 1, 2
for i in range(3, n + 1):
dp[i] = dp[i-1] + dp[i-2]
return dp[n]

Use when: House robber, fibonacci variants, coin change (minimum)

16.2D DP
Use when: Grid paths, longest common subsequence, edit distance

17.0/1 Knapsack
Use when: Subset sum, partition equal sum, target sum

18.Unbounded Knapsack
Use when: Coin change (number of ways), rod cutting

19.LCS / LIS Patterns
Use when: Longest increasing subsequence, longest common substring

OTHER ESSENTIAL PATTERNS

Binary Search
Use when: Search in O(log n), search in rotated array

Backtracking

Example: Permutations

def permute(nums):
result = []
def backtrack(path, remaining):
if not remaining:
result.append(path[:])
return
for i, num in enumerate(remaining):
path.append(num)
backtrack(path, remaining[:i] + remaining[i+1:])
path.pop()
backtrack([], nums)
return result

Use when: Permutations, combinations, N-Queens, Sudoku

Monotonic Stack
Use when: Next greater element, largest rectangle in histogram

Trie (Prefix Tree)
Use when: Autocomplete, word search, prefix matching

PATTERN RECOGNITION CHEAT SHEET

If the problem says… → Think about…

Subarray of size K → Sliding Window
Pairs that sum to → Two Pointers
Detect cycle → Fast & Slow Pointers
Level by level → BFS
All paths → DFS / Backtracking
Order of completion → Topological Sort
Maximum / Minimum → Dynamic Programming
Next greater / smaller → Monotonic Stack
Prefix matching → Trie

START PRACTICING PATTERN-BASED

I’ve organized 223+ problems by these patterns at W Code:
WCode

• Free forever (200+ problems)
• TCS / Infosys / FAANG tracks
• Interactive visualizers
• AI resume analyzer

Pattern-based DSA for Indian placements.

Top comments (0)