DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Array

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. **Hint / Expected Approach:** Hash set: only start from sequence head **Edge Cases to Consider:** - (1) All duplicates - (2) Single element - (3) Already consecutive

Examples

Example 1
Input: [100,4,200,1,3,2]
Output: 4
Example 2
Input: [0,3,7,2,5,8,4,6,0,1]
Output: 9
Example 3
Input: []
Output: 0

Constraints

  • ▪1 <= nums.length <= 10^4
  • ▪-10^9 <= nums[i] <= 10^9
  • ▪Time complexity should be O(n) or O(n log n)

Watch Out For Edge Cases

  • ▪All duplicates
  • ▪Single element
  • ▪Already consecutive
Frequently Asked At
GoogleAmazonMetaAppleFlipkartZomato