Workspace Longest Consecutive Sequence
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Longest Consecutive Sequence visualizer

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.

1 / 4Execution step
Reference example · JavaScript
Inspect reference input
[
  100,
  4,
  200,
  1,
  3,
  2
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
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
};

Execution canvas

Active Changed
STEP 1

Expand sequence from 100

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Array6 items
Run1 items
Best1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Arrays · Hashing and prefix sums

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

  1. Expand sequence from 100
  2. Expand sequence from 200
  3. 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
};

Continue learning

Practice related DSA questions in the same topic.