KatInterview

Worker Pay Rate Ledger with Interval Cost Queries

Data Structure DesignAuto-graded · 21 test cases
MediumAuto-gradedData Structure Designdesignintervalssimulation

Last updated

Tests piecewise-constant function integration, lazy evaluation, and interval clipping inside a simulated in-memory billing system.

Problem Statement

You are building an in-memory earnings tracker for a gig-worker platform. Each worker has an hourly pay rate (in cents) that can be updated at any point in time. Jobs are logged as half-open time intervals [start_time, end_time) measured in seconds. Your system must answer cost and payout queries that respect the exact rate in effect during each second of a job.

Implement a function that processes a sequence of operations given as two parallel lists — operations (strings) and arguments (lists of values) — and returns a list of results, one per operation.

The four supported operations are:


set_rate

Arguments: [worker_id: str, timestamp: int, cents_per_hour: int]

Records that worker_id earns cents_per_hour cents per hour starting at timestamp (inclusive). This rate stays in effect until a later rate is registered for the same worker. Multiple calls for the same worker and the same timestamp are allowed; the last call wins. Rate updates may arrive in any order.

Returns: None


record_delivery

Arguments: [job_id: str, worker_id: str, start_time: int, end_time: int]

Registers a job completed by worker_id over the interval [start_time, end_time), where start_time < end_time. Each job_id is unique across all calls.

Returns: None


get_delivery_cost

Arguments: [job_id: str]

Returns the total cost in cents for the given job. If the job spans multiple rate periods, compute the cost of each segment separately as duration_seconds * rate / 3600 (unrounded), sum all segments, then round the total using math.floor(total + 0.5).

Returns: int


get_courier_payout

Arguments: [worker_id: str, start_time: int, end_time: int]

Returns the total earnings in cents for worker_id across all jobs that overlap with the query window [start_time, end_time). For a job that only partially falls inside the window, count only the overlapping portion. A job whose endpoint exactly equals the window's start (or whose start equals the window's end) does not overlap.

Sum all unrounded segment costs across all overlapping (clipped) jobs, then apply a single math.floor(total + 0.5) to produce the final result.

Returns: int


Constraints

  • All timestamps and time values are non-negative integers: 0 ≤ timestamp, start_time, end_time ≤ 10^9
  • For every logged job, start_time < end_time
  • Every job's start_time is greater than or equal to the earliest set_rate timestamp recorded for that worker (so there is always a rate in effect at the job's start)
  • At most 10^4 total calls across all operations
  • Each job_id is unique
  • Jobs belonging to the same worker do not overlap each other
  • cents_per_hour ≥ 0

Examples

Example 1

Input: worker_pay_ledger(['set_rate', 'set_rate', 'record_delivery', 'get_delivery_cost', 'get_courier_payout'], [['C1', 0, 1800], ['C1', 3600, 2400], ['D1', 'C1', 1800, 5400], ['D1'], ['C1', 3600, 7200]])

Output: [None, None, None, 2100, 1200]

Hints

Hint 1

Store each worker's rate history as a dict mapping timestamp to rate. Sorting the timestamps at query time handles out-of-order arrivals without needing to keep them sorted on insert.

Hint 2

Don't compute a job's cost inside record_delivery. A set_rate call arriving later can still affect a rate that falls inside an already-logged job, so evaluate lazily at query time.

Hint 3

For get_courier_payout, clip each job to the query window first (overlap_start = max(window_start, job_start), overlap_end = min(window_end, job_end)), then price the clipped interval with the same segment-walking routine. Round the grand total once at the end, not per job.