Medium: Interview Core · Problem 62 of 82

Meetings Overlap

MediumProblem SolvingPython

Each meeting is a pair of strings (start, end) such as ("09:00", "10:30"), in 24-hour HH:MM format with leading zeros. Every meeting ends after it starts.

Write meetings_overlap(a, b) that returns True if the two meetings share some time, and False otherwise. A meeting that ends at the exact minute another one starts does not clash with it.

Understand the problem

Get the boundary rule right first.

Do ("09:00", "10:00") and ("10:00", "11:00") overlap?

  1. Yes
  2. No

The first ends at the exact minute the second starts. The task says that is not a clash. They touch but share no time. This single rule decides whether you write < or <= later.

How many minutes do ("09:30", "10:30") and ("10:00", "11:00") share? Type a number.

They share 10:00 to 10:30, which is 30 minutes. Any positive amount means they overlap.

Try examples by hand

Look for the cases a quick check would miss.

A is ("10:00", "11:00") and B is ("09:00", "12:00"). Does B start inside A?

  1. Yes
  2. No

B starts at 09:00, before A even begins, so B does not start inside A. But the meetings clearly overlap: A is entirely inside B. A check that only asks "does one start inside the other?" in a single direction misses this case.

Which statement lists the cases where two meetings do not overlap?

  1. One ends before the other starts, in either order, or they just touch
  2. One starts before the other starts
  3. Their lengths are different

It is often easier to describe when two things do not overlap, because there are only two ways: A is completely before B, or B is completely before A. Overlap is "neither of those".

Write the solution

Times in HH:MM with leading zeros compare correctly as plain strings, so no parsing is needed.

def meetings_overlap(a, b):
    # Write your code here.
    pass
Hint 1: a question to ask yourself

Is it easier to describe when the meetings do not overlap?

Hint 2: the shape of the answer

They overlap when A starts before B ends and B starts before A ends. Think about whether each comparison is strict or not.

Reflect

Solution

def meetings_overlap(a, b):
    return a[0] < b[1] and b[0] < a[1]

Another way: describe the clash by its opposite

def meetings_overlap(a, b):
    a_before_b = a[1] <= b[0]
    b_before_a = b[1] <= a[0]
    return not (a_before_b or b_before_a)

Pattern: flip the question. "Do they overlap?" has many shapes (partial, contained, identical). "Are they apart?" has exactly two. Writing the easy opposite and negating it is much safer than listing the overlap cases one by one.

Boundary rule: touching is not overlapping, so the comparisons are strict (<). Change the task to "touching counts" and every < becomes <=. Tests on both sides of that boundary catch a wrong choice.

Cost: two comparisons, so O(1).

What an interviewer might ask next: You have a list of n meetings. How would you find whether any two of them clash without comparing every pair? (Hint: sort by start time.)