Longest Consecutive Sequence

easy
Unit 6 · Matrices0/3 to milestone

Given an unsorted list of integers, find the length of the longest run of consecutive values ([100, 4, 200, 1, 3, 2] → [1, 2, 3, 4] → 4).

Implement longest_consecutive(nums) in O(n) using a set, without sorting. Then implement longest_consecutive_run(nums), which returns the run itself as a sorted list (the one with the smallest start if several tie, [] for empty input).

#hashing#arrays2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Longest Consecutive Sequence · TechGradify