Learn Longest Consecutive Sequence with a step-by-step Arrays visualizer, JavaScript source code and a worked example. Practice hashing and prefix sums.
INTERACTIVE EXECUTION STUDIO
LEARN BY DOING
Predict. Execute. Understand.
Follow the source code, inspect live state changes, and predict the next operation before revealing it.
Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.
Example input
[
100,
4,
200,
1,
3,
2
]
Execution steps
Expand sequence from 100
Expand sequence from 200
Expand sequence from 1
Computed output
4
JavaScript reference implementation
const solve = (a,s)=>{
let set=new Set(a),b=0;
for(let x of set)if(!set.has(x-1)){
let y=x;
while(set.has(y))y++;
b=Math.max(b,y-x);
s("Expand sequence from "+x,{
Array:a,Run:Array.from({
length:y-x
},(_,i)=>x+i),Best:[b]
})
}
return b
};