Longest Consecutive Sequence
Given an unsorted array of integers Find the length of the longest consecutive elements sequence Must run in O(n) time Example: [100,4,200,1,3,2] => 4 (sequence is [1,2,3,4])
I put the numbers in a set and count only from a number with no predecessor. Each run is walked once.
Examples
You write
longest_consecutive(nums: List[int])
Example 1
- Input
- [100,4,200,1,3,2]
- returns
- 4
Example 2
- Input
- [0,3,7,2,5,8,4,6,0,1]
- returns
- 9
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problemsGraphs
I reach for it when
- The input is edges, prerequisites or connections.
- You need cycles, an ordering or connected groups.
Say this before you type. Say whether the edges are directed, and which of the three jobs you are doing.
All 32 Graphs problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
Each locked part opens in the editor once the part before it passes.
Prep guides that list this problem
Our company tags come from public interview write-ups. None of these is a confirmed question.