KatInterview

Shift Tracker and Earnings Simulator

Greedy & IntervalsAuto-graded · 16 test cases
MediumAuto-gradedGreedy & Intervalsdesignsimulationintervals

Last updated

Simulate employee shifts, structural updates, bonus intervals, filtering, and time-bounded payroll computations.

Design and implement a system to manage staff profiles, record work shifts, handle promotions, and evaluate payroll over arbitrary time windows.

You will process a sequence of operational commands. Each command returns a list of strings as its output.

System Rules & Logic

  1. Staff Profiles:

    • Every staff member has a unique string name, a string level, and an integer rate (earnings in cents per minute).
    • Staff profiles can be updated or promoted over time.
    • Historical work shifts are always evaluated using the wage rate that was active when the shift started. Future changes to a staff member's wage rate do not modify the compensation of shifts logged prior to or during that rate change.
  2. Shift Tracking:

    • Shifts are logged using ENTER and LEAVE commands.
    • Time is represented in continuous non-negative integer minutes.
    • All shifts are strictly non-overlapping per worker and have positive durations.
    • Rate updates, additions, and promotions only occur when the worker is not currently clocked in.
  3. Bonus Hours:

    • Bonus multipliers cover half-open time intervals [start, end).
    • During a bonus interval, any worked minute yields double the base pay (i.e., base rate + 1x bonus rate = 2x base rate).
    • Multiple bonus intervals can overlap. However, overlapping bonus intervals do not stack beyond double pay—a minute covered by one or more bonus intervals is always paid at exactly twice the normal rate.
  4. Payroll Calculation (TOTAL):

    • For a given window [start, end), calculate the total earnings across all completed shifts for all workers.
    • Only the portion of any shift that overlaps with the query interval [start, end) is compensated.
    • Earnings for a clipped shift segment of duration $D$ worked at rate $R$ with $B$ bonus minutes within that segment is equal to $R \times (D + B)$.

Operations & Output Formats

  • ["ADD", name, level, rate]: Register a new worker. Returns ["OK"].
  • ["GET", name]: Retrieve worker details. Returns [name, level, rate].
  • ["UPDATE", name, level, rate] and ["PROMOTE", name, level, rate]: Update worker attributes. Returns ["OK"].
  • ["ENTER", name, time]: Clock in worker at time. Returns ["OK"].
  • ["LEAVE", name, time]: Clock out worker at time. Returns ["OK"].
  • ["FILTER", field, value, sortField]: Find all workers where field ("name", "level", or "rate") equals value. Return a list of matching worker names sorted by sortField ("name", "level", or "rate"). Ties in sortField must be broken by worker name in ascending lexicographical order. String comparison is used for "name" and "level", while numeric comparison is used for "rate". If no workers match, return an empty array [].
  • ["BONUS", start, end]: Add a double-pay time interval [start, end). Returns ["OK"].
  • ["TOTAL", start, end]: Compute total earnings for all workers in time range [start, end). Returns [totalCents], formatted as a decimal string with no leading zeros.

Constraints

  • Total operations $\le 2000$. Number of distinct workers $\le 200$.
  • Time ranges are non-negative integers up to $1{,}000{,}000$. Rates are non-negative integers up to $1{,}000{,}000$.
  • All inputs are well-formed and valid according to the operational rules.

Examples

Example 1

Input: worker_payroll([['ADD', 'Ada', 'L1', '10'], ['ENTER', 'Ada', '0'], ['LEAVE', 'Ada', '10'], ['BONUS', '3', '5'], ['TOTAL', '0', '10'], ['PROMOTE', 'Ada', 'L2', '20'], ['GET', 'Ada']])

Output: [['OK'], ['OK'], ['OK'], ['OK'], ['120'], ['OK'], ['Ada', 'L2', '20']]

Example 2

Input: worker_payroll([['ADD', 'A', 'L1', '5'], ['ADD', 'B', 'L2', '7'], ['FILTER', 'level', 'L1', 'name'], ['FILTER', 'rate', '7', 'rate'], ['GET', 'B']])

Output: [['OK'], ['OK'], ['A'], ['B'], ['B', 'L2', '7']]

Hints

Hint 1

Capture the worker's current pay rate at the exact moment of 'ENTER' so that subsequent rate changes do not corrupt past shift rates.

Hint 2

Bonus periods can overlap or be added out of order. Merge all recorded bonus intervals into disjoint blocks before processing a 'TOTAL' query.

Hint 3

For each completed shift, compute its intersection with the total query range [query_start, query_end). If they intersect, compute the intersection of that clipped shift with each merged bonus block to find total bonus minutes.