5 patterns · 24 problems

Arrays

The patterns behind most array and subarray problems — walk pointers, slide windows, and precompute totals to turn O(n²) into O(n).

Each pattern below starts with when to reach for it and the clue in the prompt that gives it away. Then it runs on real problems: pick one to watch the technique step by step, open its full solution, or practise it on an original problem with graded tests.

01Pattern

Two Pointers

Walk two indices toward each other and skip the wasted work.

This patternO(n)naïve O(n²)

When to reach for it

  • You need to iterate with two indices, typically starting from opposite ends or different positions in the array.
  • The task involves comparing or manipulating elements from two different parts of the array at the same time.

Spot it in the prompt

The problem mentions a sorted array, or you need to compare elements from both ends of the array.

Easy Custom animation

Two Sum

LeetCode(opens in a new tab)
Loading animation…

How to solve it

On a sorted array, put one pointer at each end and move them inward: if the pair sums too high move the right pointer left, too low move the left pointer right.

Ledger Reconciliation is our own problem on the same technique: a new statement, graded in your browser.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

02Pattern

Merge Intervals

Sort by start, then fold overlapping ranges into one.

This patternO(n log n)naïve O(n²)

When to reach for it

  • The problem involves intervals or ranges — time slots, scheduling, or overlapping events.
  • You need to combine, compare, merge, or find intersections between intervals.

Spot it in the prompt

The input is intervals represented as start/end pairs, and the task is about combining or manipulating those intervals.

Medium Custom animation

Merge Intervals

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Sort by start time, then walk the list folding each interval into the current one whenever they overlap.

Pipeline Sensor Coverage is our own problem on the same technique: a new statement, graded in your browser.

Practice · 4 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

03Pattern

Sorting

Impose order first so the rest of the problem falls out.

This patternO(n log n)naïve O(n²)

When to reach for it

  • Arranging elements in a specific order simplifies searching, counting, or comparing them.
  • Sorting by some criteria unlocks the solution or optimizes the operations that follow.

Spot it in the prompt

The problem hints the array needs to be sorted first, or that ordering the data would make the solution obvious.

Medium Custom animation

Sort Colors

LeetCode(opens in a new tab)
Loading animation…

How to solve it

The Dutch national flag algorithm: low, mid and high pointers partition 0s, 1s and 2s into place in a single pass.

Triage Band Ordering is our own problem on the same technique: a new statement, graded in your browser.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

04Pattern

Sliding Window

Keep a moving window and update the answer as it slides.

This patternO(n)naïve O(n²)

When to reach for it

  • The problem involves contiguous subarrays or substrings where you slide a window across the input.
  • You need to track a subset of elements that satisfies a specific condition as it moves.

Spot it in the prompt

The input involves contiguous subarrays, a "window" that slides across the array, or properties like "maximum sum" or "minimum length".

Medium Custom animation

Minimum Size Subarray Sum

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Grow the window to the right until the sum reaches the target, then shrink it from the left as far as possible — the shortest valid window is the answer.

Fewest Shifts to Clear the Quota is our own problem on the same technique: a new statement, graded in your browser.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

05Pattern

Prefix Sums

Precompute running totals to answer range queries in O(1).

This patternO(1) / querynaïve O(n) / query

When to reach for it

  • You need cumulative sums or averages over subarrays, or must answer many subarray-sum queries efficiently.
  • Precomputing aggregate values up front can cut the time complexity of repeated range operations.

Spot it in the prompt

The problem mentions subarray sums, cumulative sums, or range sums, and hints that precomputing sums would speed things up.

Medium Custom animation

Range Sum Query - Mutable

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Prefix sums answer any range in O(1), but a single update forces you to rebuild them in O(n) — a Fenwick (binary indexed) tree keeps both query and update logarithmic.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

After the pattern

Now solve one you haven't seen.

Reading a pattern isn't the same as using it. The practice set has 1,194 original problems on the same techniques, each graded in your browser.

1,194 problems · 15,062 graded tests · free