Longest Consecutive Sequence (Set)

Data Structures
Medium
Uber
92.7K 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…

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

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 166 Data Structures questionsBrowse all 57 Uber questions