20 Essential DSA Coding Patterns

Pattern Framework

Master the mental blueprints behind 95% of LeetCode & FAANG algorithmic interview problems.

Pattern Mastery
0/ 20 Mastered

0% syllabus completed

0%
Why Patterns Matter

Instead of memorizing 500+ individual questions, recognizing these 20 patterns unlocks instant solution formulation during high-pressure live coding rounds.

Direct Practice

Put these patterns to work on our curated 30-problem DSA sheets with full code walkthroughs.

Open DSA Sheets
Linear Arrays

1. Sliding Window

When to Apply (Interview Trigger)

Problem asks for longest/shortest substring, sub-array with sum/target K, or contiguous elements.

Maintain two pointers (left, right) representing a window that expands to the right to include new elements and shrinks from the left when window criteria are invalidated.
Typical:O(N)O(K) or O(1)
Pending practice
Sorted Arrays & Lists

2. Two Pointers

When to Apply (Interview Trigger)

Sorted arrays or strings where you need pairs, triplets, or in-place swapping from opposite ends.

Position one pointer at start and one at end (or both moving in the same direction at varying rates). Narrow search space by testing criteria and advancing pointers.
Typical:O(N)O(1)
Pending practice
Linked Lists & Cycles

3. Fast & Slow Pointers (Hare & Tortoise)

When to Apply (Interview Trigger)

Detecting cycles in a linked list, finding the middle node, or finding the start of a loop.

Use two pointers moving at different speeds (slow moves 1 step, fast moves 2 steps). If a cycle exists, they must eventually meet.
Typical:O(N)O(1)
Pending practice
Interval Scheduling

4. Merge Intervals

When to Apply (Interview Trigger)

Problems involving overlapping intervals, calendar appointments, or ranges.

Sort intervals by their start time. Iterate through: if the current interval starts before or at previous interval ends, merge by updating end = max(end1, end2).
Typical:O(N log N)O(N)
Pending practice
Array Permutations

5. Cyclic Sort

When to Apply (Interview Trigger)

Array contains numbers in a given range [1, N] or [0, N], asking to find missing, duplicate, or corrupted values.

Swap each number to its correct index (i.e. value x belongs at index x - 1) in a while loop. Then iterate once more to detect mismatches.
Typical:O(N)O(1)
Pending practice
Pointers

6. In-place Reversal of a LinkedList

When to Apply (Interview Trigger)

Reverse a sublist or full linked list without allocating auxiliary node storage.

Track prev, current, next pointers and iteratively reverse node.next pointers in-place.
Typical:O(N)O(1)
Pending practice
Level Order

7. Tree Breadth First Search (BFS)

When to Apply (Interview Trigger)

Traverse trees level-by-level, find shortest paths, or connect level siblings.

Use a FIFO Queue. Record queue length at start of each iteration to process exactly one tree depth tier at a time.
Typical:O(N)O(W)
Pending practice
Root-to-Leaf

8. Tree Depth First Search (DFS)

When to Apply (Interview Trigger)

Path sums, height/diameter of tree, or sub-tree validation.

Use recursion or an explicit LIFO Stack to plunge down branches (pre-order, in-order, or post-order).
Typical:O(N)O(H)
Pending practice
Priority Queues

9. Two Heaps

When to Apply (Interview Trigger)

Find median in a dynamic data stream or partition elements into two halves.

Maintain a Max-Heap for the smaller half of numbers and a Min-Heap for the larger half. Balance heap sizes so top elements immediately yield the median.
Typical:O(log N) insertionO(N)
Pending practice
Combinatorics

10. Subsets & Backtracking

When to Apply (Interview Trigger)

Find all permutations, combinations, or power sets of elements.

Explore decision trees: Choose an element, recursively explore subsequent states, then backtrack (un-choose) to restore state.
Typical:O(2^N) or O(N!)O(N)
Pending practice
Logarithmic Search

11. Modified Binary Search

When to Apply (Interview Trigger)

Searching for values in sorted or rotated arrays in O(log N) time.

Calculate mid = l + Math.floor((r - l) / 2). Identify which half is monotonically sorted, then check if target falls within that half.
Typical:O(log N)O(1)
Pending practice
Min/Max Heaps

12. Top "K" Elements

When to Apply (Interview Trigger)

Find K largest, smallest, or most frequent items from an unsorted collection.

Maintain a Min-Heap of size K. When processing next element, if greater than heap root, evict root and insert new element.
Typical:O(N log K)O(K)
Pending practice
Heaps & Lists

13. K-way Merge

When to Apply (Interview Trigger)

Merging K sorted arrays or lists into a single consolidated sorted stream.

Insert the first element of each of the K arrays into a Min-Heap. Continuously pop the minimum, append to result, and insert the next element from that array.
Typical:O(N log K)O(K)
Pending practice
Optimization

14. 0/1 Knapsack (Dynamic Programming)

When to Apply (Interview Trigger)

Given items with weights and profits, find optimal combination under weight limit.

For each item, compute max(excluding item, including item + remainder capacity). Compress 2D table into 1D array by iterating backwards.
Typical:O(N * W)O(W)
Pending practice
Directed Acyclic Graphs (DAG)

15. Topological Sort

When to Apply (Interview Trigger)

Scheduling tasks with prerequisites, build order, or detecting cycles in directed graphs.

Use Kahn's Algorithm (in-degrees array + BFS queue of 0 in-degree nodes). If total processed nodes < V, graph contains a cycle.
Typical:O(V + E)O(V + E)
Pending practice
Next Greater / Smaller

16. Monotonic Stack

When to Apply (Interview Trigger)

Find the next greater or smaller element, daily temperatures, or largest rectangle in histogram.

Maintain a stack whose elements are strictly increasing or decreasing. Elements get popped when current value violates monotonicity.
Typical:O(N)O(N)
Pending practice
Range Queries

17. Prefix Sum

When to Apply (Interview Trigger)

Answering contiguous subarray sum queries in O(1) time, or counting subarrays with sum = K.

Compute prefix[i] = prefix[i-1] + arr[i]. Sum between indices [L, R] = prefix[R] - prefix[L - 1]. Pair with hash map to count sums.
Typical:O(N) build, O(1) queryO(N)
Pending practice
Connected Components

18. Union-Find (Disjoint Set Union)

When to Apply (Interview Trigger)

Determining if two nodes belong to the same component, dynamic connectivity, or Kruskal minimum spanning tree.

Use parent array with path compression and rank/size balancing to connect and query components.
Typical:O(α(N)) near O(1)O(N)
Pending practice
XOR & Bitmasks

19. Bit Manipulation

When to Apply (Interview Trigger)

Numbers appearing twice except one, counting set bits, or subsets without recursion.

Exploit properties like x ^ x = 0, x ^ 0 = x, and n & (n - 1) to clear the lowest set bit.
Typical:O(1) to O(N)O(1)
Pending practice
String Searching & Autocomplete

20. Trie (Prefix Tree)

When to Apply (Interview Trigger)

Fast prefix lookups, dictionary word autocomplete, or prefix matching.

Tree data structure where each node stores a hash map or 26-element array of child characters and an isEndOfWord flag.
Typical:O(L) where L = word lengthO(ALPHABET_SIZE * N * L)
Pending practice