You receive a stream of transactions. Each transaction is [user_id, amount, timestamp], and the list is sorted by timestamp in non-decreasing order.
A rolling window of size window at time t covers every transaction with a timestamp in [t - window + 1, t]. Detect the first moment each user's total amount inside a rolling window exceeds that user's threshold, and return a list of [user_id, timestamp] pairs in the order the users are detected.
A user's threshold depends on their tier. tier_thresholds maps tier names to custom thresholds, and user_tiers maps user ids to tier names. A user with no tier, or whose tier is missing from tier_thresholds, uses the default threshold.
Function Signature
def detect_fraud(
transactions: list[list[int]],
window: int,
threshold: int,
tier_thresholds: dict[str, int],
user_tiers: dict[int, str],
) -> list[list[int]]:
Rules
-
A window total sums only that user's transactions.
-
"Exceeds" means strictly greater than the threshold. A total equal to the threshold is not flagged.
-
Both ends of the window are inclusive: a transaction at time
t - window + 1
still counts at time
t
.
-
A user is reported at most once, with the timestamp of the transaction that first pushes their window total over the threshold. That user's later transactions are ignored.
-
The output follows the order of the triggering transactions in
transactions
. When two users are flagged at the same timestamp, the one whose triggering transaction comes first in the list comes first.
-
Keys of
user_tiers
are integer user ids.
-
Return an empty list when no user is flagged.
Constraints
-
1 <= len(transactions) <= 10^5
-
1 <= user_id <= 10^4
-
1 <= amount <= 10^6
-
1 <= timestamp <= 10^9
-
Transactions are sorted by
timestamp
in non-decreasing order.
-
window >= 1
-
1 <= threshold <= 10^9
-
tier_thresholds
and
user_tiers
may be empty.
Examples
Input: transactions = [[1,500,1],[2,300,2],[1,600,3],[2,800,4],[1,200,5]], window = 3, threshold = 1000, tier_thresholds = {}, user_tiers = {}
Output: [[1,3],[2,4]]
User 1's window at time 3 covers timestamps 1 to 3, so the total is 500 + 600 = 1100, which is over 1000. User 2's window at time 4 covers timestamps 2 to 4 and totals 300 + 800 = 1100. User 1's transaction at time 5 is ignored because user 1 is already flagged.
Input: transactions = [[1,400,1],[1,400,2],[1,400,5],[2,900,6]], window = 2, threshold = 700, tier_thresholds = {"gold": 1500}, user_tiers = {1: "gold"}
Output: [[2,6]]
User 1 is in tier "gold" with threshold 1500, and their window totals are 400, 800 and 400 (at time 5 the window covers only timestamps 4 and 5). User 2 has no tier, so the default 700 applies, and 900 exceeds it at time 6.
Input: transactions = [[7,60,10],[3,150,12],[7,50,12]], window = 3, threshold = 100, tier_thresholds = {}, user_tiers = {}
Output: [[3,12],[7,12]]
At time 12 the window covers timestamps 10 to 12, so user 7's total is 60 + 50 = 110, counting the transaction at the window's lower edge. Both users are flagged at time 12, and user 3 comes first because its triggering transaction appears earlier in the list.