Design a Calendar System: You're building a smart calendar application that prevents double bookings. Your task is to implement a MyCalendar class that can efficiently determine whether a new event conflicts with existing bookings.

Key Concepts:
• Events are represented as half-open intervals [startTime, endTime)
• Two events conflict if they share any moment in time
• The interval [startTime, endTime) includes startTime but excludes endTime

Example: Event [10, 20) conflicts with [15, 25) because they overlap from time 15 to 20.

Your Implementation Must Support:
MyCalendar() - Initialize an empty calendar
book(startTime, endTime) - Return true if the event can be booked without conflicts, false otherwise

Input & Output

example_1.py — Basic Calendar Operations
$ Input: ["MyCalendar", "book", "book", "book"] [[], [10, 20], [15, 25], [20, 30]]
Output: [null, true, false, true]
💡 Note: MyCalendar() initializes empty calendar. book(10, 20) succeeds as calendar is empty. book(15, 25) fails because it overlaps with existing [10, 20). book(20, 30) succeeds because it starts exactly when [10, 20) ends (half-open intervals).
example_2.py — Edge Case with Adjacent Events
$ Input: ["MyCalendar", "book", "book", "book", "book"] [[], [5, 10], [10, 15], [0, 5], [15, 20]]
Output: [null, true, true, true, true]
💡 Note: All bookings succeed because they are perfectly adjacent with no overlaps. [5,10) and [10,15) don't overlap since first ends at 10 and second starts at 10. Same logic applies to other adjacent pairs.
example_3.py — Multiple Overlap Attempts
$ Input: ["MyCalendar", "book", "book", "book", "book", "book"] [[], [1, 5], [2, 6], [3, 4], [1, 3], [5, 10]]
Output: [null, true, false, false, false, true]
💡 Note: book(1,5) succeeds. book(2,6) fails (overlaps with [1,5)). book(3,4) fails (contained within [1,5)). book(1,3) fails (overlaps with [1,5)). book(5,10) succeeds (starts where [1,5) ends).

Constraints

  • 0 ≤ start < end ≤ 109
  • At most 1000 calls will be made to book
  • All time values are integers
  • Half-open intervals: [start, end) includes start but excludes end

Visualization

Tap to expand
Hotel Reservation TimelineRoom 101Guest A: [10,20)Guest B: [15,25)✗ CONFLICTGuest C: [20,30)✓ OKTimeline Visualization:1015202530Guest A: [10,20)Guest B: [15,25)Guest C: [20,30)OVERLAP!Conflict Detection Logic• Two intervals [a,b) and [c,d) overlap if: max(a,c) < min(b,d)• Example: [10,20) and [15,25) → max(10,15) = 15, min(20,25) = 20• Since 15 < 20, intervals overlap ✗• But [10,20) and [20,30) → max(10,20) = 20, min(20,30) = 20, no overlap ✓
Understanding the Visualization
1
Guest Arrival
New guest wants to book room from check-in to check-out time
2
Check Existing
System checks against all existing reservations for conflicts
3
Conflict Detection
Two reservations conflict if their time periods overlap at any point
4
Decision
Accept booking if no conflicts, reject if any overlap found
Key Takeaway
🎯 Key Insight: Half-open intervals [start, end) mean end time is exclusive - guests can check out at exactly the same time another checks in!
Asked in
Google 45 Amazon 35 Microsoft 28 Meta 22
42.3K Views
High Frequency
~15 min Avg. Time
1.8K 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