Find a lesson, question or project

Start typing to search published content and planned structures.

↑ ↓ moveEnter openEsc close

PATTERN INDEX / 10

Start with the shape of the problem.

Every count below reflects published articles only. Planned practice stays on the individual pattern page.

016 AVAILABLE

Array Traversal

Read each value once while carrying only the state needed for the answer.

Recognition signals
every element matters · one pass is possible
Common invariant
After index i, the stored state correctly describes the processed prefix from 0 through i.
Expected complexity
Usually O(n) time
basics: 2easy: 4
Study this pattern →
022 AVAILABLE

Running Answer

Maintain the best answer seen so far instead of storing or sorting everything.

Recognition signals
largest or smallest seen so far · one result from an unsorted input
Common invariant
The running variables contain the best valid answer for the prefix already processed.
Expected complexity
Usually O(n) time
easy: 2
Study this pattern →
038 AVAILABLE

Two Pointers

Coordinate two positions so each movement removes work that never needs to be repeated.

Recognition signals
sorted input with a safe elimination rule · preserve order in place
Common invariant
Define what each pointer means and which positions are settled or candidates can safely be discarded; the exact promise depends on the problem.
Expected complexity
Often O(n) or O(n + m) time
basics: 1easy: 2medium: 4hard: 1
Study this pattern →
043 AVAILABLE

In-place Compaction

Use separate read and write positions to keep valid values at the front of the same array.

Recognition signals
modify the same array · preserve relative order
Common invariant
Positions before write contain exactly the valid values encountered so far, in original order.
Expected complexity
O(n) time
basics: 1easy: 2
Study this pattern →
054 AVAILABLE

Reversal

Reverse carefully chosen ranges to move array blocks without extra array storage.

Recognition signals
rotate in place · move a prefix behind a suffix
Common invariant
Each completed reversal places one required block orientation into the transformation sequence.
Expected complexity
O(n) time
easy: 1medium: 3
Study this pattern →
066 AVAILABLE

Hashing

Trade extra memory for fast lookup of information seen earlier.

Recognition signals
have we seen this before · count occurrences
Common invariant
The map contains exactly the historical facts needed to answer the current step.
Expected complexity
Usually O(n) expected time
basics: 1medium: 4hard: 1
Study this pattern →
073 AVAILABLE

Prefix Sum

Represent a range by subtracting two cumulative totals.

Recognition signals
contiguous subarray · range sum
Common invariant
prefix is the exact sum from index 0 through the current index.
Expected complexity
O(n) preprocessing or scan
basics: 1medium: 2
Study this pattern →
081 AVAILABLE

XOR Cancellation

Cancel paired values so the unpaired value remains.

Recognition signals
every value appears twice except one · numbers come from a known range
Common invariant
All matched values cancel regardless of processing order, while the unmatched value remains.
Expected complexity
O(n) time
easy: 1
Study this pattern →
0941 AVAILABLE

Recursion

Solve a problem by defining a smaller version of the same problem and a stopping condition.

Recognition signals
same task on smaller input · tree or directory structure
Common invariant
Each call owns one well-defined subproblem and moves closer to the base case.
Expected complexity
Depends on branches and depth
basics: 3easy: 3medium: 25hard: 10
Study this pattern →
1029 AVAILABLE

Backtracking

Explore a choice, undo it, then explore the next choice from the same clean state.

Recognition signals
all combinations · choose or skip
Common invariant
The current path represents exactly the decisions made before the current depth.
Expected complexity
Often exponential because the output is exponential
easy: 1medium: 20hard: 8
Study this pattern →