Coinbase's compliance monitoring needs to detect suspicious transaction patterns in real-time using a sliding-window approach.
A transaction is "anomalous" if, in a trailing 24-hour window, the number of transfers for a wallet exceeds 3x its baseline average (computed from the 7 days prior).
You are given a stream of wallet events as a list of tuples:
events: List[Tuple[int, str]] # (unix_timestamp, wallet_id)
Implement:
def find_anomalous_wallets(events: List[Tuple[int, str]], baseline_days: int = 7, window_hours: int = 24, spike_multiplier: float = 3.0) -> List[Tuple[str, int]]:
"""
Returns a list of (wallet_id, anomaly_timestamp) for every wallet that
spiked above spike_multiplier * its baseline average
at any hourly checkpoint within the trailing window_hours.
Text
baseline_days: use this many days of prior history to compute baseline
window_hours: detect anomalies within this trailing window
spike_multiplier: threshold for anomaly (e.g., 3.0x baseline)
Events are NOT guaranteed to be sorted by timestamp.
A wallet can appear multiple times if it spikes more than once.
"""
Constraints:
- Events list can contain up to 5 million entries
- Timestamps span up to 60 days
- Optimize for O(n log n) or better
- Use only standard library (collections, heapq, bisect allowed)