KatInterview

Shift Ledger: Rates, Promotions and Bonus Windows

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designdesignsimulationinterval-merging

Last updated

Simulate a command stream for a shift ledger with deferred pay-rate changes, merged global bonus windows and a ranking of employees by hours worked.

Shift Ledger

You are building a small in-memory ledger that records employee shifts and computes pay. Instead of a class, you implement one function that replays a list of commands in order and returns one result per command.

commands[i] is the name of a command and params[i] is the list of its arguments. The function returns a list results of the same length, where results[i] is the output of command i.

Commands

CommandArgumentsOutput
register_employeeemp_id, base_rateNone
schedule_promotionemp_id, effective_time, new_rateNone
add_bonus_windowstart_time, end_timeNone
clock_inemp_id, timestampNone
clock_outemp_id, timestampNone
get_total_payemp_idtotal pay as a number
get_top_k_workedklist of employee ids

Rules

Registration. register_employee creates an employee whose current hourly rate is base_rate, with no hours and no pay. Registering an id that already exists replaces that employee with a fresh record.

Promotions. schedule_promotion queues a rate change for one employee. A queued promotion becomes active only at one of that employee's clock-ins, never in the middle of a shift.

  • At clock_in(emp_id, timestamp), every queued promotion of that employee with effective_time <= timestamp is applied and removed from the queue.
  • Due promotions are applied in ascending effective_time. Promotions with equal effective_time are applied in the order they were scheduled. The last one applied becomes the employee's current rate.
  • Promotions with effective_time > timestamp stay queued.
  • The current rate at the end of this step is locked in for the whole shift. Promotions scheduled while the shift is open do not change that shift's rate.

Bonus windows. add_bonus_window(start_time, end_time) adds a global half-open window [start_time, end_time). Windows are shared by all employees. Overlapping or touching windows behave as one merged window, so no time unit is ever rewarded more than once.

Shifts and pay. clock_in opens a shift at timestamp. clock_out closes it, so the shift covers [clock_in_time, timestamp). At the moment of clock_out, take the bonus windows that exist at that moment and compute:

  • duration = timestamp - clock_in_time
  • bonus_hours = number of time units of the shift that lie inside the union of bonus windows
  • normal_hours = duration - bonus_hours
  • shift pay = locked_rate * (normal_hours + 2.0 * bonus_hours)

The duration is added to the employee's total worked time and the shift pay is added to their total pay. A bonus window added after a shift has closed never changes that shift's pay.

Queries.

  • get_total_pay returns the sum of the pay of the employee's closed shifts, or 0.0 if none are closed. Open shifts contribute nothing.
  • get_top_k_worked returns the ids of the k employees with the largest total closed-shift duration, in descending order of duration. Ties are broken by emp_id in ascending lexicographic order. If k exceeds the number of employees, return all of them. Employees with zero hours are included and ranked by the same rule.

All timestamps and durations are integers (hours). Every employee id used in a command was registered earlier.

Constraints

  • 0 <= timestamp <= 10^9, and all window and promotion times are integers in the same range.
  • 0 <= start_time < end_time <= 10^9 for every bonus window.
  • For any single employee, clock_in and clock_out alternate, and a shift always has a strictly positive length (clock_out timestamp is greater than the matching clock_in timestamp). A new shift starts after the previous one ends.
  • commands and params have the same length, at most 10^5.
  • Rates are non-negative numbers.

Examples

Example 1

Input: run_shift_ledger(['register_employee', 'add_bonus_window', 'add_bonus_window', 'clock_in', 'clock_out', 'schedule_promotion', 'clock_in', 'clock_out', 'get_total_pay', 'get_top_k_worked'], [['E1', 20], [2, 5], [4, 7], ['E1', 1], ['E1', 4], ['E1', 5, 30], ['E1', 6], ['E1', 9], ['E1'], [1]])

Output: [None, None, None, None, None, None, None, None, 220, ['E1']]

Example 2

Input: run_shift_ledger(['register_employee', 'register_employee', 'add_bonus_window', 'clock_in', 'clock_in', 'clock_out', 'clock_out', 'get_total_pay', 'get_total_pay', 'get_top_k_worked'], [['A', 10], ['B', 15], [0, 3], ['A', 0], ['B', 1], ['A', 3], ['B', 5], ['A'], ['B'], [2]])

Output: [None, None, None, None, None, None, None, 60, 90, ['B', 'A']]

Hints

Hint 1

Per employee, keep the current rate, the open shift's start time and locked rate, total hours and total pay. Keep a separate list of scheduled promotions.

Hint 2

At clock-in, sort the employee's promotions by effective time (a stable sort keeps scheduling order for ties), apply every due one, and keep the rest. Store the resulting rate with the open shift.

Hint 3

Keep the bonus windows as a sorted list of disjoint intervals and merge a new window with every interval it overlaps or touches. At clock-out, sum the intersection of the shift with each merged window.

Hint 4

Rank employees with the sort key (-total_hours, emp_id) and slice the first k.