Implement a calendar that books an event only if it causes no double booking

Quick Overview

Implement a calendar class whose book method accepts a half-open time interval and adds it only when it creates no double booking with events already on the calendar. It tests reasoning about interval overlap, choosing a structure for stored bookings, and the cost of each booking call.

Implement a calendar that books an event only if it causes no double booking

Company: Airbnb

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking. A **double booking** happens when two events have some non-empty intersection (some moment is common to both events). The event can be represented as a pair of integers `start` and `end` that represents a booking on the half-open interval `[start, end)`, the range of real numbers `x` such that `start <= x < end`. Implement a `MyCalendar` class with the following methods: - `MyCalendar()`: Initializes an empty calendar. - `book(start: int, end: int) -> bool`: Returns `True` if the event can be added to the calendar successfully without causing a double booking. Otherwise, returns `False` and does not add the event. ```hint Overlap test Write the condition for two half-open intervals to share a moment as comparisons on their endpoints, then check it on two intervals that only touch, such as `[10, 20)` and `[20, 30)`. ``` ```hint Which bookings can collide Accepted bookings never overlap each other. If you keep them sorted by start, ask how many stored bookings a new event has to be compared against to detect a collision. ``` ### Examples #### Example 1 ```python cal = MyCalendar() # initialize calendar cal.book(10, 20) # True (no existing bookings, [10,20) added) cal.book(15, 25) # False ([15,25) overlaps with [10,20)) cal.book(20, 30) # True ([20,30) does not overlap [10,20), added) ``` The third event touches the first one at time 20 without sharing a moment with it, because `[10, 20)` does not contain 20. #### Example 2 ```python cal = MyCalendar() # initialize calendar cal.book(5, 10) # True ([5,10) added) cal.book(15, 20) # True ([15,20) added, no overlap with [5,10)) cal.book(10, 15) # True ([10,15) fits between [5,10) and [15,20)) cal.book(0, 5) # True ([0,5) fits before [5,10)) cal.book(12, 18) # False ([12,18) overlaps with [10,15) and [15,20)) ``` ### Constraints & Assumptions - `0 <= start < end <= 10^9` - At most `1000` calls will be made to `book`. ### What a Strong Answer Covers - The overlap condition for half-open intervals, including events that only touch at an endpoint. - How accepted bookings are stored, and how many of them a new event has to be compared against. - The time and space cost of each `book` call, and whether it fits the 1,000-call limit. - Edge cases such as an empty calendar, equal start times, and a rejected event leaving the calendar unchanged. ### Follow-up Questions - How would you change `book` so that two overlapping events are allowed but a triple booking is rejected? - If every booking were accepted, how would you return, after each one, the largest number of events that overlap at any moment? - How would you support cancelling a booking? - What data structure would you use if `book` were called millions of times?

Overview: Implement a calendar class whose book method accepts a half-open time interval and adds it only when it creates no double booking with events already on the calendar. It tests reasoning about interval overlap, choosing a structure for stored bookings, and the cost of each booking call.

Airbnb logo
Airbnb
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
1
0

You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking.

A double booking happens when two events have some non-empty intersection (some moment is common to both events).

The event can be represented as a pair of integers start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start <= x < end.

Implement a MyCalendar class with the following methods:

  • MyCalendar() : Initializes an empty calendar.
  • book(start: int, end: int) -> bool : Returns True if the event can be added to the calendar successfully without causing a double booking. Otherwise, returns False and does not add the event.

Examples

Example 1

cal = MyCalendar()   # initialize calendar
cal.book(10, 20)     # True   (no existing bookings, [10,20) added)
cal.book(15, 25)     # False  ([15,25) overlaps with [10,20))
cal.book(20, 30)     # True   ([20,30) does not overlap [10,20), added)

The third event touches the first one at time 20 without sharing a moment with it, because [10, 20) does not contain 20.

Example 2

cal = MyCalendar()   # initialize calendar
cal.book(5, 10)      # True   ([5,10) added)
cal.book(15, 20)     # True   ([15,20) added, no overlap with [5,10))
cal.book(10, 15)     # True   ([10,15) fits between [5,10) and [15,20))
cal.book(0, 5)       # True   ([0,5) fits before [5,10))
cal.book(12, 18)     # False  ([12,18) overlaps with [10,15) and [15,20))

Constraints & Assumptions

  • 0 <= start < end <= 10^9
  • At most 1000 calls will be made to book .

What a Strong Answer Covers Guidance

  • The overlap condition for half-open intervals, including events that only touch at an endpoint.
  • How accepted bookings are stored, and how many of them a new event has to be compared against.
  • The time and space cost of each book call, and whether it fits the 1,000-call limit.
  • Edge cases such as an empty calendar, equal start times, and a rejected event leaving the calendar unchanged.

Follow-up Questions Guidance

  • How would you change book so that two overlapping events are allowed but a triple booking is rejected?
  • If every booking were accepted, how would you return, after each one, the largest number of events that overlap at any moment?
  • How would you support cancelling a booking?
  • What data structure would you use if book were called millions of times?