LeetCode 128 ·
Medium
Longest Consecutive Sequence
Find the length of the longest run of consecutive integers (order irrelevant).
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.
| Aspect | Value |
|---|---|
| Pattern | Hash map |
| Recognise it by | Longest run of consecutive integers, unsorted, in O(n). |
| Time complexity | O(n) |
| Space complexity | O(n) |
| Difficulty | Medium |
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.