Fix the dropped frame model for a Flutter parse

Review the AI's codeEasy10 minGoogle style

Your task

An AI wrote a Flutter function that decodes a large analytics payload and transforms each event, and said it "runs in the background with async". The UI still froze for about 600 ms. In Dart, async does not move work to another thread: synchronous code inside it still blocks the UI isolate, the single thread that draws the frames. Your team models how many frames each version drops.

An assistant wrote count_dropped_frames(chunk_costs_ms, mode, frame_budget_ms), the model. It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • chunk_costs_ms: the CPU time of each chunk of work, in order, in ms.
  • mode: how the work runs. async_only is the original. yield_between awaits between chunks so frames can render in the gaps. isolate runs the work with Isolate.run or compute, off the UI isolate.
  • frame_budget_ms: the time for one frame.
  • Return the number of dropped frames.

Rules the tests check:

  • If frame_budget_ms is 0 or less, return 0.
  • Treat a negative chunk cost as 0.
  • isolate: return 0. The UI isolate never blocks.
  • yield_between: each chunk is its own block. Return the sum, over the chunks, of the chunk's cost divided by frame_budget_ms, rounded down.
  • Any other mode, async_only included: the chunks form one block. Return their total cost divided by frame_budget_ms, rounded down.

Examples

count_dropped_frames(chunk_costs_ms: list[int], mode: str, frame_budget_ms: int) -> int
Example 1
Inputcount_dropped_frames([120, 90, 300, 90], "async_only", 16)
Output37
Example 2
Inputcount_dropped_frames([120, 90, 300, 90], "isolate", 16)
Output0
Example 3
Inputcount_dropped_frames([120, 90, 300, 90], "yield_between", 16)
Output35