You are given a list of log entries logs, where each entry is a list of three strings: [timestamp, ip, status]. The timestamp is an integer written as a string, ip is an IPv4 address, and status is either "FAIL" or "SUCCESS".
A failed login is a log entry with status "FAIL". An IP address is flagged if there is any rolling window of y seconds (inclusive at both ends) during which that IP has strictly more than x failed login attempts.
Return a list of all flagged IP addresses, sorted in lexicographic order. Each IP appears at most once in the result.
Function Signature
def flagged_ips(logs: list[list[str]], x: int, y: int) -> list[str]:
...
Rules
-
A window is a closed interval
[t, t + y]
. Two failures from the same IP can be counted in one window exactly when their timestamps differ by at most
y
. With
y = 5
, failures at seconds 1 and 6 share a window; failures at seconds 1 and 7 do not.
-
Only
"FAIL"
entries count.
"SUCCESS"
entries are ignored and do not reset anything.
-
Several entries may share a timestamp, and each one counts separately.
-
logs
is not necessarily sorted by timestamp.
-
Lexicographic order is plain string comparison of the IP text, so
"10.0.0.1"
comes before
"9.0.0.1"
.
-
Return an empty list when no IP is flagged.
Constraints
-
1 <= len(logs) <= 10^5
-
0 <= timestamp <= 10^9
-
Every
ip
is a valid IPv4 string
-
status
is either
"FAIL"
or
"SUCCESS"
-
1 <= x <= len(logs)
-
1 <= y <= 10^9
Examples
Input: logs = [["1","192.168.1.1","FAIL"], ["2","192.168.1.1","FAIL"], ["3","192.168.1.1","FAIL"], ["4","192.168.1.2","FAIL"], ["10","192.168.1.2","FAIL"], ["100","192.168.1.2","FAIL"], ["5","192.168.1.1","SUCCESS"]], x = 2, y = 5
Output: ["192.168.1.1"]
192.168.1.1 fails at seconds 1, 2 and 3, all inside the window [1, 6], and 3 failures is more than x = 2. 192.168.1.2 fails at seconds 4, 10 and 100, and any two of those are more than 5 seconds apart, so no window holds more than one of them.
Input: logs = [["1","10.0.0.1","FAIL"], ["2","10.0.0.1","FAIL"], ["3","10.0.0.2","FAIL"], ["4","10.0.0.2","FAIL"], ["5","10.0.0.2","FAIL"], ["6","10.0.0.2","FAIL"]], x = 3, y = 10
Output: ["10.0.0.2"]
10.0.0.1 has only 2 failures in total, which does not exceed 3. 10.0.0.2 has 4 failures at seconds 3 through 6, which fit inside one 10-second window.
Input: logs = [["0","9.9.9.9","FAIL"], ["5","9.9.9.9","FAIL"], ["7","10.1.1.1","FAIL"], ["7","10.1.1.1","FAIL"], ["20","172.16.0.1","FAIL"], ["26","172.16.0.1","FAIL"]], x = 1, y = 5
Output: ["10.1.1.1", "9.9.9.9"]
9.9.9.9 fails at seconds 0 and 5, exactly 5 apart, so both fit in [0, 5], and 10.1.1.1 fails twice at second 7, while the failures of 172.16.0.1 are 6 seconds apart. String order puts "10.1.1.1" before "9.9.9.9".