Skip to main content
Back to problems
Leetcode
Medium
Arrays
Ordered Structures
Intervals
Google
My Calendar II

Design a calendar that allows double bookings but rejects any booking that would create a triple overlap.

Acceptance 0%
Problem Statement

My Calendar II

Implement a booking system for a calendar.

You will receive a sequence of interval bookings. Each booking is represented by a start time and an end time, where the interval is treated as half-open: [start, end).

A new booking is accepted if it does not cause any time point to be covered by three or more active bookings. If accepting the booking would create a triple booking at any moment, reject it and keep the calendar unchanged.

Return whether each booking request is accepted or rejected.

Notes

  • Intervals are half-open, so a booking ending at time t does not overlap with another booking starting at time t.
  • A time point may be covered by at most two active bookings.
  • The calendar must support multiple booking requests in sequence.

Input Format

  • A stream or list of booking requests.
  • Each request contains two integers: start and end.
  • Process requests in order.

Output Format

  • For each request, return true if the booking is accepted.
  • Return false if the booking would introduce any triple overlap.

Constraints

  • 0 <= start < end
  • Intervals are half-open: [start, end)
  • The number of requests may be large enough that naive re-checking of all points is inefficient.
Examples
Sample cases returned by the problem API.

Example 1

Input

book(10, 20) -> true
book(50, 60) -> true
book(10, 40) -> true
book(5, 15) -> false
book(5, 10) -> true
book(25, 55) -> true

Output

[true, true, true, false, true, true]

Explanation

The booking [5, 15) would overlap with both [10, 20) and [10, 40) on [10, 15), creating a triple booking. The others are accepted.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.