Longest Consecutive Sequence

medium

You're handed a pile of integers in no particular order — nums. Hidden inside it, values may chain together into consecutive runs: 1, 2, 3, 4 counts as a run even when those numbers are scattered all over the array.

Return the length of the longest consecutive run of values that all appear in nums. Positions don't matter, duplicates count once, and an empty array has a longest run of 0.

The catch: your algorithm must run in O(n) time — sorting first won't meet the bar.

Hints

Sorting would line the numbers up — but the required O(n) bound rules it out. What structure answers does value x exist in O(1)?
Every consecutive run has exactly one smallest element. How can you tell, with a single lookup, that a number is the smallest of its run?
Put everything in a hash set. A number x starts a run exactly when x - 1 is absent — walk forward only from starts, and every run gets traversed once, for amortized O(n).

Common doubts

No — a run is about which values exist. [1, 0, 1, 2] has values 0, 1, 2, so the answer is 3.
Only if every element starts a walk. The num - 1 guard ensures each run is walked exactly once, so the total walk steps across the entire loop are at most n — amortized O(n).
0 — there is no run at all. Make sure your best-so-far starts at 0, not 1.
It would likely pass the time limit, but the statement explicitly requires O(n) — in an interview, the hash-set solution is the expected answer, with sorting as your warm-up.

Interview follow-ups

Yes — remember the start value of the best run; the sequence is start, start + 1, …, start + len - 1. Same complexity.
Maintain a hash map from each run's endpoints to its length; on inserting x, merge the run ending at x - 1 with the run starting at x + 1 and update both new endpoints. Each insert is O(1) average.
Generalize start detection: x starts a group when none of x - 1 … x - k are present, and walks may jump gaps up to k. With large sparse values, sorting or bucketing becomes the practical choice.

Fun facts

  • The start-detection trick is amortized analysis in disguise: the code looks quadratic, but each run can only be walked once, so the total work is linear — the same argument that powers two-pointer and monotonic-stack patterns.
  • This problem also falls to a union-find that unions x with x + 1, and to an endpoint-merging hash map — three different data structures, one shared insight: only the boundaries of a run matter.

Asked at

GoogleAmazonMetaMicrosoftAdobe
Frequently Sometimes Occasionally
Example 1
Input: nums = [100,4,200,1,3,2]
Output: 4
The longest consecutive run is `[1, 2, 3, 4]` — its length is 4.
Example 2
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
Every value from `0` through `8` appears, forming a run of length 9.
Example 3
Input: nums = [1,0,1,2]
Output: 3
The values `0, 1, 2` appear; the duplicate `1` counts once, so the answer is 3.
Constraints

- 0 <= nums.length <= 10^5 - -10^9 <= nums[i] <= 10^9

Solve this problem →