My Calendar I - Problem
Design a Calendar System: You're building a smart calendar application that prevents double bookings. Your task is to implement a
Key Concepts:
• Events are represented as half-open intervals
• Two events conflict if they share any moment in time
• The interval
Example: Event
Your Implementation Must Support:
•
•
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 endTimeExample: 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
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!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code