Skip to main content

Guidelines

The best thing is to draw the intervals and see how the endpoints (start, end) behave. I also used two fingers (thumb - start of the interval and index finger - end of interval) on both hands to see how the intervals can intersect. Almost every problem requires you to sort the intervals by one endpoint if they're not already sorted. If the intervals are sorted by start time for overlap/intersection is enough to test if the following stands intervalA[1] >= intervalB[0] here we suppose the intervalB start after intervalA in sorted array of intervals (additional check end >= start can be done to test if the overlap is valid, after we take min from two end points, and max from two start points).

You should always think of how to order the intervals so you can fulfill the requirement easier (for example for overlapping you could sort by end time, giving maximum space for other intervals to intersect with the current one).