Skip to content
Decoder

My Calendar I

Medium

Problem

Build a MyCalendar class for booking events. book(start, end) tries to add the event [start, end), which is half-open: it includes start but not end. If the new event would overlap any event already booked, reject it and return false without storing it. Otherwise store it and return true.

Example 1

cal = MyCalendar()

Example 2

Input
cal.book(10, 20)
Output
true

Example 3

Input
cal.book(15, 25)
Output
false
Why
15 to 20 is taken

Example 4

Input
cal.book(20, 30)
Output
true
Why
starts exactly when the first ends

The TypeScript class has the same shape: new MyCalendar() and cal.book(start, end).

Constraints

  • 0 <= start < end <= 10^9
  • At most 1000 calls to book.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.