As reported: Walmart SDE-3 round 1, online DSA (2026): longest consecutive subsequence, after Number of Islands II.
Given an unsorted array of integers nums, return the length of the longest sequence of consecutive values (x, x+1, x+2, …) that all appear in the array. The values can be anywhere in the array. Aim for O(n).
This is LeetCode 128 — Longest Consecutive Sequence.
Example
[100,4,200,1,3,2] -> 4 (1, 2, 3, 4)
[0,3,7,2,5,8,4,6,0,1] -> 9
Constraints: 0 ≤ nums.length ≤ 10^5; -10^9 ≤ nums[i] ≤ 10^9.
Code runner format: one line with nums. Implement longestConsecutive(nums); the harness prints the length.