Skip to content

Cheat sheet

Big-O for common data structures and sorts, what input size tells you, and how to recognise each pattern.

Data structures

StructureAccessSearchInsertDeleteSpaceNotes
Array / Python listO(1)O(n)O(1)*O(n)O(n)Index by position. Append is amortized O(1); insert or delete at the front shifts everything.
Hash map / dict / Mapn/aO(1)O(1)O(1)O(1)Average case. Worst case O(n) with bad hashing. Keys must be hashable.
Hash set / set / Setn/aO(1)O(1)O(1)O(1)Membership in constant time. No order guarantees in Python sets.
Stack (list / array)O(1) topO(n)O(1)O(1) popO(n)push and pop at the end only.
Queue / dequeO(1) endsO(n)O(1)O(1)O(n)Use collections.deque in Python. Array.shift() in JS is O(n).
Linked listO(n)O(n)O(1)†O(1)†O(n)† when you already hold the node. Great for splicing, bad for indexing.
Binary heapO(1) minO(n)O(log n)O(log n) popO(n)heapq is a min-heap. Build from a list in O(n) with heapify.
Balanced BSTO(log n)O(log n)O(log n)O(log n)O(n)Sorted order. Python has none built in (use bisect on a sorted list or sortedcontainers).
Trien/aO(k)O(k)O(k)O(total chars)k = word length. Prefix queries.
Graph (adjacency list)n/aO(deg)O(1)O(deg)O(V + E)BFS and DFS both run in O(V + E).

Sorting

AlgorithmAverageWorstExtra spaceStable
Merge sortO(n log n)O(n log n)O(n)Yes
QuicksortO(n log n)O(n²)O(log n)No
Heap sortO(n log n)O(n log n)O(1)No
Timsort (Python, V8)O(n log n)O(n log n)O(n)Yes
Insertion sortO(n²)O(n²)O(1)Yes
Counting sortO(n + k)O(n + k)O(k)Yes

Read the constraints

Input size tells you which complexity the interviewer expects. A computer does roughly 10⁸ simple operations per second.

Input sizeTarget complexityTypical approach
n ≤ 10O(n!) or O(2ⁿ · n)Backtracking over every permutation
n ≤ 20O(2ⁿ)Subsets, bitmask DP
n ≤ 500O(n³)Triple loops, Floyd–Warshall
n ≤ 5,000O(n²)Nested loops, 2D DP
n ≤ 10⁶O(n log n) or O(n)Sorting, heaps, two pointers, hashing
n ≤ 10⁹+O(log n) or O(1)Binary search, math

Patterns and their giveaways

PatternYou will hearIdea
Arrays & Hashing"Have I seen this before?" · duplicates · frequency · groupingCount, group, and look things up in O(1).
Two PointersSorted input · pairs that sum to a target · palindromes · in-placeWalk inward or in step to avoid nested loops.
Sliding WindowLongest / shortest contiguous subarray or substring with a ruleGrow and shrink a range across the input.
StackMatching brackets · undo · next greater element · evaluate expressionsLast in, first out, for nesting and "next greater".
Binary SearchSorted data · "minimum x such that…" · rotated arrays · O(log n) askedHalve the search space every step.
Linked ListReverse · merge · find the middle · detect a cycleRewire pointers, use fast and slow runners.
TreesHierarchies · depth · paths · BST orderingRecursion with DFS, levels with BFS.
Heap / Priority QueueTop k · k-th largest · merge sorted streams · schedulingAlways grab the smallest or largest fast.
BacktrackingAll subsets / permutations / combinations · puzzlesTry, recurse, undo. Explore every choice.
GraphsGrids · networks · dependencies · shortest path · islandsBFS, DFS, topological sort, union-find.
Dynamic ProgrammingCount the ways · min/max cost · "can you reach…" · overlapping choicesReuse answers to smaller subproblems.
GreedyJump games · scheduling · "maximum number of…"Take the best local choice when it is provably safe.
IntervalsMeetings · overlapping ranges · insert a rangeSort by start, then merge or sweep.
Bit Manipulation & Math"Without extra space" · single number · powers of two · digitsXOR tricks, masks, and number properties.

Before you code

  1. Restate the problem and confirm the input and output types.
  2. Walk one small example by hand, then one edge case: empty, single item, duplicates, negatives.
  3. Say the brute force and its complexity out loud.
  4. Name the pattern you think fits and why.
  5. Agree on the plan, then code it while narrating.
  6. Trace your code on the example, fix bugs, and state time and space complexity.