My Calendar I
Problem Statement
Build a calendar class that never allows a double booking: two events that share some moment of time.
MyCalendar()creates an empty calendar.bool book(int startTime, int endTime)tries to add an event covering the half-open range[startTime, endTime), meaning every timetwithstartTime β€ t < endTime. If it does not overlap any event already in the calendar, add it and returntrue. Otherwise do not add it and returnfalse.
Because the end is not included, an event ending at 20 and one starting at 20 do not overlap.
A sequence of book(startTime, endTime) calls.
For each call, true if the event was booked, false if it was rejected.
[10,20) is booked. [15,25) overlaps it at 15 β¦ 19, so it is rejected. [20,30) starts exactly when [10,20) ends, so it fits.
- β’ 0 β€ startTime < endTime β€ 10^9
- β’ At most 1000 calls to book
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Keep a list of bookings; scan all of them on every call
map start β end; only the booking at lower_bound(start) and the one before it can clash
Store every accepted booking in a vector. For a new event [s, e), loop over all bookings [s2, e2) and reject if s < e2 && s2 < e. If none clash, push it into the vector.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Intervals translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.