Problem · Hash Table

Worker Management with Full-Shift Double Pay

Learn this problem
HardAirbnb logoAirbnbFULLTIMEOA

Problem statement

Process a finite ordered batch of operations for a worker management system. Return one string result for every operation, in encounter order.

Workers and Office Sessions

  • ["ADD_WORKER", worker_id, position, compensation]: add a worker with an hourly compensation. Return true, or false if the ID already exists.
  • ["REGISTER", worker_id, timestamp]: toggle the worker's office state. When outside, start a session; when inside, finish the open session. Return registered, or invalid_request if the worker does not exist.
  • ["GET", worker_id]: return the worker's total time across completed sessions. An open session contributes nothing. Return an empty string for a missing worker.
  • ["TOP_N_WORKERS", n, position]: consider workers whose current position equals position. Rank them by completed time accrued in that position, descending, then by worker ID in ascending lexicographic order. Return up to n entries formatted as workerId(time) and separated by , . Include workers with zero time, and return an empty string when none match.

Promotions and Salary

  • ["PROMOTE", worker_id, new_position, new_compensation, start_timestamp]: queue one pending promotion. Return success, or invalid_request if the worker is missing or already has an unapplied promotion.
  • A pending promotion activates when that worker next enters at a timestamp greater than or equal to start_timestamp. The position and compensation active at session entry remain attached to that entire session.
  • ["CALC_SALARY", worker_id, start_timestamp, end_timestamp]: return gross salary earned during the half-open query interval [start_timestamp, end_timestamp). Only completed sessions contribute. Return an empty string for a missing worker.

Full-Shift Double Pay

  • ["SET_DOUBLE_PAID", start_timestamp, end_timestamp]: add the global half-open grant interval [start_timestamp, end_timestamp) and return an empty string.
  • All grant intervals registered before a salary query form one union. Overlapping and adjacent intervals combine, and grants never stack beyond double pay.
  • A completed session qualifies for double pay only when its entire half-open interval is contained in that union. A partially covered session is paid entirely at its normal rate.
  • Within the salary query interval, qualifying sessions contribute 2 * overlap_length * session_compensation; other sessions contribute overlap_length * session_compensation. Eligibility is based on the whole original session, even when the query includes only part of it.

Function

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

Examples

Example 1

operations = [["ADD_WORKER","Alice","Engineer","100"],["REGISTER","Alice","10"],["REGISTER","Alice","30"],["SET_DOUBLE_PAID","10","20"],["SET_DOUBLE_PAID","20","30"],["CALC_SALARY","Alice","0","40"],["REGISTER","Alice","40"],["REGISTER","Alice","60"],["SET_DOUBLE_PAID","45","55"],["CALC_SALARY","Alice","0","70"]]return = ["true","registered","registered","","","4000","registered","registered","","6000"]

The adjacent grants [10,20) and [20,30) form a union that contains Alice's full session [10,30), so that session earns 20 * 100 * 2 = 4000. The later grant [45,55) covers only part of [40,60), so that second session earns its normal 20 * 100 = 2000. The final gross salary is 6000.

Example 2

operations = [["ADD_WORKER","Bob","Developer","100"],["REGISTER","Bob","5"],["REGISTER","Bob","15"],["PROMOTE","Bob","Senior Developer","200","20"],["REGISTER","Bob","18"],["REGISTER","Bob","22"],["REGISTER","Bob","25"],["REGISTER","Bob","35"],["GET","Bob"],["TOP_N_WORKERS","5","Senior Developer"],["SET_DOUBLE_PAID","25","35"],["CALC_SALARY","Bob","0","40"],["CALC_SALARY","Bob","28","32"]]return = ["true","registered","registered","success","registered","registered","registered","registered","24","Bob(10)","","5400","1600"]

Bob enters at 18 before the promotion start, so [18,22) keeps the old position and compensation. The promotion activates on the entry at 25. The completed durations total 24, while 10 units belong to the current senior position. Session [25,35) is fully granted and earns 4000; the earlier sessions earn 1400, for gross salary 5400. Query [28,32) overlaps four units of the same qualifying full session and returns 1600.

Constraints

  • 1 <= operations.length <= 2000.
  • Every operation has a valid name and arity, and every numeric field is a base-10 integer string.
  • Worker IDs and positions are non-empty strings of at most 50 characters.
  • 1 <= compensation <= 10^6.
  • 0 <= timestamp <= 10^9; all REGISTER timestamps are globally strictly increasing.
  • Every SET_DOUBLE_PAID and CALC_SALARY interval has start_timestamp < end_timestamp.
  • 1 <= n <= 2000 for every TOP_N_WORKERS operation.
  • Every salary result fits in a signed 64-bit integer.

More Airbnb problems

drafts saved locally
public String[] workerManagementFullShiftGrants(String[][] operations) {
    // Write your code here.
}
operations[["ADD_WORKER","Alice","Engineer","100"],["REGISTER","Alice","10"],["REGISTER","Alice","30"],["SET_DOUBLE_PAID","10","20"],["SET_DOUBLE_PAID","20","30"],["CALC_SALARY","Alice","0","40"],["REGISTER","Alice","40"],["REGISTER","Alice","60"],["SET_DOUBLE_PAID","45","55"],["CALC_SALARY","Alice","0","70"]]
expected["true", "registered", "registered", "", "", "4000", "registered", "registered", "", "6000"]
checking account