DSA Cheat Sheet

Big-O complexities, algorithm patterns, sorting comparisons, and interview tips — all on one page.

Data Structure Complexity

StructureAccessInsertDeleteSpaceNotes
ArrayO(1)O(n)O(n)O(n)Contiguous memory, cache-friendly
Linked ListO(n)O(1)*O(n)O(n)*O(1) insert if you have the node ref
Hash MapO(1) avgO(1) avgO(1) avgO(n)Amortized; worst case O(n) with collisions
Binary Search TreeO(log n)O(log n)O(log n)O(n)Balanced: AVL, Red-Black guarantee O(log n)
Heap / Priority QueueO(1) peekO(log n)O(log n)O(n)Min-heap or max-heap; used in top-K, Dijkstra
StackO(1)O(1)O(1)O(n)LIFO; used in DFS, expression eval, monotonic patterns
QueueO(1)O(1)O(1)O(n)FIFO; used in BFS, sliding window
TrieO(L)O(L)O(L)O(N*L)L = word length; prefix search, autocomplete
Graph (Adj List)O(V+E)O(1)O(V)O(V+E)BFS/DFS traversal; sparse graphs preferred

10 Must-Know Algorithm Patterns

Recognizing patterns is the single most important skill for coding interviews. Each pattern below includes when to use it, classic problems, and a pseudocode template.

Two Pointers

When: Array sorted or pair-sum problems

Classic problems: Two Sum II, Container With Most Water, 3Sum

for left=0, right=n-1: shrink based on comparison

Sliding Window

When: Subarray/substring with constraint (max, min, distinct)

Classic problems: Longest Substring Without Repeating, Min Window Substring

expand right, shrink left when constraint violated

Binary Search

When: Sorted array, search space with monotonic property

Classic problems: Search in Rotated Array, Koko Eating Bananas

lo, hi = bounds; mid = (lo+hi)//2; adjust based on condition

BFS / Level-order

When: Shortest path (unweighted), level-by-level tree traversal

Classic problems: Binary Tree Level Order, Rotting Oranges, Word Ladder

queue = deque([start]); process level by level

DFS / Backtracking

When: All paths, permutations, combinations, constraint satisfaction

Classic problems: Subsets, Permutations, N-Queens, Word Search

choose → explore → unchoose; prune early

Dynamic Programming

When: Overlapping subproblems + optimal substructure

Classic problems: Climbing Stairs, Longest Common Subsequence, Coin Change

dp[i] = f(dp[i-1], dp[i-2], ...); bottom-up or memoized top-down

Monotonic Stack

When: Next greater/smaller element, histogram problems

Classic problems: Daily Temperatures, Largest Rectangle in Histogram

maintain decreasing/increasing stack; pop when violated

Topological Sort

When: DAG ordering, dependency resolution, course scheduling

Classic problems: Course Schedule, Alien Dictionary

Kahn's (BFS + in-degree) or DFS post-order reversal

Union-Find

When: Connected components, cycle detection, grouping

Classic problems: Number of Islands, Redundant Connection, Accounts Merge

find(x) with path compression, union by rank

Greedy

When: Local optimal leads to global optimal; interval scheduling

Classic problems: Jump Game, Merge Intervals, Task Scheduler

sort by end time / deadline; process greedily

Sorting Algorithm Comparison

AlgorithmBestWorstSpaceStable?Notes
Bubble SortO(n²)O(n²)O(1)YesEducational only
Selection SortO(n²)O(n²)O(1)NoMinimal swaps
Insertion SortO(n)O(n²)O(1)YesBest for nearly sorted
Merge SortO(n log n)O(n log n)O(n)YesStable, predictable
Quick SortO(n log n)O(n²)O(log n)NoFast in practice; partition-based
Heap SortO(n log n)O(n log n)O(1)NoIn-place, not stable
Counting SortO(n+k)O(n+k)O(k)Yesk = range; integer keys only
Radix SortO(d*(n+k))O(d*(n+k))O(n+k)Yesd = digits; non-comparison

Big-O Complexity Hierarchy

From fastest to slowest growth. For n = 1,000,000:

O(1)
Constant — hash lookup
O(log n)
Logarithmic — binary search (~20 ops)
O(n)
Linear — single pass (1M ops)
O(n log n)
Linearithmic — merge sort (~20M ops)
O(n²)
Quadratic — nested loops (1T ops, too slow)
O(2ⁿ)
Exponential — brute-force subsets
O(n!)
Factorial — permutations (n>20 infeasible)

Interview Quick Tips

Before Coding

  • Clarify inputs, outputs, edge cases
  • Ask about constraints (n size, duplicates, sorted?)
  • Walk through 1-2 examples out loud
  • State your approach + time complexity BEFORE coding

While Coding

  • Use meaningful variable names
  • Talk through your logic as you write
  • Handle edge cases (empty, single element, overflow)
  • Don't optimize prematurely — get a working solution first

After Coding

  • Trace through with a test case
  • State time and space complexity
  • Discuss trade-offs and potential optimizations
  • Ask if the interviewer wants you to optimize

Common Mistakes

  • Jumping to code without understanding the problem
  • Off-by-one errors in loops and indices
  • Forgetting to handle null / empty inputs
  • Not considering integer overflow for large inputs

Ready to Practice?

Apply these patterns to 57+ curated problems with Java solutions.