Design a Calendar
📅 What is a calendar system? It is the software behind a shared calendar app. People create events, some of which repeat every day or every week. They can move or cancel a single occurrence ("this Monday's stand-up is at 11:00 instead") without changing the rest of the series. The calendar reminds people before their events, and it can find a time when enough members of a team are free to meet.
Interviewers ask this problem in a few forms: "design a calendar with recurring events and one-off edits", "add reminders", "schedule a team meeting that needs N of M people within working hours". They are one design. The hard part is the same in all of them: a repeating event with no end date cannot be stored as a list of occurrences, so we store a rule plus a few exceptions, and compute occurrences when someone asks. Reminders and team scheduling are then two different questions asked of those computed occurrences.
Requirements
"Design a calendar. Users create events, events can repeat, and users get a reminder before an event."
Most interviewers stop there and wait for questions. Each answer below changes a class or a method.
Clarifying Questions
You: How do events repeat? Daily and weekly only, or things like "the first Tuesday of the month"?
Interviewer: A fixed interval is enough for now: every day, every week, every two hours. A series can stop after a number of occurrences or go on forever.
You: Can people change a single occurrence of a repeating event?
Interviewer: Yes. They can move one occurrence to another time or delete it, and the rest of the series stays as it was. Deleting the whole series should also work.
You: How do reminders work? Who sends them?
Interviewer: Each event can have one reminder, some minutes before every occurrence. Something outside calls the calendar as time passes and sends whatever is due. Don't worry about email or push.
You: For team meetings, does everyone need to be free?
Interviewer: No. The organizer says how many people are needed, say 3 of 5. Find the earliest time inside a given window, within working hours, when at least that many are free, and invite everyone who is free then.
You: Are working hours the same for everyone, every day?
Interviewer: Yes, one range like 9:00 to 17:00, every day. A meeting must fit inside one day's working hours.
You: How is time passed in? Do we deal with time zones?
Interviewer: Minutes as integers, passed into every call. One time zone. We may talk about zones later.
The first two answers decide the data model. "Forever" means we can never write out all occurrences, and "move just this one" means the model needs a place for exceptions to the rule. Ask them before anything else.
💡 Interview tip: When a prompt says "repeating" or "recurring", ask right away whether a series can be endless and whether single occurrences can be edited. Those two answers rule out the most common wrong design (expanding the series into rows) before you have drawn a single box.
Final Requirements
- An event has attendees, a start, a duration and an optional reminder. It happens once, or repeats every fixed number of minutes, either a set number of times or forever.
- One occurrence of a series can be moved to a new time and length, or deleted, without changing the others. An occurrence is named by its original start, the time the rule gives it.
- A whole event can be deleted.
- A user can list their occurrences that overlap a time window, after all edits and deletes.
- When the clock reaches a new time, the calendar returns every reminder that became due since the previous time, once each.
- Given team members, a needed count, a duration and a window, the calendar finds the earliest start, inside one day's working hours and inside the window, when at least that many members are free. It creates the event for all free members.
Out of scope
- Time zones and daylight saving time (we come back to this in Extensibility).
- Rules like "every weekday" or "the last Friday of the month" (also in Extensibility).
- Sending emails or push messages, and invitations with accept or decline.
- "Edit this and all following occurrences" (Extensibility).