Skip to content

Practice Longest Consecutive Sequence

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#128
LeetCode Problem

Longest Consecutive Sequence

Problem Statement

Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. You must write an algorithm that runs in O(n) time.

Source: Longest Consecutive Sequence on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [100,4,200,1,3,2]
Output
4

# Example 2

Input
nums = [0,3,7,2,5,8,4,6,0,1]
Output
9

# Example 3

Input
nums = [1,0,1,2]
Output
3

Constraints

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

How to solve Longest Consecutive Sequence

Original notes for interview preparation, written for this problem. Reviewed .

Key insight

Put every value in a hash set and only start counting from numbers whose predecessor is absent. Each number is then visited by at most one upward walk, which turns an apparently quadratic scan into linear time.

Why brute force is not enough

Sorting and scanning for runs costs O(n log n), which violates the required O(n). Checking, for every element, how far its run extends by repeatedly searching the array is O(n^2), hopeless for n up to 10^5.

Approach

  1. Insert all values into a hash set, which also collapses duplicates.
  2. Iterate over the set. For a value whose predecessor (value minus one) is in the set, skip it: it is in the middle of some run and will be counted when that run's start is processed.
  3. For a value whose predecessor is absent, it is the start of a run. Walk upward while value plus one is present, counting the length.
  4. Keep the maximum run length seen. Return it, or zero for an empty array.

Walkthrough of Example 1

nums = [100,4,200,1,3,2] → 4

  1. The set is {100, 4, 200, 1, 3, 2}.
  2. 100: 99 is absent, so it starts a run; 101 is absent, so the run length is 1.
  3. 4: 3 is present, so it is skipped. 200: 199 is absent and 201 is absent, run length 1.
  4. 1: 0 is absent, so it starts a run; 2, 3, and 4 are present and 5 is not, giving length 4.
  5. 3 and 2 are skipped because 2 and 1 are present. The maximum is 4.

Time and space

Time O(n) · Space O(n). Each of the n values is inserted once and visited by at most one upward walk because walks begin only at run starts; the set holds up to n entries.

Interview pitfalls

  • Starting an upward walk from every element instead of only from run starts, which is O(n^2) on an already consecutive input.
  • Returning 1 instead of 0 for an empty array; the constraint allows length zero.
  • Duplicates such as [1,0,1,2]: counting from the original list rather than the set can produce an inflated length.
  • Reaching for a counting array; values span -10^9 to 10^9, so only a hash set is practical.
  • Treating the sort-based solution as acceptable without acknowledging it breaks the explicit O(n) requirement.

Follow-up questions interviewers ask

  • How would you return the actual elements of the longest run, not just its length?
  • What if numbers arrive as a stream and you must report the longest run after each insertion?
  • Can you solve it with union-find, and what does its complexity look like?

Further reading on the technique: Hash table

Related LeetCode Problems

Practice more problems similar to Longest Consecutive Sequence.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.