DIY: My Calendar

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

Problem statement

You need to implement a module called MyCalendar that has a function called book(). This function takes the struct object along with the starting and ending time of an event as book(obj, start, end_var) 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(obj, 1, 3), followed by book(obj, 2, 4), and then book(obj, 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 module init function does not take any input. However, the book() function takes two integer inputs representing the starting and ending time of the event as input along with the struct object. The following is an example of consecutive inputs to the book() function:

MyCalendar.book(%MyCalendar{}, 2, 4)
MyCalendar.book(%MyCalendar{}, 6, 8)
MyCalendar.book(%MyCalendar{}, 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 module with init and book(obj, start, end_var) functions using the skeleton code given below:

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