Longest Consecutive Sequence (Set)

Data Structures
Medium
Uber
92.5K views

Given an unsorted array, find the length of the longest consecutive elements sequence. The algorithm should run in $O(n)$ time. Use a Hash Set to store elements and optimize lookups.

Why Interviewers Ask This

Interviewers at Uber ask this to evaluate your ability to optimize for time complexity beyond standard sorting. They specifically test if you can recognize that O(n log n) solutions are insufficient and identify the pattern where a Hash Set allows O(1) lookups. This assesses your depth in data structures, your capacity to handle unsorted inputs efficiently, and your skill in avoiding redundant processing during sequence traversal.

How to Answer This Question

1. Clarify requirements: Confirm input constraints like negative numbers or duplicates immediately. 2. Propose the brute force approach first: Briefly mention sorting (O(n log n)) to show baseline understanding, then pivot to why it fails the O(n) requirement. 3. Design the optimal solution: Explain using a Hash Set for O(1) average time lookups. Detail the logic of only starting a sequence count when the current number is the start of a sequence (i.e., num - 1 does not exist). 4. Walk through an example: Trace '100, 4, 200, 1, 3, 2' step-by-step on a whiteboard, showing how the algorithm skips non-starting elements. 5. Analyze complexity: Explicitly state that while nested loops look like O(n^2), each element is visited at most twice, proving O(n) time and O(n) space. 6. Discuss edge cases: Mention empty arrays or single elements.

Key Points to Cover

  • Explicitly identifying that sorting leads to O(n log n) and rejecting it in favor of O(n)
  • Explaining the logic of skipping non-sequence-starts to ensure linear time complexity
  • Demonstrating that each element is accessed a constant number of times despite nested loops
  • Correctly handling edge cases like duplicates or empty arrays without breaking the logic
  • Articulating the space-time trade-off clearly between the Hash Set storage and lookup speed

Sample Answer

To solve the Longest Consecutive Sequence problem within O(n) time, we must avoid sorting, which would take O(n log n). Instead, I recommend using a Hash Set to store all array elements. The core insight is that we only want to start counting a sequence from its smallest number. For any number in the set, if the previous number (num - 1) exists, then this number cannot be the start of a new sequence, so we skip it. If num - 1 is missing, we increment a counter by checking if num + 1, num + 2, etc., exist in the set until the sequence breaks. We track the maximum length found across all such sequences. For example, with input [100, 4, 200, 1, 3, 2], the set contains all these values. When processing 100, 99 is missing, but 101 is not, so the length is 1. When processing 1, 0 is missing, but 2, 3, and 4 are present, giving a sequence length of 4. Crucially, even though we have nested loops, every element is part of exactly one sequence check. Since we only initiate checks from sequence starts, no element is processed more than twice—once when added to the set and once during the inner loop expansion. This ensures linear time complexity. At Uber, where scalability is critical, demonstrating this optimization shows we prioritize efficient algorithms for large-scale data processing rather than settling for acceptable but suboptimal solutions.

Common Mistakes to Avoid

  • Attempting to sort the array first, which results in O(n log n) time complexity and fails the constraint
  • Starting a sequence count for every number in the array, leading to redundant work and O(n^2) behavior
  • Failing to check if the current number is the start of a sequence before entering the inner loop
  • Ignoring duplicate handling in the Hash Set, causing incorrect sequence length calculations

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 57 Uber questions