My Calendar I

MediumIntervalsbinary search

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Keep a calendar that never allows two events to overlap. An event is a half-open interval [start, end): it includes start and excludes end, so an event ending at 20 does not conflict with one starting at 20. Implement: - MyCalendar() creates an empty calendar. - book(start, end) adds the event and returns true if it does not overlap any event already booked; otherwise it leaves the calendar unchanged and returns false. Two events [s1, e1) and [s2, e2) overlap exactly when s1 < e2 and s2 < e1. Rejected bookings are not stored.

Examples

Input: ["MyCalendar","book","book","book","book","book"] [[],[10,20],[15,25],[20,30],[5,10],[9,11]]
Output: [null,true,false,true,true,false]
[15,25) overlaps [10,20). [20,30) and [5,10) only touch existing events at their endpoints, so they fit. [9,11) overlaps both [5,10) and [10,20).
Input: ["MyCalendar","book","book","book"] [[],[0,1],[0,1],[1,2]]
Output: [null,true,false,true]
An identical interval overlaps; an adjacent one does not.

Constraints

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

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More binary search problems