Report a canary's worst sustained error rate

Agent buildMedium30 min

Your task

A canary, the new version on a small slice of traffic, has an error rate that jumps around from hour to hour, and one bad hour with few tasks tells you little. So the rollback review looks at the canary's worst sustained rate. That is the highest overall error rate across any stretch of at least a set number of hours.

Write worst_stretch(errors, tasks, min_hours) that returns that rate.

Input and output:

  • errors and tasks: two lists of the same length, 1 to 50,000 hours in order. tasks[i] is a whole number from 1 to 1,000 and errors[i] a whole number from 0 to tasks[i].
  • min_hours: the shortest stretch that counts, a whole number from 1 to the number of hours.
  • Return a float. The tests accept any value within 0.000001 of the exact one.

Rules:

  • A stretch is one or more hours in a row, at least min_hours long, and it may be longer.
  • A stretch's rate is its total errors divided by its total tasks. It is not the average of its hourly rates.
  • Return the highest rate of any stretch.

Examples

worst_stretch(errors: list[int], tasks: list[int], min_hours: int) -> float
Example 1
Inputworst_stretch([1, 9, 0, 0], [100, 10, 100, 100], 1)
Output0.9
Example 2
Inputworst_stretch([1, 9, 0, 0], [100, 10, 100, 100], 2)
Output0.09090909090909091
Example 3
Inputworst_stretch([1, 1, 1], [2, 2, 2], 3)
Output0.5