Service failure streak detection

Write codeMedium13 min

Your task

A health checker appends one line per probe: "<timestamp> <service> <status>", where status is "ok" or "fail". An incident page should fire when any single service fails n times in a row, counting only that service's own probes, in log order; probes of other services in between do not reset the streak. Given the log lines and n (n >= 1), return true if some service reaches n consecutive "fail" probes, and false otherwise.

Examples

has_consecutive_failures(lines: list[str], n: int) -> bool
Example 1
Inputhas_consecutive_failures(["t1 db fail", "t2 db fail", "t3 api ok", "t4 db fail"], 3)
OutputTrue