Longest Consecutive Sequence in Python: O(n) Hash Set Solution Explained

What Is Longest Consecutive Sequence in Python?

The Longest Consecutive Sequence in Python problem is one of the most popular coding interview questions asked by top technology companies such as Google, Amazon, Microsoft, Meta, and Netflix.

At first glance, the problem appears straightforward. However, the requirement to achieve O(n) time complexity makes it an excellent test of your understanding of Hash Sets, optimization techniques, and algorithm design.

In this tutorial, you’ll learn how to solve the Longest Consecutive Sequence in Python problem efficiently using a Hash Set while understanding the intuition behind the solution.

Longest Consecutive Sequence in Python

Problem Statement

Given an unsorted array of integers:

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

Return the length of the longest consecutive elements sequence.

Example 1

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

Explanation:

1 → 2 → 3 → 4

Length = 4

Example 2

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

Explanation:

0 → 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8

Length = 9

Example 3

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

Explanation:

0 → 1 → 2

Length = 3


Why Is Longest Consecutive Sequence in Python Asked in Interviews?

Interviewers love the Longest Consecutive Sequence in Python problem because it tests several important concepts:

  • Data Structures
  • Hash Sets
  • Time Complexity Optimization
  • Problem Solving
  • Edge Case Handling

Many candidates immediately think about sorting the array, but doing so results in:

O(n log n)

The challenge explicitly requires:

O(n)

which forces you to think beyond conventional approaches.


Longest Consecutive Sequence in Python: Brute Force Approach

A straightforward solution is:

  1. Take every number.
  2. Check if the next number exists.
  3. Continue until the sequence breaks.
  4. Track the longest sequence.

Pseudo-code:

for num in nums:
    current = num
    count = 1

    while current + 1 exists:
        current += 1
        count += 1

Complexity Analysis

Time Complexity: O(n²)
Space Complexity: O(1)

This solution becomes inefficient for large datasets.


Longest Consecutive Sequence in Python Using a Hash Set

The key insight for solving Longest Consecutive Sequence in Python efficiently is using a Hash Set.

Hash Sets provide:

O(1)

average lookup time.

Instead of repeatedly searching through the array, we can instantly determine whether a number exists.

Important Observation

Consider:

[100,4,200,1,3,2]

The sequence starts at:

1

because:

0 not in set

However:

2

is not a sequence starter because:

1 exists

A number is the beginning of a sequence only when:

num - 1 not in num_set

This observation eliminates redundant work and enables an O(n) solution.


Algorithm for Longest Consecutive Sequence in Python

Step 1

Convert the array into a Hash Set.

num_set = set(nums)

Step 2

Iterate through each number.

Step 3

Check whether it starts a sequence.

if num - 1 not in num_set:

Step 4

Expand the sequence.

while current + 1 in num_set:

Step 5

Track the maximum sequence length.


Longest Consecutive Sequence in Python: Optimal Solution

class Solution:
    def longestConsecutive(self, nums):
        num_set = set(nums)
        longest = 0

        for num in num_set:

            if num - 1 not in num_set:

                current = num
                length = 1

                while current + 1 in num_set:
                    current += 1
                    length += 1

                longest = max(longest, length)

        return longest


Longest Consecutive Sequence in Python: Dry Run Example

Input:

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

Hash Set:

{100,4,200,1,3,2}

Processing 100

99 not in set

Sequence length:

1

Processing 4

3 exists

Skip.

Processing 200

199 not in set

Sequence length:

1

Processing 1

0 not in set

Start sequence:

1 → 2 → 3 → 4

Length:

4

Update:

longest = 4

Final Answer:

4

Longest Consecutive Sequence in Python Complexity Analysis

Many developers initially assume the solution is O(n²) because of the nested loop.

However, every number is visited only once.

Consider:

1 → 2 → 3 → 4

After processing this sequence, these numbers are never revisited as sequence starters.

Therefore:

Time Complexity = O(n)

Space Complexity

The Hash Set stores all elements:

Space Complexity = O(n)

This satisfies the problem requirement perfectly.


Common Mistakes in Longest Consecutive Sequence in Python

Mistake 1: Sorting the Array

nums.sort()

Complexity becomes:

O(n log n)

which violates the requirement.


Mistake 2: Starting from Every Number

Always check:

num - 1 not in num_set

before starting a sequence.


Mistake 3: Ignoring Duplicates

Input:

[1,0,1,2]

Converting to a set automatically removes duplicates:

{0,1,2}

Real-World Applications

The pattern used in Longest Consecutive Sequence in Python appears in:

  • User login streaks
  • Fitness tracking applications
  • Attendance monitoring systems
  • Event stream analytics
  • Fraud detection systems
  • Customer engagement analysis
  • Gaming achievement systems

Understanding this pattern helps solve many practical business problems.


Additional Resources

Python Tutorials:

https://geekycodes.in/category/python

Data Structures and Algorithms:

https://geekycodes.in/category/data-structures-and-algorithms


Python Sets Documentation:

https://docs.python.org/3/tutorial/datastructures.html#sets

LeetCode Problem:

https://leetcode.com/problems/longest-consecutive-sequence

Python Time Complexity Guide:

https://wiki.python.org/moin/TimeComplexity


Final Thoughts on Longest Consecutive Sequence in Python

The Longest Consecutive Sequence in Python problem is a fantastic example of how choosing the right data structure can dramatically improve performance.

While sorting may seem like the obvious solution, it cannot satisfy the O(n) requirement. By leveraging a Hash Set and identifying true sequence starting points, we achieve an elegant and optimal solution.

If you’re preparing for coding interviews, mastering the Longest Consecutive Sequence in Python approach will strengthen your understanding of Hash Sets, complexity analysis, and optimization techniques.

Have you encountered another clever solution for the Longest Consecutive Sequence in Python problem? Share your thoughts and approaches in the comments below.

For more Python, Data Structures, Algorithms, System Design, Machine Learning, and AI tutorials, keep following Geeky Codes.

Leave a Reply

Discover more from Geeky Codes

Subscribe now to keep reading and get access to the full archive.

Continue reading