DIY: My Calendar

Solve the interview question "My Calendar" in this lesson.

Problem statement

You need to implement a class called MyCalendar that has a function called book(). This function takes the starting and ending time of an event as book(start, end) as input, and returns a Boolean representing whether the event was booked successfully. An event can be booked successfully only if it doesn’t conflict with an already booked event. For example, if we call book(1, 3), followed by book(2, 4), and then book(6, 9), the first and third bookings succeed, but the second does not. If the new event conflicts with an existing event, it returns false.

Input

The MyCalendar class constructor does not take any input. However, the book() function takes two integer inputs representing the starting and ending time of the event as input. The following is an example of consecutive inputs to the book() function:

MyCalendar.book(2, 4)
MyCalendar.book(6, 8)
MyCalendar.book(3, 5)

Output

The output of the book() function is a Boolean representing whether the event can be booked. The following is an example output:

true
true
false

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.