Editor

Overlapping Free Times

#123 · Two pointers · Advanced · 115 XP · about 20 min

Two friends each list the times they are free as intervals [start, end]. Each list is sorted by start time, and within one list every interval ends before the next one starts. Write free_together(a, b) that returns the intervals when both friends are free, as a list of [start, end] pairs in time order.

Intervals include both ends, so [3, 5] and [5, 8] share the moment 5, which counts as the interval [5, 5].

Examples

free_together([[1, 4], [6, 9]], [[3, 7], [8, 12]])   # returns [[3, 4], [6, 7], [8, 9]]
free_together([[3, 5]], [[5, 8]])                    # returns [[5, 5]]
free_together([[1, 2]], [[3, 4]])                    # returns []
free_together([[0, 10]], [[2, 3], [5, 6]])           # returns [[2, 3], [5, 6]]

In the first example, [6, 9] overlaps two of the other friend's intervals, so it adds two pieces: [6, 7] and [8, 9].

Constraints

  • 0 <= len(a), len(b) <= 10,000
  • 0 <= start <= end <= 10^9

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.