Skip to content
RED SAGSkills

Card Velocity and Geo Fraud Rules

A payments team runs two simple fraud rules over transactions, which are sorted by ts (seconds) ascending. Each is { id, card, ts, amountCents, country }.

**Velocity rule:** a transaction is flagged if the same card has more than maxPerWindow transactions with a timestamp in the window (ts - windowSec, ts], counting the transaction itself.

**Geo rule:** a transaction is flagged if the previous transaction on the same card was in a different country and happened less than 3600 seconds earlier.

Return the ids of all flagged transactions in input order (a transaction flagged by both rules appears once).

Example 1

Input: transactions = [{"id":"t1","card":"A","ts":0,"amountCents":100,"country":"IN"},{"id":"t2","card":"A","ts":10,"amountCents":100,"country":"IN"},{"id":"t3","card":"A","ts":20,"amountCents":100,"country":"IN"},{"id":"t4","card":"A","ts":30,"amountCents":100,"country":"IN"},{"id":"t5","card":"B","ts":40,"amountCents":900,"country":"IN"},{"id":"t6","card":"B","ts":100,"amountCents":900,"country":"AE"}], maxPerWindow = 3, windowSec = 60
Output: ["t4","t6"]
Explanation: t4 is the 4th card-A transaction within 60 s; t6 jumps from IN to AE within an hour.

Example 2

Input: transactions = [{"id":"a","card":"C","ts":0,"amountCents":5,"country":"US"},{"id":"b","card":"C","ts":60,"amountCents":5,"country":"US"},{"id":"c","card":"C","ts":61,"amountCents":5,"country":"US"}], maxPerWindow = 1, windowSec = 60
Output: ["c"]

Constraints

  • 0 <= transactions.length <= 10^5
  • 1 <= maxPerWindow <= 100
  • 1 <= windowSec <= 86400
  • ts values are non-decreasing
Hint 1

Keep, per card, a queue of recent timestamps and drop the ones that fall out of the window.

Hint 2

Also keep the last transaction per card for the geo rule.

Good practice for: Backend Developer, Full Stack Developer, Blockchain Developer, Data Engineer, Automation Test Engineer, Data Scientist, Machine Learning Engineer

Run checks your code on 3 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.