KatInterview

Shift Pay Totals Across Time-of-Day Wage Bands

Greedy & IntervalsAuto-graded · 18 test cases
EasyAuto-gradedGreedy & Intervalsintervalsoverlapsimulation

Last updated

Sum payouts for work sessions by intersecting each session with non-overlapping hourly wage bands, using interval overlap arithmetic.

Shift Pay Totals Across Time-of-Day Wage Bands

A gig worker is paid an hourly wage that changes through the day. The day is described by a list of wage bands, each given as [band_start, band_end, hourly_wage]. During the time range from band_start up to band_end, every hour worked pays hourly_wage.

The worker also completes several jobs, each given as [job_start, job_end]. A job is paid for every moment it overlaps a wage band, at that band's hourly wage. Any part of a job that lies in no wage band earns nothing.

Return the total pay over all jobs.

Rules

  • Treat every interval as half-open, [start, end). If a job ends exactly where a band begins (or vice versa), they share zero hours.
  • Jobs are independent. If two jobs overlap in time, each is still paid in full for its own duration. Do not merge them.
  • Wage bands never overlap each other, but they may appear in any order and may leave gaps between them.
  • If either list is empty, the total is 0.
  • Return the total as a number (a float or an integer value equal to the total is fine, e.g. 150 or 150.0).

Input

  • wage_bands: a list of [band_start, band_end, hourly_wage] triples of integers.
  • jobs: a list of [job_start, job_end] pairs of integers.

Output

The total pay across all jobs.

Constraints

  • 0 <= start < end <= 24 for every band and every job.
  • Wage bands do not overlap one another.
  • A job may lie inside a single band, span several bands, or fall completely outside all bands.
  • hourly_wage is a non-negative integer.

Examples

Example 1

Input: total_job_pay([[0, 10, 10], [10, 20, 20]], [[5, 15]])

Output: 150

Example 2

Input: total_job_pay([[0, 5, 10], [5, 10, 15]], [[0, 2], [8, 9]])

Output: 35

Hints

Hint 1

For one job and one wage band, how many hours do they have in common? Think in terms of the later of the two starts and the earlier of the two ends.

Hint 2

If the computed shared length is negative, the intervals do not touch at all, so clamp it to zero before multiplying by the wage.

Hint 3

Because bands never overlap, you can simply add up (shared hours * wage) over every job and band pair, with no sorting or merging needed.