DSA Guide
Patterns

Patterns

A cross-cutting map of the techniques in this guide, and how to recognise which one a problem wants

Most interview problems are variations on a small number of techniques. Learning to recognise the pattern is worth more than memorising any individual solution — and it is what turns an unfamiliar problem into a familiar one.

This page maps every technique in the guide to the problems that teach it.

The Recognition Table

Start here. The wording of a problem usually points straight at the technique.

The problem says…Reach forStart with
"have I seen this / find a pair"Hash map or setTwo Sum
"the array is sorted"Two pointers or binary searchMerge Sorted Array
"subarray of size k"Fixed sliding windowMaximum Average Subarray I
"sum over a range"Prefix sumsRunning Sum of 1d Array
"in place, O(1) space"Read and write pointersRemove Duplicates II
"matching / nesting / undo"StackValid Parentheses
"level by level / fewest steps"BFS with a queueLevel Order Traversal
"explore every path / subtree"DFS with recursionN-ary Preorder
"how many ways / min cost"Dynamic programmingClimbing Stairs
"can I reach / is it possible"Greedy, if provableJump Game
"find the first X where…"Binary search on a boundaryFirst Bad Version
"k most frequent / largest"Counting + bucket or heapTop K Frequent
"group things that are equivalent"Canonical key + hash mapGroup Anagrams
"support these ops in O(1)"Combine structuresInsert Delete GetRandom

The Ten Patterns

1. Hash Map / Set — trade memory for time

Turn an O(n) search into an O(1) lookup. The single highest-leverage move in this guide.

Two Sum · Contains Duplicate · Group Anagrams · Valid Anagram · Minimum Index Sum · Unique Email Addresses

2. Two Pointers — converging

Start at both ends and move inward under a rule that provably discards possibilities.

Container With Most Water · Valid Palindrome · Trapping Rain Water

3. Two Pointers — same direction

Both pointers move forward, at different rates or under different conditions.

Is Subsequence · Remove Duplicates II · Merge Sorted Array

4. Fast & Slow Pointers

One pointer moves twice as fast. Finds middles, detects cycles, locates offsets.

Middle of the Linked List · Linked List Cycle II · Intersection of Two Lists

5. Sliding Window

A range that slides or grows across the data. Fixed size for "subarray of size k"; variable size for "longest such that…".

Maximum Average Subarray I

6. Prefix / Suffix Precomputation

One pass forward, one pass back, then combine. Turns range queries into O(1) arithmetic.

Running Sum · Find Pivot Index · Product of Array Except Self · Candy · Trapping Rain Water

7. Stack

Last-in-first-out. Nesting, undo, deferred operands, depth-first traversal.

Valid Parentheses · Min Stack · Evaluate RPN · Simplify Path

8. BFS / DFS

Queue for breadth, stack for depth. The choice determines both the visit order and the space cost.

Level Order Traversal · N-ary Preorder · Jump Game II

Halve the search space. Works for exact matches, for boundaries, and even when there is no array.

Binary Search · Search Insert Position · First Bad Version

10. Greedy vs. Dynamic Programming

Greedy when the local choice is provably safe; DP when it is not.

Jump Game · Gas Station · Candy · Integer to Roman · Climbing Stairs

Complexity Cheat Sheet

NotationMeaningExample
O(1)constanthash map lookup
O(log n)halving each stepbinary search
O(n)one passa single loop
O(n log n)sortingcomparison sorts
O(n²)nested loopsbrute-force pairs
O(2ⁿ)branching recursionunmemoized subsets

Constraints tell you the target complexity

n ≤ 10⁵ rules out O(n²) — that would be 10 billion operations. It points at O(n) or O(n log n).

n ≤ 20 is small enough for exponential, which usually means the intended solution is backtracking or bitmasking.

Reading the constraints first often tells you which technique the author had in mind.

On this page