learn deep. ship safe.Practice testsFollow ByteMunSubscribe

easySOCDetection

Failed Login IP Detector

Scan login events and flag the IP addresses with too many failed attempts, the first rule every SOC writes to catch brute-force attacks.

JavaScript or Python

A SOC wants a simple brute-force detection. You get a list of login events, each a pair [ip, success] where success is true or false, and a threshold. Return the IP addresses with at least threshold failed logins, sorted in ascending string order. Successful logins don't count toward the total.

Return an empty list when no IP reaches the threshold.

Examples

Example 1

Input: events = [["10.0.0.5", false], ["10.0.0.5", false], ["10.0.0.9", true]], threshold = 2
Output: ["10.0.0.5"]
Why: Two failures from 10.0.0.5; 10.0.0.9 succeeded

Example 2

Input: events = [["1.1.1.1", false]], threshold = 3
Output: []
Why: One failure is below the threshold

Constraints

  • 0 <= events.length <= 10^5
  • 1 <= threshold <= 1000
  • IP addresses are IPv4 strings

Hints

Hint 1

Count failures per IP in a dictionary or map.

Hint 2

Filter by the threshold, then sort the IPs as strings.

Solution

Show the solution (try it yourself first)

One pass counts failures per IP, then you keep the IPs at or above the threshold and sort them so the output is stable for analysts and tests.

def suspicious_ips(events, threshold):
    fails = {}
    for ip, ok in events:
        if not ok:
            fails[ip] = fails.get(ip, 0) + 1
    return sorted(ip for ip, n in fails.items() if n >= threshold)

Complexity

Time O(n + k log k) for n events and k distinct failing IPs, space O(k). Real detections add a time window (for example 10 failures in 5 minutes), look at failures per account as well as per IP to catch password spraying from many addresses, and check whether a success follows the failures, which turns noise into an incident. That follow-up is what interviewers usually ask next.

Found a mistake in this problem, its tests or the solution?

Written by Published Tests checked by Deepak
Case 1Input: [["10.0.0.5",false],["10.0.0.5",false],["10.0.0.9",true]], 2Expected: ["10.0.0.5"]
Case 2Input: [["1.1.1.1",false]], 3Expected: []

Run checks these 2 samples. Submit checks all 6 tests, including hidden ones.

  1. Pair with Target SumArrays · Hash map
    easy
  2. Balanced BracketsStack · Strings
    easy
  3. Palindrome SentenceStrings · Two pointers
    easy
  4. Search a Sorted ArrayBinary search · Arrays
    easy