These are typical complexities, not guarantees. The real cost depends on the problem, on how you implement the pattern, and on any extra work such as sorting the input first. Always check the bound against your own code.

Coding patternTypical time complexityTypical auxiliary spaceTypical problems and caveats
Two pointersO(n)O(1)Pair sum on a sorted array, removing duplicates in place, palindrome check. If the input is not sorted and the pattern needs it, add O(n log n) for the sort.
Sliding windowO(n)O(1) or O(n)Longest substring or subarray under a constraint. Space is O(1) for a running sum or count, and grows when the window stores its contents, such as a character map or a deque for the window maximum.
Prefix sumsO(n) preprocessing; O(1) per range sumO(n)Many range-sum queries on a fixed array. Updates to the array force a rebuild, or a Fenwick or segment tree instead.
Hash map frequency countingExpected O(n) totalO(n)Anagrams, duplicates, two-sum. Relies on expected O(1) hash operations; heavy collisions can make a single operation O(n).
Monotonic stackO(n) totalO(n)Next greater element, daily temperatures, largest rectangle in a histogram. Each item is pushed and popped at most once.
Backtracking and subsetsO(2n) output generationDepends on implementationRecursion depth alone is O(n). Storing every subset needs O(n·2n) space, and copying each subset into the result adds a factor of n to the time.
PermutationsO(n·n!) to generate all permutationsDepends on output and implementationThere are n! permutations of length n. Recursion depth is O(n); storing all of them takes O(n·n!).
Dynamic programmingNumber of states × transitions per stateDepends on memoization or tabulationKnapsack with n items and capacity W has n·W states with two transitions each, so O(n·W) time. Tabulation often lets you keep only the previous row, cutting space to O(W). Memoization also uses recursion stack space.

Topics on this page

Each one has its own page with an explanation, code, and the mistakes people make.

Keyboard shortcuts

/
Search topics
?
Show this list of shortcuts
r
Open the revision sheet
b
Save or unsave the focused card, or the topic you are reading
l
Mark the focused card, or the topic you are reading, as learned
Space Left arrow Right arrow
In the visualizer: play or pause, step back, step forward
1 to 4 Enter
In the quiz: pick an answer, then go to the next question
Escape
Close this list