The My Calendar II problem extends the booking problem to allow up to one overlap (double-booking) but no triple-booking. The book(start, end) returns true if adding the event doesn't cause any time slot to be covered three or more times, and false otherwise. This My Calendar II coding problem tests two-level interval tracking.
Uber, Amazon, and Google ask this as a direct extension of My Calendar I. It tests whether candidates can extend their interval tracking to handle "at most k overlaps" — a generalization pattern. The array, design, binary search, ordered set, segment tree, and prefix sum interview pattern is applied here with two levels of overlap tracking.
Two sorted lists: booked (all confirmed events) and overlaps (regions where exactly one overlap exists). For book(start, end):
overlaps. If yes, return false (would cause triple-booking).booked. Add these intersections to overlaps.booked. Return true.Book (10, 20): booked=[(10,20)], overlaps=[]. Return true. Book (15, 25): intersect with (10,20)→(15,20). overlaps=[(15,20)]. booked=[(10,20),(15,25)]. Return true. Book (20, 30): check overlaps (15,20). 20 ≥ 20 → no overlap with overlaps. Add to booked. Return true. Book (17, 22): check overlaps. (17,22) overlaps (15,20)? 17<20 AND 15<22 → overlap! Return false.
My Calendar II introduces the concept of "tracking overlaps at each level." The pattern generalizes: maintain a list for each booking level. Checking the highest-level list first prevents the next booking level. After mastering I and II, the difference map approach (segment tree or sorted difference array) handles arbitrary k levels efficiently — a key technique for My Calendar III. Practice the two-list approach here until it's fluent.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| My Calendar I | Medium | Solve | |
| My Calendar III | Hard | Solve | |
| Fruits Into Baskets III | Medium | Solve | |
| Design Exam Scores Tracker | Medium | Solve | |
| Range Frequency Queries | Medium | Solve |