Coding interview problems › Arrays & Hashing
#️⃣ Arrays & Hashing Medium
Longest Consecutive Sequence
Longest Consecutive Sequence in O(n) explained: put values in a hash set and only count upward from run starts, with tested Python code and a live visual.
Also searched as: longest consecutive elements sequence O(n) · longest run of consecutive integers
Try it live Free
Hash Tables
Insert keys into the hash table and see that checking whether a key exists costs O(1). This problem asks "is x - 1 here?" and "is x + 1 here?" over and over, which is only fast because of that.
The problem
Given an unsorted list of integers, return the length of the longest run of values that form consecutive integers (like 4, 5, 6, 7). The values may appear anywhere in the list, and the target is O(n) time.
The run is 1, 2, 3, 4.
0 through 8; duplicates do not extend a run.
The key insight
Put every value in a hash set. A number x is the start of a run only if x - 1 is not in the set. Count upward only from starts, so each value is visited by at most one counting loop and the total work stays linear.
Step-by-step approach
- Build
s = set(nums)to remove duplicates and allow O(1) lookups. - For each
xin the set, skip it ifx - 1is in the set (it is not a run start). - Otherwise walk
x, x+1, x+2, ...while the next value is in the set, counting the length. - Keep the maximum length seen and return it (0 for an empty list).
Python solution
def longest_consecutive(nums):
s = set(nums)
best = 0
for x in s:
if x - 1 in s:
continue # not the start of a run
y = x
while y + 1 in s:
y += 1
best = max(best, y - x + 1)
return bestComplexity
The inner while loop only runs from run starts, so across the whole algorithm each value is stepped over at most once. Set lookups are O(1) on average.
Common mistakes
- Dropping the
x - 1 in scheck, which re-counts every run from every member and becomes O(n²). - Iterating over
numsinstead of the set, so a list with many duplicates of a run start repeats the walk. - Returning 1 instead of 0 for an empty input.
Frequently asked questions
What is the time and space complexity of Longest Consecutive Sequence?
The hash set solution is O(n) time and O(n) space. Sorting first gives O(n log n) time with O(1) extra space if sorting in place is allowed.
Why is the nested while loop still O(n)?
It only starts from values whose predecessor is missing, so each value is part of exactly one walk. The total number of while-loop steps across all starts is at most n.
Can union-find solve this?
Yes, you can union x with x + 1 whenever both exist and track component sizes. It works but is longer than the set solution.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Arrays & Hashing Hash Tables lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.