LC 128
LeetCode 128 · Medium

Longest Consecutive Sequence

Find the length of the longest run of consecutive integers (order irrelevant).

Hash map → · High frequency · Solve on LeetCode →

Try it

Step through the core mechanic. The simulator below runs the hash map shape this problem is built on.

Walk the pattern

No dedicated step-through for this one yet. The shape is Hash map — its pattern page has the interactive walkthrough, the reference implementation, and a five-problem progression that this problem sits inside.

The approach

Put everything in a set. Only start counting from a value whose predecessor is absent (a run start), then walk upward. Each element is visited at most twice, so it is O(n) despite the nested look.

AspectValue
PatternHash map
Recognise it byLongest run of consecutive integers, unsorted, in O(n).
Time complexityO(n)
Space complexityO(n)
DifficultyMedium

Who asks it

Companies known to ask this problem, from public LeetCode company-tag aggregations. A signal of where to expect it, not a guarantee.