Fix the hotspot ranking from a profile

Review the AI's codeEasy12 min

Your task

An AI asked to speed up a C++ particle loop swapped a vector for a linked list, then added a data race, and each change made things worse. It was guessing. Now the agent gets a profile first and may only touch the regions that account for most of the time, and the list of those regions must be the same on every run.

An assistant wrote rank_profile_hotspots(regions, self_time_pct, budget_pct). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • regions: the profiled code regions, by name.
  • self_time_pct: each region's share of the total time, in percent, at the same position.
  • budget_pct: the share of time the chosen regions must cover.
  • Return the chosen region names, in the order below.

Rules the tests check:

  • If the lists differ in length, use only the positions both have.
  • Order the regions by self time, highest first. Break ties by name, compared as plain strings, then by position in the input.
  • Walk that order. Before adding each region, stop if the regions already chosen cover the budget: their total self time plus 0.000000001 is at least budget_pct. Otherwise add it.
  • So a budget of 0 returns [], and a budget above the total returns every region.

Examples

rank_profile_hotspots(regions: list[str], self_time_pct: list[float], budget_pct: float) -> list[str]
Example 1
Inputrank_profile_hotspots(["gravity_pairwise", "integrate", "render"], [80, 12, 8], 75)
Output["gravity_pairwise"]
Example 2
Inputrank_profile_hotspots(["gravity_pairwise", "integrate", "render"], [80, 12, 8], 90)
Output["gravity_pairwise", "integrate"]
Example 3
Inputrank_profile_hotspots(["gravity_pairwise", "integrate", "render"], [80, 12, 8], 0)
Output[]