Estimate how many users a bad version reaches

Agent buildMedium30 min

Your task

A canary puts a new version on a small slice of traffic. Before yours starts, you want to know how far a bad version could spread. If the new version is bad, each task it handles fails visibly with a chance you can estimate from the task's type. The canary controller rolls the version back as soon as it has seen k visible failures, and no later task reaches the version.

Write expected_reach(tasks, k) that returns how many different users a bad version reaches on average.

Input and output:

  • tasks: 1 to 3,000 tasks in arrival order, each [user, chance]. user is a non-empty string compared exactly. chance is a whole number from 0 to 100, the percent chance that this task fails visibly. Tasks fail independently of each other.
  • k: a whole number from 1 to 200.
  • Return a float. The tests accept any value within 0.000001 of the exact one.

Rules:

  • Tasks reach the version in order until the rollback. The task that causes the k-th failure did reach the version, so its user counts.
  • A user counts once, however many of their tasks reached the version. Each of those tasks can still fail.
  • If fewer than k failures ever happen, every task reaches the version.

Examples

expected_reach(tasks: list[list], k: int) -> float
Example 1
Inputexpected_reach([["ana", 50], ["bo", 50], ["cy", 50]], 1)
Output1.75
Example 2
Inputexpected_reach([["ana", 100], ["bo", 0], ["ana", 0]], 2)
Output2
Example 3
Inputexpected_reach([["ana", 20], ["ana", 20], ["bo", 20], ["cy", 20]], 2)
Output2.856