Design a stateful employee working-hours register that handles badge events, completed time, position rankings, delayed promotions, historical pay, and grant periods. The exercise tests validation, deterministic tie handling, interval boundaries, conflicting updates, scalability, and reproducible pay calculations.
Design a working-hours register for employees who badge into and out of an office. Describe the data model, public operations, validation rules, and algorithms. You may use in-memory structures, but the design should make state transitions explicit and testable.
### Part 1: Attendance and Total Time
Support adding an employee, recording alternating entry and exit badge events, and querying the employee's total completed working time. Define how duplicate employees, an exit without an open session, and an entry while already inside are handled.
#### What This Part Should Cover
- Employee identity and current inside/outside state
- Completed sessions versus an open session
- Clear invalid-operation behavior
- Complexity of updates and total-time queries
### Part 2: Top Workers
Return the top `n` employees for a position, ordered by completed working time. Define deterministic tie-breaking and explain whether open sessions count.
#### What This Part Should Cover
- Position-aware ranking
- Stable tie-breaking
- The cost of sorting on demand versus maintaining an index
### Part 3: Delayed Promotions and Historical Pay
A promotion specifies a new position and compensation but takes effect only on the employee's next successful entry. Calculate pay over a time interval using the compensation attached to each completed session when that session began.
#### What This Part Should Cover
- Pending-promotion state and its activation point
- Session-level compensation history
- Interval overlap calculations
- Repeated or conflicting promotion requests
### Part 4: Double-Pay Grant Periods
Add grant periods. A completed session earns double pay only when the entire session lies within a grant period. Support querying both total pay and the portion attributable to the double-pay bonus.
#### What This Part Should Cover
- Boundary semantics for full containment
- Overlapping or duplicate grant periods
- Separation of base pay from bonus pay
- Tests around exact boundaries and promotions
### What a Strong Answer Covers
A strong answer uses one authoritative state transition for badge events, records enough history to reproduce pay, and avoids recomputing facts that can be accumulated safely. It states assumptions about timestamps, money, and incomplete sessions.
### Follow-up Questions
- How would you persist the register and make badge events idempotent?
- How would you support corrections to a historical event?
- What indexing strategy would you use for millions of sessions and grant periods?
Quick Answer: Design a stateful employee working-hours register that handles badge events, completed time, position rankings, delayed promotions, historical pay, and grant periods. The exercise tests validation, deterministic tie handling, interval boundaries, conflicting updates, scalability, and reproducible pay calculations.
Design a working-hours register for employees who badge into and out of an office. Describe the data model, public operations, validation rules, and algorithms. You may use in-memory structures, but the design should make state transitions explicit and testable.
Part 1: Attendance and Total Time
Support adding an employee, recording alternating entry and exit badge events, and querying the employee's total completed working time. Define how duplicate employees, an exit without an open session, and an entry while already inside are handled.
What This Part Should Cover Guidance
Employee identity and current inside/outside state
Completed sessions versus an open session
Clear invalid-operation behavior
Complexity of updates and total-time queries
Part 2: Top Workers
Return the top n employees for a position, ordered by completed working time. Define deterministic tie-breaking and explain whether open sessions count.
What This Part Should Cover Guidance
Position-aware ranking
Stable tie-breaking
The cost of sorting on demand versus maintaining an index
Part 3: Delayed Promotions and Historical Pay
A promotion specifies a new position and compensation but takes effect only on the employee's next successful entry. Calculate pay over a time interval using the compensation attached to each completed session when that session began.
What This Part Should Cover Guidance
Pending-promotion state and its activation point
Session-level compensation history
Interval overlap calculations
Repeated or conflicting promotion requests
Part 4: Double-Pay Grant Periods
Add grant periods. A completed session earns double pay only when the entire session lies within a grant period. Support querying both total pay and the portion attributable to the double-pay bonus.
What This Part Should Cover Guidance
Boundary semantics for full containment
Overlapping or duplicate grant periods
Separation of base pay from bonus pay
Tests around exact boundaries and promotions
What a Strong Answer Covers Guidance
A strong answer uses one authoritative state transition for badge events, records enough history to reproduce pay, and avoids recomputing facts that can be accumulated safely. It states assumptions about timestamps, money, and incomplete sessions.
Follow-up Questions Guidance
How would you persist the register and make badge events idempotent?
How would you support corrections to a historical event?
What indexing strategy would you use for millions of sessions and grant periods?