Coding questions
Track Workers, Sessions, Promotions, and Bonus Pay
Implement worker sessions, current metadata, promotions, ordered filtering, and time-clipped compensation with historical rates and non-stacking double-pay intervals.
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 whosename,level, orrateequals value. Sort ascending byname,level, or numericrateas 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 except0.
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
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.
Track Worker Sessions and Bonus Compensation
Approach
Maintain current worker fields in a name-keyed registry. Capture the current numeric rate on ENTER and append [start,end,rate] on LEAVE; later updates cannot change those stored sessions. GET and FILTER use current fields, with exact string equality and the prescribed ascending comparator plus name tie-breaker. Maintain the union of all bonus intervals as sorted disjoint intervals: when BONUS arrives, sort and merge overlapping or touching ranges, then build prefix sums of their lengths. Merging touching half-open intervals preserves their union. To find covered length before time t, binary-search the first union interval whose end exceeds t. Prefix sums count completed intervals; add any partial length of that next interval before t. Thus covered(high)-covered(low) gives exactly the bonus minutes in a clipped session. TOTAL sums rate*(ordinary minutes + bonus minutes), adding one extra rate for each covered minute and never stacking bonuses. Union updates apply to all recorded sessions at subsequent queries, including bonuses entered after those sessions. Half-open clipping omits zero-length intersections. Rates and minutes are integral, with Java/C++ wide integers; at most 200 nonoverlapping worker timelines yield at most 400000000000000 doubled cents, below JavaScript exact-integer limits. With W workers, S completed sessions and B stored union intervals, ADD/GET/UPDATE/PROMOTE/ENTER/LEAVE are expected O(1) aside from string costs. FILTER takes O(W log W), BONUS O((B+1) log(B+1)), and TOTAL O(S log(B+2)). State is O(W+S+B), plus returned rows and stored string characters.
- Time complexity: FILTER O(W log W); BONUS O((B+1) log(B+1)); TOTAL O(S log(B+2)); other operations expected O(1), excluding string costs
- Space complexity: O(W + S + B) state plus string characters and output