Earlier start goes first
Swap the two intervals if needed so that the first starts no later than the second. Now overlap has a single test: does the second begin before the first is over? It is like two shifts, where the one that starts later just has to begin while the earlier one is still on.
This is the same check you wrote before, made simpler by the order.
Order first: if the first starts later, swap them.
Starts while the other runs: the second begins before the first ends, so they overlap.
a = [5, 8], b = [1, 6]
b starts earlier, so swap: call [1, 6] the first one.
Move 1 of 3
Joining takes the later end
The merged range starts at the first one's start and ends at the later of the two ends. Using the second end alone is the classic slip: with [1, 10] and [2, 3], the merged range is [1, 10], not [1, 3].
A short meeting inside a long one doesn't shorten the long one.
Earliest start, latest end: one interval that covers both.
Inside case: a later start can still have an earlier end.
if a[0] > b[0]:
a, b = b, a
if b[0] <= a[1]:
return [[a[0], max(a[1], b[1])]]
return [a, b]