How do you determine whether two people can attend the same meeting, and what is the edge case that catches people out?
You compare the start of one meeting with the end of the other. Two meetings overlap when each one starts before the other one ends.
def overlaps(a, b):
start_a, end_a = a
start_b, end_b = b
return start_a < end_b and start_b < end_a
So how do you think about it? A meeting is a stretch of time from its start to its end. Two stretches of time overlap when they share at least one moment. That happens when the first one starts before the second one ends and the second one starts before the first one ends.
And the edge case that catches people out is when one meeting starts exactly when the other one ends. So one meeting is from ten to eleven and the other is from eleven to twelve. Do they overlap?
And the answer depends on the rule that the company uses. In most calendars a meeting that starts at the hour when the other one ends does not overlap, because the first one has already finished. So the check uses < and not <=.
the meetings overlap? why
------------------------ ---------- -------------------------
[10, 11] and [10, 11] yes the same time
[10, 11] and [10:30, 12] yes the second starts inside
[10, 11] and [9, 10:30] yes the first starts inside
[10, 11] and [9, 10] yes they share the minute at 10
[10, 11] and [11, 12] no the first ends at 11
And there is a second thing that catches people out. An empty list of meetings means that the person is free, and a single meeting always means that the person is busy. So the number of people who can attend is the number of meetings that do not overlap with anything.
And a third edge case. Some people write the check as start_a <= end_b and start_b <= end_a, which treats a meeting that starts exactly when another ends as an overlap. Both versions appear in real code, so the right one depends on the rule that the business uses. What matters is that you say which rule you chose.
So the rule to remember is this. Two intervals overlap when each start is before the other end, and you have to decide what happens when the times are exactly the same.
The last question in this topic shows the check written out.