Insert an interval into sorted busy blocks, merging overlaps
A calendar app stores each person’s day as a list of busy blocks. When someone accepts a new meeting, the app adds the meeting to the list and keeps it tidy: blocks that overlap or touch become one block, so the “free time” view never shows a zero-minute gap.
Implement insertBusyBlock(blocks, block). It returns a new busy list that contains block.
insertBusyBlock(blocks, block) // => Array<[start, end]>
[start, end] of minutes since midnight, with start <= end.blocks is already tidy: sorted by start, and each block ends strictly before the next one starts. It may be empty.block is the new meeting. If block[1] < block[0], throw a RangeError.blocks or block.[60, 90] and [90, 120] become [60, 120].[75, 75] are allowed and follow the same rules.blocks or any of its pairs.insertBusyBlock([[60, 90], [180, 240]], [120, 150]);
// => [[60, 90], [120, 150], [180, 240]]
insertBusyBlock([[60, 90], [180, 240]], [80, 120]);
// => [[60, 120], [180, 240]]
insertBusyBlock([[0, 30], [60, 90], [120, 150], [300, 330]], [20, 140]);
// => [[0, 150], [300, 330]]
insertBusyBlock([[60, 90], [120, 150]], [90, 120]);
// => [[60, 150]] (touches both neighbors)
insertBusyBlock([], [45, 60]);
// => [[45, 60]]
insertBusyBlock([[60, 90]], [100, 50]);
// throws RangeError