← 返回 ramp 的题目列表Detect Recurring Transactions
类型:online_judge
Problem: Detect Recurring Transactions
Given a transaction file, identify transactions that likely represent recurring charges or subscriptions.
Each transaction contains:
transaction_id, merchant, date, amount_cents
where:
transaction_id is a unique string;
merchant is a merchant identifier;
date is in YYYY-MM-DD format;
amount_cents is a positive integer in cents.
A set of transactions for the same merchant is considered recurring if it contains at least 3 transactions, with approximately stable gaps between consecutive transactions and approximately stable amounts.
Tunable parameters
The function receives:
interval_tolerance_days: maximum allowed deviation of a consecutive date gap from the median interval;
amount_tolerance_cents: maximum allowed deviation of a transaction amount from the group's median amount.
Output
Return every detected recurring transaction group. Each group contains:
merchant, estimated_interval_days, median_amount_cents, transaction_ids
where:
estimated_interval_days is the median consecutive date gap, rounded to an integer;
median_amount_cents is the median transaction amount, rounded to an integer;
transaction_ids are ordered by date.
Sort groups lexicographically by merchant.
Example
Input:
8
1,Netflix,2024-01-05,1599
2,Netflix,2024-02-05,1599
3,Netflix,2024-03-06,1699
4,Gym,2024-01-01,5000
5,Gym,2024-02-01,5000
6,Gym,2024-03-01,5000
7,Coffee,2024-01-01,500
8,Coffee,2024-01-03,500
With interval_tolerance_days = 3 and amount_tolerance_cents = 150, output:
Gym,30,5000,4|5|6
Netflix,30,1599,1|2|3
Coffee is not recurring because it has only two transactions.
Example
Input
8 3 150
1,Netflix,2024-01-05,1599
2,Netflix,2024-02-05,1599
3,Netflix,2024-03-06,1699
4,Gym,2024-01-01,5000
5,Gym,2024-02-01,5000
6,Gym,2024-03-01,5000
7,Coffee,2024-01-01,500
8,Coffee,2024-01-03,500
Output
Gym,30,5000,4|5|6
Netflix,30,1599,1|2|3