KatInterview

Freelance Crew Pay Tracker With Rolling Settlements

Data Structure DesignAuto-graded · 15 test cases
MediumAuto-gradedData Structure Designdesignsimulationhash-map

Last updated

Tests the ability to design an in-memory ledger class driven by an operation list, maintaining running cost totals and an idempotent pay-cutoff pointer.

Freelance Crew Pay Tracker With Rolling Settlements

You are building a payroll tool for a production company that hires freelance crew members on short-term work blocks. Each crew member earns a fixed hourly rate, and the company needs to track what has been earned, what has already been paid out, and what is still owed — all in memory.

Implement a function that accepts a sequence of named operations and their arguments, executes them in order against a single in-memory tracker, and returns a result list of the same length. Operations that produce no meaningful value append None to the result list.

Operations

add_worker — [worker_id, hourly_rate_usd]
Registers a crew member with the given string ID and their USD hourly rate (a number). Appends None.

log_call — [worker_id, start_time, end_time]
Records one completed work block for the given worker. start_time and end_time are integer timestamps in seconds. The cost of the block is hourly_rate * (end_time - start_time) / 3600. Appends None.

total_cost — []
Returns the total labor cost accrued across all workers and all logged work blocks, regardless of whether any of it has been paid. Appends that number.

pay_through — [cutoff_time]
Marks all work performed up to cutoff_time as settled. Calling this again with the same or any earlier timestamp must be a no-op — no amount already paid out can be counted as unpaid again. Appends None.

unpaid_balance — []
Returns the total wages still owed after all settlements applied so far. Appends that number.

How unpaid work is calculated after a settlement

After pay_through(cutoff) is called, the unpaid portion of a logged block [start, end) is the cost of the interval [max(start, cutoff), end). Any seconds at or before the cutoff are considered paid.

If a work block is logged after a pay_through call and its start time falls before the current cutoff, only the portion from the cutoff onward is treated as unpaid (the pre-cutoff portion still counts toward total_cost).

Constraints

  • All timestamps are non-negative integers (seconds).
  • end_time >= start_time for every log_call.
  • hourly_rate_usd is a positive number (float or int).
  • worker_id is a non-empty string; a worker is registered before any of their calls are logged.
  • There is at most one global settlement pointer shared across all workers (not per-worker).
  • pay_through is never called with a time earlier than the first such call; if it is, treat it as a no-op.
  • total_cost and unpaid_balance may be called any number of times and always reflect the current state.
  • 1 ≤ total number of operations ≤ 10,000.
  • 0 ≤ timestamps ≤ 10^9.
  • 0 < hourly_rate_usd ≤ 10,000.

Examples

Example 1

Input: crew_pay_tracker(['add_worker', 'log_call', 'total_cost', 'pay_through', 'unpaid_balance', 'pay_through', 'unpaid_balance'], [['crew_7', 42], ['crew_7', 0, 7200], [], [3600], [], [3600], []])

Output: [None, None, 84, None, 42, None, 42]

Hints

Hint 1

Keep two running numeric totals — one for all accrued cost and one for unpaid cost — so that `total_cost` and `unpaid_balance` are O(1) lookups rather than re-scanning every logged block each time they are called.

Hint 2

When `pay_through(cutoff)` is called with a strictly later cutoff than before, you need to recompute the unpaid balance. Walk every logged block and for each one count only the seconds that fall after the new cutoff: `max(0, end - max(start, cutoff))`.

Hint 3

When a new work block is logged, the unpaid portion starts not at `start_time` but at `max(start_time, current_cutoff)` — the pre-cutoff seconds belong to total cost but not to unpaid balance.