‹ DS&A interview · Socratic
DSA · Hashing (O(n)) · #30

Longest Consecutive Sequence

Module 33 · difficulty 3/5·30:00starts on first keystroke

Given an unsorted integer array `nums`, return the length of the longest run of consecutive integers (e.g. 1,2,3,4). The elements need not be adjacent in the array. You must write an O(n) algorithm.

Examples
  • nums = [100,4,200,1,3,2] 4the run 1,2,3,4
  • nums = [0,3,7,2,5,8,4,6,0,1] 9
  • nums = [] 0

Stuck? Reveal an animated walkthrough of the approach.

Constraints
  • · 0 <= nums.length <= 10^5
  • · -10^9 <= nums[i] <= 10^9
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.