← Questions

My calendar: book without double booking

0
MicrosoftCodingLast asked Nov 2025

A coding interview question asked in Microsoft's technical phone screen, last asked Nov 2025. It tests intervals, binary-search and sorted maps.

Build a calendar that stores events as half-open intervals [start, end). A new event is added only if it does not overlap any event already booked. Two events [s1, e1) and [s2, e2) overlap when max(s1, s2) < min(e1, e2), so events that only touch at an endpoint do not overlap.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • MyCalendar() — creates an empty calendar.
  • book(start: int, end: int) -> bool — adds the event and returns True if it overlaps nothing; otherwise returns False and adds nothing.

Example

book(10, 20)  ->  True
book(15, 25)  ->  False   # overlaps [10, 20)
book(20, 30)  ->  True    # touching at 20 is allowed

book(5, 10)   ->  True    # on a new calendar
book(1, 5)    ->  True
book(10, 15)  ->  True

Constraints

  • 0 <= start < end <= 10^9.
  • Up to 10^5 calls to book.

Clarifying questions

  • Are intervals half-open, so touching endpoints are fine?
  • How many bookings should I plan for?
  • Do I ever need to cancel a booking?

What a strong answer covers

  • The simple version: scan every booked event, O(n) per call and O(n^2) overall.
  • The better version: keep events sorted by start; only the predecessor and successor of the new start can overlap it; find them by binary search in a balanced tree or sorted map for O(log n) per call.
  • Why insertion into a plain sorted array is still O(n), and which structures avoid it.
  • Edge cases: touching intervals, an event inside another, an event covering several others.

Follow-ups

  • Allow double booking but reject triple booking.
  • Return the maximum number of overlapping events after each booking.
  • Add cancel(start, end).

Where it's asked

What it tests

intervals · binary-search · sorted maps

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding