Stripe computes fraud signals in real-time for every merchant. You are given a transaction
stream and must compute multiple fraud-related metrics for each merchant within a 90-day window.
You are given:
transactions: List[Dict] with keys:
- merchant_id: str
- payment_method_id: str (unique card/ACH fingerprint)
- amount: float (USD)
- country: str (2-letter country code)
- created_at: str (ISO 8601 timestamp, e.g. '2026-08-10T10:00:00')
- has_chargeback: bool (True if this charge was disputed/chargedback)
Implement:
def compute_merchant_fraud_signals(
transactions: List[Dict],
observation_window_days: int = 90
) -> Dict[str, Dict[str, float]]:
"""
For each merchant active in the last observation_window_days, compute fraud signals:
{
merchant_id: {
"transaction_count": int,
"volume_usd": float,
"avg_transaction_amount": float,
"repeat_card_ratio": float, # (unique_cards / total) capped at 1.0
"cross_country_ratio": float, # (unique_countries / total)
"chargeback_rate": float, # chargebacks / total, 0.0-1.0
}
}
Text
Rules:
- Treat the LATEST transaction timestamp in the stream as "now"; the observation
window is the observation_window_days ending at that timestamp
- "Active" = at least 1 transaction inside that window
- If a merchant has no transactions in window, exclude them
- All rates are 0.0 to 1.0
- Round floats to 4 decimal places
"""
Constraints:
- Up to 10 million transactions
- Up to 1 million unique merchants
- Optimize for single-pass computation