Workspace Merge Intervals
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Merge Intervals visualizer

Learn Merge Intervals with a step-by-step Arrays visualizer, JavaScript source code and a worked example. Practice optimization and matrix.

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 / 5Execution step
Reference example · JavaScript
Inspect reference input
[
  [
    1,
    3
  ],
  [
    2,
    6
  ],
  [
    8,
    10
  ],
  [
    15,
    18
  ]
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = (a,s)=>{
a.sort((x,y)=>x[0]-y[0]);
let o=[];
for(let x of a){
let l=o.at(-1);
if(l&&x[0]<=l[1])l[1]=Math.max(l[1],x[1]);
else o.push([...x]);
s("Merge or append",{
Input:a,Output:o
})
}
return o
};

Execution canvas

Active Changed
STEP 1

Merge or append

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

Input4 rows
Output1 rows
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Arrays · Optimization and matrix

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

[
  [
    1,
    3
  ],
  [
    2,
    6
  ],
  [
    8,
    10
  ],
  [
    15,
    18
  ]
]

Execution steps

  1. Merge or append

Computed output

[
  [
    1,
    6
  ],
  [
    8,
    10
  ],
  [
    15,
    18
  ]
]
JavaScript reference implementation
const solve = (a,s)=>{
  a.sort((x,y)=>x[0]-y[0]);
  let o=[];
  for(let x of a){
    let l=o.at(-1);
    if(l&&x[0]<=l[1])l[1]=Math.max(l[1],x[1]);
    else o.push([...x]);
    s("Merge or append",{
      Input:a,Output:o
    })
  }
  return o
};

Continue learning

Practice related DSA questions in the same topic.