Worker Management, Part 4: Double-Paid Intervals
Problem statement
The source assessment is a cumulative four-level worker-hours register for contract workers at a facility. Each level includes every operation from the earlier levels. This page implements Levels 1 through 4 and returns one string result for every query in input order.
Level 1: Workers and Office Sessions
["ADD_WORKER", workerId, position, compensation]adds a new worker and stores the worker's position and compensation. IfworkerIdalready exists, leave the system unchanged and returnfalse; otherwise returntrue. Worker IDs and positions contain only English letters and spaces.["REGISTER", workerId, timestamp]records an office entry when the worker is outside and records an exit when the worker is inside. Calls toREGISTERarrive in increasing timestamp order. Returninvalid_requestfor an unknown worker andregisteredafter a successful entry or exit.["GET", workerId]returns the worker's total time in the office using completed sessions only. A current session without an exit contributes nothing. Return the empty string when the worker does not exist.
Level 2: Ordered Worker Statistics
["TOP_N_WORKERS", n, position]returns at mostnworkers whose current position equalsposition.- Order workers by completed office time in descending order. Break equal-time ties by worker ID in ascending alphabetical order.
- Format the result as
workerId1(time1), workerId2(time2), .... Return every matching worker when fewer thannexist, and return the empty string when there are no matches. - A worker with no completed office session has time
0.
Level 3: Promotions and Salary
["PROMOTE", workerId, newPosition, newCompensation, startTimestamp]schedules a new position and compensation. The source guarantees thatnewPositiondiffers from the worker's current position and thatstartTimestampis greater than the timestamp of the latestREGISTERcall for any worker.- A scheduled promotion becomes active on the worker's first office entry whose timestamp is at least
startTimestamp. A session beginning earlier keeps the previous position and compensation. - Return
invalid_requestwhen the worker does not exist or already has a promotion waiting to take effect. Returnsuccesswhen the promotion is scheduled. - After promotions exist,
TOP_N_WORKERSselects workers by current position and ranks them using completed time accumulated in that current position.GETcontinues to return lifetime completed time across past and current positions. ["CALC_SALARY", workerId, startTimestamp, endTimestamp]returns salary earned between the two timestamps. Only completed office sessions contribute. For each contributing portion of a session, multiply its duration by the compensation attached to that session. Return the empty string for an unknown worker.
Level 4: Double-Paid Periods
["SET_DOUBLE_PAID", startTimestamp, endTimestamp]registers a period during which worked time receives double pay.- Double-paid periods may overlap, but overlapping coverage never stacks beyond a
2× multiplier. - The operation has no worker parameter, so the registered periods apply to every worker.
CALC_SALARYpays normal compensation for completed-session time and adds one extra copy of compensation for covered time.
FastPrep representation choices
- Salary-query and double-paid periods use half-open intervals
[startTimestamp, endTimestamp). SET_DOUBLE_PAIDreturns the empty string because the source report preserves its state change but not a scalar return value.
FastPrep Runner Interface
The source uses an array of query rows and one string output per query. FastPrep exposes the same operation rows as workerManagementLevel4(String[][] operations) and returns String[]. Numeric arguments are encoded as base-10 strings.
Multipart Series
Function
workerManagementLevel4(operations: String[][]) → String[]Examples
Example 1
operations = [["ADD_WORKER","Alice","Engineer","100"],["REGISTER","Alice","10"],["REGISTER","Alice","30"],["SET_DOUBLE_PAID","15","25"],["CALC_SALARY","Alice","0","40"],["SET_DOUBLE_PAID","20","35"],["CALC_SALARY","Alice","0","40"]]return = ["true","registered","registered","","3000","","3500"]Alice's completed session pays 20*100 = 2000 normally. The first double-paid interval overlaps it for 10 units, producing 3000. After merging [15,25) with [20,35), the session has 15 double-paid units, producing 3500.
Constraints
1 <= operations.length <= 500, matching the source's query-count bound.- Every row uses an operation available at this level, has the documented arity, and satisfies the source-stated operation preconditions.
- Every numeric argument is a valid base-10 integer string.
- Worker IDs and positions contain only English letters and spaces.
- All
REGISTERcalls are supplied in increasing timestamp order. - Each
PROMOTEuses a new position different from the worker's current position. - A promotion's
startTimestampis greater than the timestamp of the latest priorREGISTERcall for any worker. - Each salary query satisfies
endTimestamp > startTimestamp >= 0. - For the FastPrep interval representation, each
SET_DOUBLE_PAIDquery satisfiesendTimestamp > startTimestamp >= 0.