My Calendar I
01 · Problem
You are building the booking engine for a personal calendar. Each event occupies the half-open time range [start, end), meaning it covers every real number t with start <= t < end. Two events conflict (a double booking) when their ranges share at least one moment. Events that merely touch, such as [10, 20) and [20, 30), do not conflict.
Implement the MyCalendar class:
MyCalendar()creates an empty calendar.boolean book(int start, int end)tries to add the event[start, end). If it does not overlap any event already stored, store it and returntrue. Otherwise leave the calendar unchanged and returnfalse.
Rejected events are never stored, so they cannot block later requests.
The input lists the operation names and their argument lists. The output lists each call's return value, using null for the constructor.
02 · Examples
["MyCalendar","book","book","book"], [[],[10,20],[15,25],[20,30]]
[null,true,false,true]
[10,20) is booked. [15,25) overlaps it on [15,20), so it is rejected. [20,30) only touches [10,20) at 20, which is allowed.
["MyCalendar","book","book","book","book"], [[],[5,10],[1,5],[3,8],[10,12]]
[null,true,true,false,true]
[5,10) and [1,5) touch but do not overlap. [3,8) overlaps both stored events and is rejected. [10,12) starts exactly where [5,10) ends.
["MyCalendar","book","book","book"], [[],[0,100],[50,60],[100,101]]
[null,true,false,true]
[50,60) lies inside [0,100) and is rejected. [100,101) begins right after [0,100) ends.
03 · Constraints
- 010 <= start < end <= 109
- 02At most 1000 calls will be made to book
- 03Event ranges are half-open: [start, end)
04 · Optimal complexity
- Time
- O(log n) per book
- Space
- O(n)
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.