Design an Employee Attendance System
🕘 What is an employee attendance system? It is the software behind the badge reader at an office door. Every time an employee taps in or out, it records the time. From those taps it works out how long each person worked on each day, whether that counts as a full day, and it gives HR a report for the month.
This is a good first LLD problem. There are only a few classes, but there are two details that most first attempts get wrong: people check in and out several times a day, and night shifts cross midnight. Getting those right, and keeping the "what counts as a full day" rule in one place, is most of the interview.
Requirements
"Design an attendance system. Employees check in and check out. HR wants to know who was present each day and a summary report."
Short prompts like this hide the rules. The questions below are the ones that change the code.
Clarifying Questions
You: Can an employee check in and out more than once a day, say for lunch?
Interviewer: Yes. Every check-in to check-out is one session, and a day's time is the sum of its sessions.
You: What about night shifts that start before midnight and end after?
Interviewer: Each day gets the minutes that fall on it. A shift from 22:00 to 06:00 is 2 hours on the first day and 6 on the second.
You: How do we decide whether someone was present?
Interviewer: 8 hours or more is present, 4 hours or more is a half day, less is absent. HR tweaks those numbers from time to time.
You: What if someone taps in twice, or taps out without tapping in?
Interviewer: Reject the second tap and tell the reader it failed. Don't guess.
You: What should the report contain?
Interviewer: For a range of days, how many days each employee was present, half day and absent, sorted by employee id.
You: Do we read the clock?
Interviewer: No, the badge reader sends a timestamp in minutes. Day 0 starts at minute 0.
The second answer is the one to slow down on. "Credit the whole shift to the day it started" is simpler and many candidates assume it. The interviewer said otherwise, and a test with a night shift will tell the two apart.
💡 Interview tip: Ask about midnight before you design storage. Whether a session belongs to one day or is split decides what you store and how you add it up. It is a one-sentence question that saves a rewrite.
Final Requirements
- HR can register employees by id.
- An employee checks in to start a session and checks out to end it. A second check-in while checked in, or a check-out while not checked in, is rejected.
- An employee can have any number of sessions per day. A day's worked time is the sum of the parts of finished sessions that fall on that day; a session that crosses midnight is split between the days.
- A day's status is PRESENT at 480 minutes or more, HALF_DAY at 240 or more, otherwise ABSENT. These thresholds change, so the rule must be easy to swap.
- Anyone can ask how many minutes an employee worked on a day, and what the day's status was.
- HR can ask for a report over a range of days: for each employee, sorted by id, the number of present, half and absent days.
Out of scope
- Leave requests, holidays and weekends (every day in a range counts).
- Payroll and overtime pay.
- Badge hardware, photos and access control.
- Several badge readers at the same time (we come back to this in Extensibility).