Track Workers, Sessions, Promotions, and Bonus Pay
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Online Assessment
Implement a worker registry with working sessions, promotion and salary updates, filtering, and compensation queries. Bonus periods pay double the normal rate.
Implement `worker_payroll(operations: string[][]) -> string[][]`, returning one string-array result for each operation. This operation encoding and the timing policies below are explicit practice choices because the reported assessment provides features rather than an exact API.
### Constraints & Assumptions
- At most 2000 operations and 200 workers. Names and levels are nonempty ASCII letters/digits/underscores. Worker names are unique and immutable.
- Times are integer minutes from 0 through 1000000. Rates are integer cents per minute from 0 through 1000000. Use wide integers for compensation.
- All inputs satisfy the preconditions below. No invalid-operation handling is required. Each worker alternates ENTER and LEAVE; all recorded sessions have positive duration and never overlap for that worker.
- ADD, UPDATE, and PROMOTE occur while that worker is outside. A session keeps the rate in effect when it began. Updates do not change historical session pay.
- All TOTAL queries occur when every worker is outside. All sessions relevant to the query have already been recorded. BONUS periods are global and may be added before a query; overlapping bonus periods form a union and never multiply pay more than twice.
- Intervals are half-open `[start,end)`. A TOTAL query clips every recorded session to the requested interval.
### Operations and Results
- `["ADD",name,level,rate]`: add a worker; return `["OK"]`.
- `["GET",name]`: return `[name,currentLevel,currentRate]`.
- `["UPDATE",name,level,rate]` and `["PROMOTE",name,newLevel,newRate]`: replace the current level and rate; return `["OK"]`. PROMOTE is a separately named business action with the same immediate update timing in this practice contract.
- `["ENTER",name,time]`, `["LEAVE",name,time]`: open or close a session; return `["OK"]`.
- `["FILTER",field,value,sortField]`: select current workers whose `name`, `level`, or `rate` equals value. Sort ascending by `name`, `level`, or numeric `rate` as specified by sortField, breaking ties by name. Return the selected names, possibly an empty array.
- `["BONUS",start,end]`: record a positive-length double-pay interval; return `["OK"]`.
- `["TOTAL",start,end]`: return `[totalCents]` for all workers combined, with no decimal point or leading zeroes except `0`.
All integer arguments and results use ordinary base-10 strings. FILTER uses exact case-sensitive equality. The source does not require an asymptotically optimal data structure; explain the complexity of the representation you choose.
### Example
```text
operations = [["ADD","Ada","L1","10"],["ENTER","Ada","0"],
["LEAVE","Ada","10"],["BONUS","3","5"],["TOTAL","0","10"],
["PROMOTE","Ada","L2","20"],["GET","Ada"]]
result = [["OK"],["OK"],["OK"],["OK"],["120"],["OK"],["Ada","L2","20"]]
```
```hint Preserve historical rates
A worker's current salary is insufficient to price an old session after a promotion. Store the rate with the completed session and count each minute of overlapping bonus intervals only once.
```
Overview: Implement worker sessions, current metadata, promotions, ordered filtering, and time-clipped compensation with historical rates and non-stacking double-pay intervals.
Read the full Software Engineer interview experience this question came from
Implement a worker registry with working sessions, promotion and salary updates, filtering, and compensation queries. Bonus periods pay double the normal rate.
Implement `worker_payroll(operations: string[][]) -> string[][]`, returning one string-array result for each operation. This operation encoding and the timing policies below are explicit practice choices because the reported assessment provides features rather than an exact API.
### Constraints & Assumptions
- At most 2000 operations and 200 workers. Names and levels are nonempty ASCII letters/digits/underscores. Worker names are unique and immutable.
- Times are integer minutes from 0 through 1000000. Rates are integer cents per minute from 0 through 1000000. Use wide integers for compensation.
- All inputs satisfy the preconditions below. No invalid-operation handling is required. Each worker alternates ENTER and LEAVE; all recorded sessions have positive duration and never overlap for that worker.
- ADD, UPDATE, and PROMOTE occur while that worker is outside. A session keeps the rate in effect when it began. Updates do not change historical session pay.
- All TOTAL queries occur when every worker is outside. All sessions relevant to the query have already been recorded. BONUS periods are global and may be added before a query; overlapping bonus periods form a union and never multiply pay more than twice.
- Intervals are half-open `[start,end)`. A TOTAL query clips every recorded session to the requested interval.
### Operations and Results
- `["ADD",name,level,rate]`: add a worker; return `["OK"]`.
- `["GET",name]`: return `[name,currentLevel,currentRate]`.
- `["UPDATE",name,level,rate]` and `["PROMOTE",name,newLevel,newRate]`: replace the current level and rate; return `["OK"]`. PROMOTE is a separately named business action with the same immediate update timing in this practice contract.
- `["ENTER",name,time]`, `["LEAVE",name,time]`: open or close a session; return `["OK"]`.
- `["FILTER",field,value,sortField]`: select current workers whose `name`, `level`, or `rate` equals value. Sort ascending by `name`, `level`, or numeric `rate` as specified by sortField, breaking ties by name. Return the selected names, possibly an empty array.
- `["BONUS",start,end]`: record a positive-length double-pay interval; return `["OK"]`.
- `["TOTAL",start,end]`: return `[totalCents]` for all workers combined, with no decimal point or leading zeroes except `0`.
All integer arguments and results use ordinary base-10 strings. FILTER uses exact case-sensitive equality. The source does not require an asymptotically optimal data structure; explain the complexity of the representation you choose.
### Example
```text
operations = [["ADD","Ada","L1","10"],["ENTER","Ada","0"],
["LEAVE","Ada","10"],["BONUS","3","5"],["TOTAL","0","10"],
["PROMOTE","Ada","L2","20"],["GET","Ada"]]
result = [["OK"],["OK"],["OK"],["OK"],["120"],["OK"],["Ada","L2","20"]]
```
```hint Preserve historical rates
A worker's current salary is insufficient to price an old session after a promotion. Store the rate with the completed session and count each minute of overlapping bonus intervals only once.
```
Constraints
- At most 2000 valid operations and 200 workers; names and levels are nonempty ASCII letters/digits/underscores.
- Times and rates are integer decimal strings from 0 through 1000000; rates are cents per minute.
- Workers have immutable unique names, alternate ENTER/LEAVE, and have positive nonoverlapping sessions.
- ADD, UPDATE and PROMOTE occur while the worker is outside. Capture each session rate when it begins.
- All TOTAL operations occur when every worker is outside; relevant sessions are already recorded.
- Intervals are half-open. BONUS periods are global, positive-length and unioned; pay is at most doubled.
- FILTER equality is exact and case-sensitive on current name, level or rate; ascending sorting uses the selected field, numeric rate when applicable, and name ties.
- Return one string array per operation, with canonical decimal total cents. No invalid-operation handling is needed.
Examples
Input: ([['ADD', 'Ada', 'L1', '10'], ['ENTER', 'Ada', '0'], ['LEAVE', 'Ada', '10'], ['BONUS', '3', '5'], ['TOTAL', '0', '10'], ['PROMOTE', 'Ada', 'L2', '20'], ['GET', 'Ada']],)
Expected Output: [['OK'], ['OK'], ['OK'], ['OK'], ['120'], ['OK'], ['Ada', 'L2', '20']]
Explanation: The source example preserves the old session and updates current fields.
Input: ([['ADD', 'A', 'L', '10'], ['ENTER', 'A', '0'], ['LEAVE', 'A', '10'], ['BONUS', '2', '8'], ['BONUS', '4', '9'], ['TOTAL', '0', '10'], ['TOTAL', '5', '7']],)
Expected Output: [['OK'], ['OK'], ['OK'], ['OK'], ['OK'], ['170'], ['40']]
Explanation: Overlapping bonuses form one [2,9) union and queries clip sessions.