Fix the dropped frame model for a Flutter parse
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_onlyis the original.yield_betweenawaits between chunks so frames can render in the gaps.isolateruns the work withIsolate.runorcompute, 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_msis 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 byframe_budget_ms, rounded down.- Any other mode,
async_onlyincluded: the chunks form one block. Return their total cost divided byframe_budget_ms, rounded down.
Examples
count_dropped_frames(chunk_costs_ms: list[int], mode: str, frame_budget_ms: int) -> intExample 1
Input
count_dropped_frames([120, 90, 300, 90], "async_only", 16)Output
37Example 2
Input
count_dropped_frames([120, 90, 300, 90], "isolate", 16)Output
0Example 3
Input
count_dropped_frames([120, 90, 300, 90], "yield_between", 16)Output
35