Longest Consecutive Sequence
Find the length of the longest run of consecutive integer values in any order.
Why does this pattern fit?
Restate the exact job
Find the length of the longest run of consecutive integer values in any order.
Only values without a predecessor can begin a new run.
O(n) expected time · O(n) space
Starting a walk from every value repeats entire runs.
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.