My Calendar II

MediumIntervalsdesign

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

Build a calendar that accepts new events as long as no moment would be covered by **three** events at once. Double bookings are allowed; triple bookings are not. Each event is a half-open interval [startTime, endTime): it covers every real time t with startTime <= t < endTime. So an event ending at 10 and another starting at 10 do not overlap. - MyCalendarTwo() creates an empty calendar. - book(startTime, endTime) adds the event and returns true if doing so creates no triple booking. Otherwise it returns false and the event is **not** added.

Examples

Input: ["MyCalendarTwo","book","book","book","book","book","book"] [[],[2,8],[20,30],[4,22],[6,10],[8,12],[21,25]]
Output: [null,true,true,true,false,true,false]
[4,22) double-books [4,8) and [20,22). [6,10) would triple-book [6,8), so it is rejected. [8,12) only overlaps [4,22) because [2,8) ends at 8. [21,25) would triple-book [21,22).
Input: ["MyCalendarTwo","book","book","book","book"] [[],[0,5],[0,5],[0,5],[5,10]]
Output: [null,true,true,false,true]
The same slot can be booked twice but not three times. [5,10) touches [0,5) only at its excluded end.

Constraints

  • `0 <= startTime < endTime <= 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 design problems