Skip to main content

75. Longest Consecutive Sequence

mediumAsked at Vercel

Find the length of the longest consecutive elements sequence in an unsorted array, in O(n). Vercel asks this for the hash-set 'only start from the run boundary' insight — same trick they use to find longest contiguous deploy intervals.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)Vercel platform onsite; O(n) version expected.
  • Blind (2026-Q1)Listed in Vercel routing engineer screen.

Problem

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.

Constraints

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

Examples

Example 1

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

Explanation: The longest consecutive elements sequence is [1, 2, 3, 4].

Example 2

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

Approaches

1. Sort then scan

Sort; walk and count consecutive runs.

Time
O(n log n)
Space
O(1) or O(n) depending on sort
function longestConsecutive(nums) {
  if (!nums.length) return 0;
  nums.sort((a, b) => a - b);
  let best = 1, cur = 1;
  for (let i = 1; i < nums.length; i++) {
    if (nums[i] === nums[i - 1]) continue;
    if (nums[i] === nums[i - 1] + 1) cur++;
    else cur = 1;
    best = Math.max(best, cur);
  }
  return best;
}

Tradeoff: O(n log n). Vercel wants O(n).

2. Hash set with sequence-start filter (optimal)

Build a Set. For each num, check if it's a sequence START (num - 1 not in set). If so, count forward.

Time
O(n)
Space
O(n)
function longestConsecutive(nums) {
  const set = new Set(nums);
  let best = 0;
  for (const n of set) {
    if (set.has(n - 1)) continue; // not a sequence start
    let len = 1;
    while (set.has(n + len)) len++;
    if (len > best) best = len;
  }
  return best;
}

Tradeoff: O(n) total. Each element is visited at most twice (once in the outer loop, once in the inner while). The 'is sequence start' check is the trick that prevents O(n^2).

Vercel-specific tips

Vercel grades the start-filter trick. Bonus signal: arguing why this is O(n) — every element is the start of at most one sequence, and only sequence starts trigger the inner loop. They may follow up with 'what if you must report the sequence itself?' — track start and length.

Common mistakes

  • Looping the inner while without the start-filter check — O(n^2).
  • Iterating nums (array) instead of set — duplicate work on duplicates.
  • Forgetting the empty-array check — `best` returns 0 correctly but verify.

Follow-up questions

An interviewer at Vercel may pivot to one of these next:

  • Find the actual longest sequence, not just length.
  • Binary Tree Longest Consecutive Sequence (LC 298).
  • Longest Consecutive Path in Matrix (LC 329) — DFS with memo.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why O(n)?

The inner while runs only for sequence starts. Each element is in exactly one sequence, so the total work across all inner whiles is O(n). The outer loop is O(n). Combined: O(n).

Why iterate the set, not the array?

Duplicates in the input would trigger redundant outer-loop iterations. Iterating the set processes each unique value once.

Practice these live with InterviewChamp.AI

Drill Longest Consecutive Sequence and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →