My Calendar II is an advanced calendar booking system that prevents triple bookings. You need to implement a smart calendar that can track overlapping events and reject any booking that would create a situation where three events overlap at the same time.

Think of it like a conference room booking system - while double bookings might be manageable (perhaps one meeting can use half the room), triple bookings are absolutely not allowed as they would create chaos!

Each event is represented as a half-open interval [startTime, endTime), meaning it includes the start time but excludes the end time. Your calendar should:
• Accept bookings that don't create triple overlaps
• Reject bookings that would cause three events to share common time
• Return true for successful bookings, false for rejected ones

Example: If you have events [10,20) and [15,25), you can still book [17,22) because only two overlap. But booking another event like [18,23) would be rejected since all four time periods [18,20), [18,22), [18,23), and [18,25) would have three events overlapping.

Input & Output

example_1.py — Basic Usage
$ Input: myCalendarTwo = MyCalendarTwo() myCalendarTwo.book(10, 20) // return True myCalendarTwo.book(50, 60) // return True myCalendarTwo.book(10, 40) // return True myCalendarTwo.book(5, 15) // return False myCalendarTwo.book(5, 10) // return True myCalendarTwo.book(25, 55) // return True
Output: [null, true, true, true, false, true, true]
💡 Note: First three bookings succeed. The fourth booking [5,15) would create a triple overlap with [10,20) and [10,40) in the time period [10,15), so it's rejected. The fifth booking [5,10) is accepted since it doesn't create a triple overlap. The sixth booking [25,55) is also accepted.
example_2.py — Edge Cases
$ Input: myCalendarTwo = MyCalendarTwo() myCalendarTwo.book(10, 20) // return True myCalendarTwo.book(15, 25) // return True myCalendarTwo.book(20, 30) // return True myCalendarTwo.book(15, 20) // return False
Output: [null, true, true, true, false]
💡 Note: The first three bookings create overlaps [15,20) and [20,25). When we try to book [15,20), it would overlap completely with the existing double-booked interval [15,20), creating a triple booking, so it's rejected.
example_3.py — No Conflicts
$ Input: myCalendarTwo = MyCalendarTwo() myCalendarTwo.book(10, 15) // return True myCalendarTwo.book(20, 25) // return True myCalendarTwo.book(30, 35) // return True myCalendarTwo.book(40, 45) // return True
Output: [null, true, true, true, true]
💡 Note: All bookings are accepted because none of them overlap with each other, so there are no double bookings and certainly no triple bookings.

Constraints

  • 0 ≤ start < end ≤ 109
  • At most 1000 calls will be made to book
  • All time values are integers
  • Intervals are half-open: [start, end)

Visualization

Tap to expand
🏢 Smart Calendar SystemTimeline →Meeting AMeeting BOverlap ZoneNew RequestDecisionEngine✓ APPROVED: No triple overlap detectedSystem maintains two lists: All Bookings + Danger ZonesKey Insight: Triple bookings only occur when overlapping with existing double-booked intervals
Understanding the Visualization
1
Check Danger Zone
First check if the new booking would overlap with any currently double-booked time slots (the danger zone)
2
Scan for New Conflicts
If safe, scan through all existing bookings to identify any new overlaps the new booking would create
3
Update Tracking Lists
Add any newly discovered overlaps to the double-booked intervals list for future conflict checking
4
Accept Booking
Add the new booking to the accepted bookings list and return success
Key Takeaway
🎯 Key Insight: By separating all bookings from double-booked intervals, we can efficiently detect potential triple bookings in O(n) time per operation, making the calendar system scalable and responsive.
Asked in
Google 42 Microsoft 28 Amazon 35 Apple 15
58.0K Views
Medium-High Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen