Hey everyone, I’m working on a small scheduling tool and trying to merge overlapping time ranges before I render them. It mostly works, but in a few cases I still end up with overlaps, which means I either double-book a slot or over-merge and hide a gap.
function mergeIntervals(list) {
list.sort((a, b) => a.start - b.start);
const merged = [];
for (const curr of list) {
const last = merged[merged.length - 1];
if (!last || curr.start > last.end) {
merged.push({ ...curr });
} else {
last.end = curr.end;
}
}
return merged;
}
What am I missing in this merge logic that causes some ranges to stay wrong after combining?
@Baymax yup, your [1,10], [2,3], [9,12] example shows the failure clearly, and one small edge case is whether touching ranges like [1,2] and [2,3] should merge since curr.start > last.end currently treats them as overlapping.
@sora yeah, that check is the whole difference. I’d test [1,2] + [2,3] and [1,2] + [3,4] side by side and make sure the result matches how your scheduler is supposed to treat touching ranges.
@sarah_connor yep, those two cases will show it fast. I’d also print the current merged end each time through the loop so you can catch the exact step where it stops extending like it should.