KatInterview

Calculate Worker Pay with Bonus Time Periods

Greedy & IntervalsAuto-graded · 20 test cases
MediumAuto-gradedGreedy & Intervalsintervalsarraysmath

Last updated

Tests interval merging, overlap calculation, and time-based pay aggregation with capped rate multipliers.

You are implementing payroll calculations for a gig work platform.

Given a list of work records for one worker, calculate the total amount the worker should be paid.

Each work record is a dictionary containing:

  • order_id: a unique string identifier
  • status: either "delivered" or "canceled"
  • start_time: work start time, an integer representing minutes since midnight
  • end_time: work end time, an integer representing minutes since midnight
  • hourly_rate: the worker's base hourly rate for that work (dollars per hour)

You are also given:

  • canceled_pay: a fixed dollar amount paid for each canceled work order
  • bonus_periods: a list of time intervals [start, end) during which delivered work earns bonus pay at double the normal rate

Payment Rules:

  1. A delivered order pays the worker based on work duration and the hourly rate.
  2. Any portion of a delivered order that falls within a bonus period should be paid at double the hourly rate.
  3. Any portion that does not overlap a bonus period should be paid at the normal hourly rate.
  4. If multiple bonus periods overlap each other, the worker still receives at most double pay for any given minute (not triple or higher).
  5. A canceled order pays only the fixed canceled_pay amount; do not apply hourly calculations or bonus pay.
  6. All time intervals use half-open semantics: [start, end) includes start but excludes end.

Return the worker's total pay as a float.

Constraints:

  • 0 ≤ number of orders ≤ 1000
  • 0 ≤ number of bonus periods ≤ 500
  • 0 ≤ start_time < end_time ≤ 1440 for valid intervals
  • 0 ≤ hourly_rate ≤ 200
  • 0 ≤ canceled_pay ≤ 100
  • Bonus periods may overlap, be nested, or be unsorted
  • Work intervals may have start_time ≥ end_time (treat as zero duration)

Examples

Example 1

Input: calculate_worker_pay([{'order_id': 'A', 'status': 'delivered', 'start_time': 60, 'end_time': 120, 'hourly_rate': 30}, {'order_id': 'B', 'status': 'canceled', 'start_time': 130, 'end_time': 160, 'hourly_rate': 30}, {'order_id': 'C', 'status': 'delivered', 'start_time': 150, 'end_time': 210, 'hourly_rate': 24}], 5, [[90, 150], [180, 240]])

Output: 86

Hints

Hint 1

Overlapping bonus periods must not stack beyond 2x pay. How can you consolidate them into non-overlapping intervals first?

Hint 2

Double pay means the worker earns the base rate plus one additional copy of the base rate for bonus minutes. Can you express total pay without splitting the work into separate segments?

Hint 3

Use the standard half-open interval overlap formula: max(0, min(end1, end2) - max(start1, start2))