Code Room
CodingMediumcod-g1330
Subject Log processingLevel Entry–Mid~13 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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.

Implement
first_alert_index(flags: list[int], window: int, k: int) → int
Examples
in[[0,1,0,1,1,0],3,2]out3
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.