Pre-Calculus for Programmers

When are we both free?

When are we both free?

Two people each give you the window they are free in, as minutes counted from midnight. A window is a pair [start, end] and it includes both ends. You want the window in which both of them are free, which is the intersection of the two intervals.

Return that shared window as a pair [start, end], or an empty list if there is no moment they are both free.

Worked example

Input: a = [540, 720], b = [660, 900]

The shared start is the later of the two starts: max(540, 660) = 660
The shared end   is the earlier of the two ends:  min(720, 900) = 720
660 is not after 720, so the window is real.

Output: [660, 720]
One person free 09:00 to 12:00, the other 11:00 to 15:00.

The awkward ends

Input: a = [0, 100],   b = [200, 300]   Output: []
Input: a = [100, 200], b = [120, 150]   Output: [120, 150]
Input: a = [100, 200], b = [200, 300]   Output: [200, 200]
No overlap at all, one window inside the other, and windows that touch.

Constraints

  • a and b each hold exactly two values, with the start no later than the end.
  • Values run from −109-10^{9} to 10910^{9}, so they need not be minutes of a real day, and they may be negative.
  • Windows that touch at a single value do overlap, at that one value, and the answer is a window of zero width.
  • Return an empty list, not a pair, when there is no overlap.
Code

Run your code to see how it does on the examples. Submit also runs the hidden cases.