Blind 75
8 / 75Free practice

Longest Consecutive Sequence

Medium · Arrays & hashing

Jump to code

Find the length of the longest run of consecutive integer values present in an unsorted array. Values in the run need not occupy adjacent positions.

Examples

Example 1

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

Output: 4

Example 2

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

Output: 9

Constraints

  • 0 <= nums.length <= 1000
  • -10^9 <= nums[i] <= 10^9
Need a hint?

Start counting only at the smallest value of a run.

Asked in real interviews

Candidates reported versions of Longest Consecutive Sequence from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 10 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  [
    100,
    4,
    200,
    1,
    3,
    2
  ]
]
Expected
4
Case 2
Input
[
  [
    0,
    3,
    7,
    2,
    5,
    8,
    4,
    6,
    0,
    1
  ]
]
Expected
9
Case 3
Input
[
  []
]
Expected
0
Case 4
Input
[
  [
    5
  ]
]
Expected
1
Case 5
Input
[
  [
    1,
    2,
    2,
    3
  ]
]
Expected
3

Drafts are saved per language in this browser when storage is available.