DIY: My Calendar
Solve the interview question "My Calendar" in this lesson.
We'll cover the following
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
Coding exercise
You need to implement the MyCalendar
structure and Book(start, end)
function using the skeleton code given below:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.