Detect fraud events and extract PII
AffirmSoftware Engineer · Technical ScreenLast updated
This problem tests your ability to process nested JSON-like structures, perform deduplication, and map related entities to detect fraudulent patterns.
Problem Statement
You are provided with a list of transaction events, where each event is a dictionary containing a unique event_id, a user_id, and a list of metadata objects. Each metadata object may contain sensitive fields like email, phone, or ip_address.
Your task is to identify potentially fraudulent transactions by finding "connected" events. Two events are connected if they share at least one PII field (e.g., the same email, same phone, or same IP).
Write a function detect_fraud_clusters(events) that:
- Extracts all unique PII values from each event.
- Groups all
event_ids into clusters where each event in a cluster is connected to at least one other event in the same cluster via a shared PII value. - Returns a list of lists, where each inner list contains the
event_ids belonging to a single connected cluster.
Input
events: A list of dictionaries. Each dictionary has:
event_id(string)metadata(list of dictionaries, e.g.,{'type': 'email', 'value': '[email protected]'})
Output
List[List[str]]: A list of lists containing grouped event_ids.
Example
events = [
{"event_id": "A", "metadata": [{"type": "email", "value": "[email protected]"}]},
{"event_id": "B", "metadata": [{"type": "email", "value": "[email protected]"}, {"type": "ip", "value": "1.1.1.1"}]},
{"event_id": "C", "metadata": [{"type": "ip", "value": "1.1.1.1"}]},
{"event_id": "D", "metadata": [{"type": "email", "value": "[email protected]"}]}
]
# Output: [["A", "B", "C"], ["D"]]
Constraints
- Number of events: up to 10,000.
- Number of metadata entries per event: up to 50.
- Time complexity should be better than O(N^2) where N is the number of events.
Hints
Hint 1
Consider this as a graph problem where events are nodes and shared PII values are edges between events.
Hint 2
You can build an adjacency list by creating a mapping of each PII value to the list of `event_id`s that contain it.
Hint 3
Use Disjoint Set Union (DSU) or Breadth-First Search (BFS)/Depth-First Search (DFS) to find connected components in your graph.
Follow-up questions
- How would you optimize the solution if the input stream of events is infinite?
- What if the PII fields are not exact matches (e.g., slight variations in phone numbers)?
- How would you adapt this to handle a scenario where an event is only considered fraudulent if it shares a PII with at least three other distinct events?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.