Practice/Anthropic/Employee Management System (Online Assessment)
Employee Management System (Online Assessment)
CodingMust
Problem Overview
You are implementing an employee management system with four progressive levels of complexity. Each level builds on the previous one, adding new features for tracking employee time, promotions, and compensation.
This is a typical online assessment (OA) problem where levels unlock sequentially. The starter code for Level 1 is provided - you'll extend it for subsequent levels.
Level Summary
- Level 1: Add employees and track clock in/out
- Level 2: Query total office time and find top K employees by office time
- Level 3: Promotions with deferred activation and salary queries for time periods
- Level 4: Double salary time periods
Important Notes
- Timestamps are in milliseconds
- Design data structures that can be extended for future levels
- The starter code provides basic structure - extend it as needed
Level 1: Basic Employee Operations
Implement basic employee management functionality including adding employees and tracking when they enter and leave the office.
Operations
add_employee(name, position, hourly_salary)
- Add a new employee to the system
- Store name, position, and hourly salary rate
clock_in(name, timestamp)
- Record when employee enters the office
- Track the timestamp
clock_out(name, timestamp)
- Record when employee leaves the office
- Store clock in/out pair for later calculations
Example
`
ems = EmployeeManagementSystem()
ems.add_employee("Alice", "Engineer", 50.0)
ems.clock_in("Alice", 1000)
ems.clock_out("Alice", 5000)
Alice worked from timestamp 1000 to 5000 (4000 time units)
`
Requirements
- Store employee information
- Maintain records of clock in/out times
- Handle multiple clock sessions per employee
Clarification Questions to Ask
- Can employees clock in multiple times without clocking out?
- What happens if clock_out is called without a matching clock_in?
- Should we validate timestamps (ensure clock_out is greater than clock_in)?
Level 2: Office Time Tracking
Support querying employee office time statistics and ranking employees by time spent in the office.
New Operations
get_total_office_time(name)
- Calculate total time an employee has spent in the office
- Sum across all clock in/out sessions
- Return total time as an integer
top_k_employees_by_office_time(k)
- Find the top K employees who have spent the most time in the office
- Return list of employee names
- Ranked by total office time (descending)
Example
`
ems.add_employee("Bob", "Manager", 75.0)
ems.clock_in("Bob", 0)
ems.clock_out("Bob", 3000)
ems.clock_in("Bob", 5000)
ems.clock_out("Bob", 9000)
total = ems.get_total_office_time("Bob")
Returns: 7000 (3000 + 4000)
top = ems.top_k_employees_by_office_time(1)
Returns: ["Bob"] if Bob has most office time
`
Requirements
- Efficiently calculate cumulative office time
- Sort employees by time spent in office
- Handle edge case when K is greater than number of employees
Level 3: Promotions and Salary Queries
Implement promotion functionality with deferred activation and support querying total compensation for any time period.
New Operations
announce_promotion(name, new_position, new_salary)
- Announce a promotion for an employee
- Promotion does NOT take effect immediately
- Promotion takes effect on the next clock in after announcement
- New salary applies to all hours worked after promotion takes effect
query_salary_for_period(name, start, end)
- Calculate total salary earned during time period
[start, end]
- Formula:
Total Salary = Clock Time × Hourly Rate
- Must account for different hourly rates if promotions occurred during the period
- Only count time when employee was actually in the office (clocked in)
Example
`
ems.add_employee("Alice", "Engineer", 50.0)
ems.clock_in("Alice", 1000)
ems.clock_out("Alice", 3000)
Alice works 2000 units at 50/unit = 100000
ems.announce_promotion("Alice", "Senior Engineer", 75.0)
ems.clock_in("Alice", 5000) # Promotion takes effect now
ems.clock_out("Alice", 8000)
Alice works 3000 units at 75/unit = 225000
salary = ems.query_salary_for_period("Alice", 0, 10000)
Returns: 325000 (100000 + 225000)
`
Requirements
- Store pending promotions
- Apply promotion on next clock in
- Track hourly rate for each clock session
- Calculate salary considering overlapping time periods and rate changes
Key Insight
Store the hourly rate with each clock record to handle salary changes between sessions:
employee.clock_records.append((clock_in, clock_out, hourly_salary_at_time))
Level 4: Double Salary Periods
Support designating certain time periods as double salary periods, where employees earn twice their normal hourly rate.
New Operation
define_double_salary_period(start, end)
- Designate time period
[start, end] as double salary period
- During these periods, effective hourly rate = 2 × base hourly rate
- Multiple double salary periods can be defined
Salary Calculation with Double Salary
When calculating salary for a time period:
- Hours worked during double salary periods earn double the normal rate
- Handle cases where work sessions overlap with double salary periods
- Handle cases where promotions and double salary periods interact
Example
`
ems.add_employee("Charlie", "Engineer", 50.0)
ems.define_double_salary_period(2000, 4000)
ems.clock_in("Charlie", 1000)
ems.clock_out("Charlie", 5000)
salary = ems.query_salary_for_period("Charlie", 1000, 5000)
Breakdown:
1000-2000: normal (1000 × 50 = 50000)
2000-4000: double (2000 × 50 × 2 = 200000)
4000-5000: normal (1000 × 50 = 50000)
Total: 300000
`
Requirements
- Store multiple double salary periods
- Segment work periods at double salary boundaries
- Calculate each segment separately with correct multiplier
- Handle overlapping double salary periods