PROBLEM 07 OF 75

Longest Consecutive Sequence

Find the length of the longest run of consecutive integer values in any order.

PATTERNArray scans & hashingName what you need to remember before choosing the container.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Find the length of the longest run of consecutive integer values in any order.

WHY THIS FITS

Only values without a predecessor can begin a new run.

COMPLEXITY

O(n) expected time · O(n) space

COMMON FAILURE

Starting a walk from every value repeats entire runs.

THE REASONING, IN ONE PLACE

How to solve Longest Consecutive Sequence

The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.

What the question asks

Find the length of the longest run of consecutive integer values in any order.

Why Array scans & hashing fits

Only values without a predecessor can begin a new run.

State to maintain

A set for membership and a best length.

Transition

For each run start, walk upward until the next value is absent.

Time and space

O(n) expected time · O(n) space

Counterexample to the tempting mistake

Starting a walk from every value repeats entire runs.

Prove it again tomorrow

Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.

Open blank recall ↗