Stack Bash - Master data structures and algorithms

Merge intervals

Medium
Let's say you manage office space, and you want to report the days that it was occupied.
You're given a list of days that workers occupied the space as a list of intervals:
[[0, 3], [2, 5], [9, 10], [4, 6]]
To make the report easy to read, write a function that merges these intervals into the smallest number of disjointed interval.
The above example input should be merged into the following intervals:
[[0, 6], [9, 10]]
Note that the numbers are inclusive. I.e. the intervals [5, 9] and [9, 10] should be merged to [5, 10].
But the intervals [4, 6] and [7, 9] should not be merged as they're considered disjointed.

Try it first

Solution

6 Essential Sorting & Searching Coding Interview Problems

Master Sorting & Searching by trying the coding challenges below.
  1. 1.Implement binary searchEasy
  2. 2.Merge intervalsMedium
  3. 3.Kth largest in arrayMedium
  4. 4.Intersection of ArraysMedium
  5. 5.Search sorted matrixHard
  6. 6.Search row & col sorted matrixHard