Let me formalize the overlap check you just used. Two intervals and overlap if and only if:
Think about it: they do NOT overlap if one ends before the other starts. That means or . The negation gives the overlap condition.
For closed intervals and , use and instead. The boundary matters for touching intervals. Common bug: mixing half-open and closed interval semantics.