Try it first — give it 25 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
Given an unsorted array nums, return the length of the longest sequence of consecutive integers whose values all appear in the array. The values can be in any order in the array. Your algorithm must run in O(n).
nums = [100, 4, 200, 1, 3, 2] → 4, because 1, 2, 3, 4 are all present.
Start with brute force
Sort, then scan. After sorting, consecutive values sit next to each other. Walk the array, extending the current run when nums[i] == nums[i−1] + 1, skipping duplicates, and resetting otherwise.
That's O(n log n). Good to mention, but the problem explicitly asks for O(n), so sorting is out.
Building the intuition
Put every value in a hash set, so "is x + 1 present?" is O(1).
Now, from any number you could count upward: x, x+1, x+2, … until a number is missing. But if you do that from every number, a run of length L gets re-walked from each of its L members. For [1, 2, 3, …, n] that's O(n²).
The fix: only start counting at the beginning of a run. A number x begins a run exactly when x − 1 is not in the set.
set: {1, 2, 3, 4, 100, 200}
1 → 0 not in set → START: count 1, 2, 3, 4 → length 4
2 → 1 is in set → skip (it's in the middle of a run)
3 → skip
4 → skip
100 → 99 missing → START: length 1
200 → 199 missing → START: length 1Every number is visited at most twice — once in the outer loop and once while some run counts through it — so the total is O(n).
The optimal approach
- Put all numbers in a set
s. - For each
xins(iterate the set, so duplicates don't repeat work):- If
x − 1is ins, skip —xisn't a start. - Otherwise count
length = 1and keep going whilex + lengthis ins. - Update
best = max(best, length).
- If
- Return
best.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
s = set(nums)
best = 0
for x in s: # iterate the SET, so duplicates don't repeat work
if x - 1 in s:
continue # x is in the middle of a run, not its start
length = 1
while x + length in s: # count upward from the start
length += 1
best = max(best, length)
return bestclass Solution {
public:
int longestConsecutive(vector<int>& nums) {
unordered_set<int> s(nums.begin(), nums.end());
int best = 0;
for (int x : s) { // iterate the SET, not the array
if (s.count(x - 1)) continue; // not the start of a run
int length = 1;
while (s.count(x + length)) length++; // count upward from the start
best = max(best, length);
}
return best;
}
};class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> s = new HashSet<>();
for (int x : nums) s.add(x);
int best = 0;
for (int x : s) { // iterate the SET, not the array
if (s.contains(x - 1)) continue; // not the start of a run
int length = 1;
while (s.contains(x + length)) length++; // count upward from the start
best = Math.max(best, length);
}
return best;
}
}/**
* @param {number[]} nums
* @return {number}
*/
var longestConsecutive = function (nums) {
const s = new Set(nums);
let best = 0;
for (const x of s) { // iterate the SET, not the array
if (s.has(x - 1)) continue; // not the start of a run
let length = 1;
while (s.has(x + length)) length++; // count upward from the start
best = Math.max(best, length);
}
return best;
};Dry run
nums = [0, 3, 7, 2, 5, 8, 4, 6, 0, 1] → set {0, 1, 2, 3, 4, 5, 6, 7, 8}
| x | x − 1 in set? | action | best |
|---|---|---|---|
| 0 | no (−1) | count 0 → 8: length 9 | 9 |
| 1 – 8 | yes | skip | 9 |
Answer: 9. Nine numbers, nine steps of counting, plus nine set checks.
Complexity
- Time: O(n). Building the set is O(n). Each run is walked once from its start, so inner loops add up to n steps in total.
- Space: O(n) for the set.
Edge cases and traps
- Empty array. Return 0.
- Duplicates like
[1, 2, 2, 3]. Iterate over the set, not the array, or you'll re-scan from repeated starts. The answer is 3. - Negative numbers.
x − 1works the same way. - Iterating the array but checking starts. Still O(n) in most cases, but a huge array of the same start value (e.g. many copies of 1 followed by a long run) repeats the inner walk. Iterating the set avoids it.
Say it in the interview
"Sorting gives O(n log n), but they want O(n). I put everything in a hash set. A number starts a run only if its predecessor is missing, so I only count upward from those starts. Each number is touched a constant number of times, which gives O(n) time and O(n) space."
Follow-ups interviewers ask
- "Return the sequence itself?" Remember the start and length of the best run, then rebuild it.
- "What if numbers arrive one at a time?" Union-Find (later in the bonus stage) merges a number with its neighbours as they arrive.
- "Can you do it without extra memory?" Only by sorting in place — O(n log n) time, O(1) space.
Practise the same idea
This is Day 17 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free