As reported: JPMorgan Chase SDE-3 loop (2026), round 1 (DSA, no hints): LeetCode 128. The round covered explaining the approach, optimizing it, time and space complexity, and edge cases.
Given an unsorted array of integers nums, return the length of the longest sequence of consecutive values (x, x+1, x+2, …) that can be formed from its elements. The elements do not need to be adjacent in the array. The algorithm must run in O(n) time.
This is LeetCode 128 — Longest Consecutive Sequence.
Example 1
nums = [100,4,200,1,3,2] -> 4 the run 1, 2, 3, 4
Example 2
nums = [0,3,7,2,5,8,4,6,0,1] -> 9 the run 0 … 8
Clarifications
- Duplicates count once:
[1,2,2,3]has a run of length 3. - An empty array gives
0.
Constraints: 0 ≤ nums.length ≤ 10^5; -10^9 ≤ nums[i] ≤ 10^9.
Code runner format: one line holding nums as a JSON array (LeetCode's test-case format). Implement longestConsecutive(nums); the harness prints the length.