Fix the interval model for a countdown effect

Review the AI's codeEasy10 minMeta style

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. render is a re-render with the same props. prop_change is a re-render where the endsAt prop changed. tick is one interval period passing. unmount removes 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 ignore render, prop_change and any further unmount.
  • On a render or prop_change, the effect runs if there is no dependency array, or the event is a prop_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 unmount with a cleanup, every interval stops.
  • On a tick after unmount, every running interval makes one state update. A tick while mounted adds nothing.

Examples

simulate_interval_leak(event_lines: list[str], has_dependency_array: bool, has_cleanup: bool) -> list[int]
Example 1
Inputsimulate_interval_leak(["render", "tick", "render", "tick", "render", "tick", "unmount", "tick"], False, False)
Output[3, 3]
Example 2
Inputsimulate_interval_leak(["render", "tick", "render", "tick", "render", "tick", "unmount", "tick"], True, True)
Output[0, 0]