Intervals Arrive One at a Time

You know the merge algorithm in code. Now run it with your hands.

Sorted intervals arrive on a number line one by one. For each arrival, you have one decision: does this interval overlap the last committed bar, or does it start a new group?

No code editor. No multiple choice. Just a number line and your spatial intuition.

Merge or New?

Four sorted intervals from the classic LC56 example. Process each arrival — tap Merge or New.

Before you start

Process 4 sorted intervals. For each arrival, decide: does it overlap the last committed interval, or start a new group?

012345678910111213141516171819

If you run the merge-intervals pass on this list, how many distinct groups will remain at the end?

Sort, Then Forge

Now the intervals arrive unsorted. Sort them by tapping in the right order, then merge overlapping bars by dragging them together.

Sort by start time

Which field do we sort intervals by before merging?

Containment: Does Math.max Hold?

[1,10] fully contains [2,3] and [4,8]. When you drag the contained interval toward the committed bar, watch what happens to the end boundary.

Math.max(10, 3) = 10 — the interval does not shrink.

Forge the merge

A new interval has arrived. What should happen with it?

[1,10]
[2,3]
0
2
4
6
8
10
12
14
16

or tap the dashed bar to merge it into the last group