Error rate alert trigger

Write codeMedium13 min

Your task

A gateway tags each request with an error flag, giving you a list of 0s and 1s in arrival order (1 = failed request). The alerting rule watches the last window requests: it trips at the first position where the most recent window flags contain at least k ones. Return the smallest index i (0-based, counting from the window-th request, so i >= window - 1) where flags[i-window+1..i] holds k or more ones; return -1 if the rule never trips (including when fewer than window requests exist). Assume window >= 1 and k >= 1.

Examples

first_alert_index(flags: list[int], window: int, k: int) -> int
Example 1
Inputfirst_alert_index([0, 1, 0, 1, 1, 0], 3, 2)
Output3