KatInterview

Find the Meeting Slot That Conflicts With the Most Others

SQL & DataModel answer
MediumModel answerSQL & Datasqlinterval-overlapself-join

Last updated

Given a table of time-bounded sessions, write SQL to find which session overlaps the largest number of other sessions and report that count.

Setup

You have a bookings table that logs scheduled sessions, each with a unique ID and a start and end timestamp.

bookings

| booking_id | start_time          | end_time            |
|------------|---------------------|---------------------|
| 1          | 2023-01-01 09:00:00 | 2023-01-01 10:00:00 |
| 2          | 2023-01-01 09:30:00 | 2023-01-01 11:00:00 |
| 3          | 2023-01-01 10:30:00 | 2023-01-01 12:00:00 |
| 4          | 2023-01-01 13:00:00 | 2023-01-01 14:00:00 |

Task

Write a SQL query that returns the booking_id that conflicts (overlaps in time) with the greatest number of other bookings, along with that conflict count.

Two bookings conflict if their time ranges overlap at all. Treat intervals as half-open — if one booking ends exactly when another begins, they do not conflict.

If multiple bookings share the highest conflict count, return all of them.

Expected Output

| booking_id | overlap_count |
|------------|---------------|
| 2          | 2             |

What the interviewer expects

  • A correct two-interval overlap predicate, with a brief justification
  • Proper self-exclusion so a booking isn't counted as conflicting with itself
  • Handling of bookings with zero conflicts
  • A comment on ties and how your query handles them
  • (Bonus) A note on how the approach scales and what you'd do for a very large table

Hints

Hint 1

Two intervals do NOT overlap when one ends before or exactly when the other starts. Negate that condition to get the overlap predicate: start_a < end_b AND end_a > start_b.

Hint 2

Self-join the table so every booking is compared against every other booking, but add a filter to exclude a booking being paired with itself — otherwise every count is inflated by one.

Hint 3

Use a LEFT JOIN (not INNER JOIN) so bookings with zero conflicts still appear in the output with a count of 0. Pair this with COUNT(b.booking_id) rather than COUNT(*), or the NULL-padded row from the left join gets counted as 1.