Fix the interval model for a countdown effect
Your task
An AI wrote a React Native countdown with useEffect(() => { setInterval(...) }): no dependency array and no cleanup. After a few visits to the screen, the timer ticked several times a second and React warned about updating an unmounted component. Your team models how intervals pile up for each version of the effect.
An assistant wrote simulate_interval_leak(event_lines, has_dependency_array, has_cleanup), the model. It is in the editor. It has a bug. Find it and fix it.
Input and output:
event_lines: the component's lifecycle, in order.renderis a re-render with the same props.prop_changeis a re-render where theendsAtprop changed.tickis one interval period passing.unmountremoves the component.has_dependency_array: the effect has[endsAt]as its dependency array.has_cleanup: the effect returns a function that clears its interval.- Return a list of two numbers: the intervals still running, then the state updates made after unmount.
Rules the tests check:
- Ignore any other event. After
unmount, also ignorerender,prop_changeand any furtherunmount. - On a
renderorprop_change, the effect runs if there is no dependency array, or the event is aprop_change, or the effect has never run. - When the effect runs: if there is a cleanup and at least one interval is running, one interval stops. Then one new interval starts.
- On
unmountwith a cleanup, every interval stops. - On a
tickafter unmount, every running interval makes one state update. Atickwhile mounted adds nothing.
Examples
simulate_interval_leak(event_lines: list[str], has_dependency_array: bool, has_cleanup: bool) -> list[int]Example 1
Input
simulate_interval_leak(["render", "tick", "render", "tick", "render", "tick", "unmount", "tick"], False, False)Output
[3, 3]Example 2
Input
simulate_interval_leak(["render", "tick", "render", "tick", "render", "tick", "unmount", "tick"], True, True)Output
[0, 0]