Design an Apartment Visitor System
🏢 What is an apartment visitor system? It is the app a guard uses at the gate of an apartment complex. When a visitor arrives, the guard types in who they are and which flat they are visiting. The residents of that flat get a notification and tap approve or deny. Residents who expect a guest can also send them a pass in advance, so the guest walks straight in.
This is a good easy problem because it has one small state machine (a visit goes from waiting to inside to gone), one permission rule (only the right flat may answer), and one feature that is easy to get subtly wrong (passes that can be reused or never expire). Apps like MyGate made this flow common in Indian gated communities, and interviewers like it because everyone understands it in one sentence.
Requirements
"Design a visitor management system for an apartment complex. Guards log visitors at the gate, and residents approve or deny them."
The prompt leaves out who may answer, what happens to the answer, and how guests that are expected get in. Those are the questions to ask.
Clarifying Questions
You: Who can approve a visitor? Any resident, or only the flat being visited?
Interviewer: Only residents of that flat. A flat can have several residents, and any one of them can answer.
You: If two residents of the same flat answer, which answer counts?
Interviewer: The first one. After that the visit is decided, and a second answer is refused.
You: Can a resident approve someone ahead of time, like a friend coming for dinner?
Interviewer: Yes. They create a guest pass with a time window. The guest shows the code at the gate and goes straight in. A pass works once.
You: Do we need to know who is inside right now?
Interviewer: Yes. The guard should be able to see, per flat, who is currently inside. Visitors check out when they leave.
You: How are residents told that someone is at the gate? Push, SMS, a call?
Interviewer: It will be a push notification, but don't build that. Just make sure it can be plugged in.
You: How do we get the time?
Interviewer: Pass it in with each call, as an integer.
You: What if nobody answers?
Interviewer: Leave it waiting for now. We may come back to timeouts.
The second answer is the one that shapes the code. "First answer wins" means a visit has to remember that it was already decided and refuse to change its mind. That is a state machine, even if a small one.
💡 Interview tip: When the prompt has two kinds of actors (here guards and residents), ask what each one is allowed to do to the other's data. Permission rules like "only that flat's residents" turn into the first
ifin a method, and interviewers notice when you find them on your own.
Final Requirements
- Residents are registered with a flat. A flat can have several residents; a resident belongs to one flat.
- A guard logs a visitor (name and flat). The visit starts waiting, and every resident of that flat is notified. A flat with no residents cannot receive visitors.
- A resident of the visit's flat approves or denies a waiting visit. The first answer wins; later answers are refused. Residents of other flats cannot answer.
- A resident can create a single-use guest pass for a named guest, valid within a time window. A guest who shows a valid pass enters without waiting for approval.
- A visitor who is inside can check out. Each visit's status can be looked up at any time.
- The guard can list who is inside for a flat, in the order they entered.
- The way residents are notified must be swappable without touching the rest of the system.
Out of scope
- The notification service itself (push, SMS, calls).
- Logins, photos, ID checks and vehicle numbers.
- Recurring passes for daily help, timeouts and blocklists (we come back to these in Extensibility).
- Several gates at the same time (also in Extensibility).