Cheatsheet

Pattern recognition

Signals in a problem statement, the technique they usually point to, its typical cost, and a solved problem that shows it.

Version 1 · updated 2026-11-24

Arrays and strings

Signal → technique → typical cost
Signal in the problemTechniqueTypical timeExample
Sorted input, find a pair or meet a targetTwo pointers from both endsO(n)Two prices that fit the budget
Unsorted input, "does some pair add up to…"Hash map of complementsO(n) expectedTwo readings that add up
"Has this been seen before?"Hash setO(n) expectedBadge scanned twice
Contiguous block of fixed length kFixed sliding windowO(n)Busiest stretch of k hours
Longest / shortest contiguous block with a condition that stays true when shrinkingVariable sliding windowO(n)Longest stretch without repeats
Many range sums, or count subarrays summing to kPrefix sums (+ hash map of seen sums)O(n) build, O(1) per queryStretches that add up to k
"Product / sum of everything except me"Prefix and suffix passesO(n)Every product but your own
Compact or filter in placeRead/write pointersO(n), O(1) spacePush the empty slots to the end
Three groups in one passThree-way partitionO(n)Red, white and blue
Best sum of a contiguous runKadane’s algorithmO(n)Best streak of profits
An element occurring more than n/2 timesMajority voteO(n), O(1) spaceMore than half the votes
Every value appears twice except oneXOR everythingO(n), O(1) spaceThe sock without a pair

Graphs and search spaces

Signal → technique → typical cost
Signal in the problemTechniqueTypical timeExample
Fewest steps, unweighted moves, levelsBFSO(V + E)Shortest path in a grid
Explore everything reachable, count regionsDFS or BFSO(V + E)Flood fill, connected components
Groups that merge over time, "are a and b connected?"Union–Findnear O(1) amortized per operationDynamic connectivity
Tasks with prerequisitesTopological sortO(V + E)Build order, course schedule
Cheapest path, non-negative weightsDijkstra with a binary heapO((V + E) log V)Road network routing
Connect everything at minimum total costKruskal or PrimO(E log E) / O(E log V)Cabling a set of buildings
All subsets, permutations or placementsBacktracking with pruningO(2ⁿ) / O(n!) worstN-Queens, Sudoku
Count ways or optimise over choices, same subproblems recurDynamic programmingstates × transitionsNo two neighbours
Local choice provably never hurts (exchange argument)Greedyoften O(n log n) (sort first)Buy once, sell once
Overlapping intervalsSort by start, then sweepO(n log n)Merge meeting times

Reading the constraints

What input size usually allows (rough, at ~10⁸ simple steps per second)
n up toUsually acceptableTypical techniques
10 – 12O(n!), O(n² · 2ⁿ)Permutations, bitmask DP over subsets
20 – 25O(2ⁿ), O(n · 2ⁿ)Subsets, backtracking, meet in the middle at ~40
~500O(n³)Triple loops, Floyd–Warshall, interval DP
~5,000O(n²)All pairs, 2D DP
~10⁶O(n log n)Sorting, heaps, binary search per element
~10⁸ or moreO(n) or betterOne pass, two pointers, math, O(log n) search
A guide for picking a direction, not a rule: constant factors and the language matter.
  • Before reaching for a pattern, write the brute force and its cost. The pattern is whatever removes the repeated work.
  • Sorted input, or the freedom to sort, suggests two pointers or binary search.
  • "Contiguous" suggests windows or prefix sums; "subsequence" suggests DP.
  • A window only works when shrinking it never makes an invalid window valid again (all-positive sums, at most k distinct). With negative numbers, use prefix sums.

References