Z12

Longest Consecutive Sequence

Mid / Senior Engineer
Target Time: 25 - 35 Minutes
Type: NUMERIC

Problem Statement & Objective

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.

Function Signature (Python)

longest_consecutive_sequence(nums: list[int]) -> int

Input & Output Contract

Output Rule:Length of the longest consecutive sequence.

System Constraints

0 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9

Example Execution Walkthrough

nums = [100, 4, 200, 1, 3, 2]
longest sequence is [1, 2, 3, 4] -> length 4

Example Test Scenarios (3)

Example 1
Input:[[100,4,200,1,3,2]]
Expected Output:4
Example 2: 0..8
Input:[[0,3,7,2,5,8,4,6,0,1]]
Expected Output:9
Example 3: Boş Dizi
Input:[[]]
Expected Output:0
Loading Python Editor...
Input Parameters
[
  [
    100,
    4,
    200,
    1,
    3,
    2
  ]
]
Expected Output
4