A room-booking widget keeps the room's busy blocks as a list of [start, end] intervals that is sorted by start and has no overlapping or touching blocks. When someone books a new slot, the list must be updated.
Write insertInterval(blocks, slot), which returns a new list with slot added, still sorted, with any blocks that overlap or touch the slot merged into it.
insertInterval([[0, 30], [60, 90], [120, 150], [200, 210]], [80, 130]);
// [[0, 30], [60, 150], [200, 210]]
insertInterval([[0, 30], [60, 90]], [40, 50]);
// [[0, 30], [40, 50], [60, 90]]
insertInterval([[0, 30]], [30, 45]);
// [[0, 45]] (touching blocks merge)
insertInterval([], [5, 10]);
// [[5, 10]]
blocks, its inner arrays, or slot.Spotted a mistake or a rights concern? Report an issue