Work out when each ramp stage meets its quota

Agent buildMedium25 min

Your task

Your new agent version ramps through stages, and each stage takes a larger share of traffic. A stage ends only once it has seen enough tasks, so a quiet night can hold a stage up for hours. You want to know when each stage will end, so the on-call team can plan around it.

Write stage_ends(traffic, shares, quota) that returns the hour each stage ends.

Input and output:

  • traffic: 1 to 1,000 whole numbers from 0 to 1,000,000, at least one above 0. traffic[i] is the tasks arriving in hour i of the pattern, and the pattern repeats forever. Hour 0 of the plan is hour 0 of the pattern.
  • shares: 1 to 20 whole numbers from 1 to 100, the percent of traffic each stage takes, in order.
  • quota: the tasks each stage must see, a whole number from 1 to 1,000,000,000.
  • Return a list with one whole number per stage: the hour the stage ends, counted from hour 0 of the plan.

Rules:

  • The first stage starts at hour 0. Each later stage starts at the hour the one before it ends.
  • A stage at share s sees traffic[i] * s / 100 tasks in hour i. This may be a fraction. Keep it exact and never round it.
  • A stage ends at the end of the first hour in which its total reaches quota. The value to return is the number of hours from the start of the plan to that moment.
  • Nothing carries over between stages.

Examples

stage_ends(traffic: list[int], shares: list[int], quota: int) -> list[int]
Example 1
Inputstage_ends([10, 0, 20], [50, 100], 15)
Output[3, 6]
Example 2
Inputstage_ends([4], [25], 3)
Output[3]
Example 3
Inputstage_ends([100, 0], [1, 10, 100], 2)
Output[3, 5, 7]