Problem · Hash Table

Worker Management, Part 3: Overtime Rules

Learn this problem
HardAirbnb logoAirbnbFULLTIMEOA

Problem statement

Build the worker management register through Level 3. Process the cumulative operation contract below.

Practice Interface

FastPrep-authored scaffolding: the source exposes direct methods. This practice version serializes the same calls as one finite ordered String[][] operations batch. Implement workerManagementLevel3Overtime and return one string response per row, in order. Direct list results are joined as described below; a missing optional integer is serialized as an empty string.

Operations from Levels 1 and 2

  • ["ADD_WORKER", workerId, position, compensation]: add a worker. Return true, or false when the ID already exists.
  • ["REGISTER", workerId, timestamp]: toggle entry or exit. Return registered, or invalid_request for a missing worker.
  • ["GET", workerId]: return total time in completed sessions, or an empty string for a missing worker. An open session contributes nothing.
  • ["TOP_N_WORKERS", n, position]: consider workers whose current position equals position. Sort by completed-session time descending, then worker ID alphabetically; format each as workerId(time) and join with , . Include zero-time workers and return an empty string when no worker matches.

Level 3 Operations

  • ["SET_OVERTIME_RULE", workerId, threshold, timestamp]: on success, return success. Return invalid_request for a missing worker.
  • The rule applies to sessions that begin after the operation. In each such session, the first threshold time units are paid at the session's regular compensation and all remaining time is paid at twice that compensation.
  • A rule change while the worker is inside does not change the open session; it applies on the next entry. If several rules are set before that entry, only the latest applies.
  • ["CALC_SALARY", workerId, startTimestamp, endTimestamp]: return salary from completed-session time overlapping the query range, or an empty string for a missing worker. The regular/overtime split is measured from each session's start. With no applicable rule, the whole overlap uses regular compensation.
  • Changing overtime rules never changes GET or TOP_N_WORKERS.

Function

workerManagementLevel3Overtime(operations: String[][]) → String[]

Examples

Example 1

operations = [["ADD_WORKER","Alice","Developer","10"],["SET_OVERTIME_RULE","Alice","3","10"],["REGISTER","Alice","20"],["SET_OVERTIME_RULE","Alice","1","25"],["REGISTER","Alice","26"],["CALC_SALARY","Alice","0","100"],["REGISTER","Alice","30"],["REGISTER","Alice","34"],["GET","Alice"],["CALC_SALARY","Alice","22","33"],["TOP_N_WORKERS","1","Developer"]]return = ["true","success","registered","success","registered","90","registered","registered","10","120","Alice(10)"]

The first session uses threshold 3, even though the rule changes while it is open, and earns 90. The second session uses threshold 1 and earns 70. The partial query earns 120, and completed time is 10.

Example 2

operations = [["ADD_WORKER","Bob","QA","8"],["ADD_WORKER","Cara","QA","12"],["REGISTER","Bob","5"],["REGISTER","Bob","9"],["SET_OVERTIME_RULE","Cara","2","10"],["SET_OVERTIME_RULE","Cara","1","11"],["REGISTER","Cara","12"],["REGISTER","Cara","15"],["CALC_SALARY","Bob","6","8"],["CALC_SALARY","Cara","13","15"],["GET","Missing"],["SET_OVERTIME_RULE","Missing","3","16"],["TOP_N_WORKERS","5","QA"]]return = ["true","true","registered","registered","success","success","registered","registered","16","48","","invalid_request","Bob(4), Cara(3)"]

Bob has no overtime rule, so two queried time units earn 16. Cara's latest pre-entry threshold is 1; the queried suffix is entirely overtime and earns 48. Bob ranks before Cara by completed time.

Constraints

  • Every operation row is well formed and uses one of the listed operation shapes.
  • Worker IDs and positions contain only English letters and spaces.
  • Compensation and overtime thresholds are integer values supplied in the operation rows.
  • The mutation timestamps supplied to REGISTER, SET_OVERTIME_RULE are strictly increasing across the batch.
  • For every salary query, 0 <= startTimestamp < endTimestamp <= 10^6.
  • Durations and overlaps are timestamp differences. The practice serialization writes a session or salary range as [start, end); a boundary does not contribute an extra time unit.

More Airbnb problems

drafts saved locally
public String[] workerManagementLevel3Overtime(String[][] operations) {
  // Write your code here.
}
operations[["ADD_WORKER","Alice","Developer","10"],["SET_OVERTIME_RULE","Alice","3","10"],["REGISTER","Alice","20"],["SET_OVERTIME_RULE","Alice","1","25"],["REGISTER","Alice","26"],["CALC_SALARY","Alice","0","100"],["REGISTER","Alice","30"],["REGISTER","Alice","34"],["GET","Alice"],["CALC_SALARY","Alice","22","33"],["TOP_N_WORKERS","1","Developer"]]
expected["true", "success", "registered", "success", "registered", "90", "registered", "registered", "10", "120", "Alice(10)"]
checking account