Fix the wake lock cost estimate for a sync

Review the AI's codeEasy10 minGoogle style

Your task

An AI wrote an Android background sync that takes a wake lock with no timeout, never releases it, and pulls data every minute in a while (true) loop. A wake lock keeps the CPU awake while it is held, and users reported the app drained their battery overnight. Your team estimates how long the CPU stays awake for each version of the sync.

An assistant wrote estimate_cpu_awake_seconds(window_minutes, interval_minutes, sync_seconds, lock_timeout_seconds), the estimate. It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • window_minutes: the period to estimate, such as a night.
  • interval_minutes: the time between syncs.
  • sync_seconds: how long one sync's work takes.
  • lock_timeout_seconds: the wake lock's timeout. 0 or less means no timeout and no release, as in the original.
  • Return the seconds the CPU is kept awake, as an integer.

Rules the tests check:

  • If window_minutes is 0 or less, return 0.
  • If lock_timeout_seconds is 0 or less, the lock is never released: return the whole window in seconds.
  • If interval_minutes is 0 or less, the loop never sleeps: return the whole window in seconds.
  • Otherwise there is one sync per whole interval in the window: window_minutes divided by interval_minutes, rounded down.
  • Each sync holds the lock for sync_seconds or lock_timeout_seconds, whichever is smaller, and never less than 0. The timeout releases the lock even if the work has not finished.
  • The total is the number of syncs times that hold time, but never more than the whole window in seconds.

Examples

estimate_cpu_awake_seconds(window_minutes: int, interval_minutes: int, sync_seconds: int, lock_timeout_seconds: int) -> int
Example 1
Inputestimate_cpu_awake_seconds(480, 1, 5, 0)
Output28800
Example 2
Inputestimate_cpu_awake_seconds(480, 1, 5, 30)
Output2400
Example 3
Inputestimate_cpu_awake_seconds(480, 15, 5, 30)
Output160