Find recurring transaction groups by merchant, amount tolerance and 30-day spacing

Quick Overview

Group a list of transactions into recurring series: same merchant, amounts within 1.0 of the previous charge, and charges 28 to 32 days apart. It tests grouping and sorting, calendar day arithmetic across month ends and leap years, and following a precise greedy rule with inclusive bounds and a fixed output order.

Find recurring transaction groups by merchant, amount tolerance and 30-day spacing

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of `transactions`, where each transaction is `[id, amount, date, merchant]`. Identify the groups of recurring transactions. A recurring group is a set of transactions that share the same `merchant`, have amounts within a tolerance of `1.0`, and occur at roughly regular intervals of 30 days, with +/- 2 days allowed between consecutive transactions in the group (so 28 to 32 days apart). Return a list of lists, where each inner list holds the transaction IDs of one recurring group, sorted by date. Only include groups with 2 or more transactions. Sort the outer list by the first transaction ID in each group. ### Function Signature ```python def find_recurring_transactions(transactions: list[list]) -> list[list[str]]: ... ``` ### Rules - `id` is a string, `amount` is an int or a float, `date` is a `"YYYY-MM-DD"` string and `merchant` is a string. The input can be in any order. - Day gaps are calendar days, so month lengths and leap years count. Groups are built one merchant at a time: 1. Order the merchant's transactions by date. Transactions on the same date keep their input order. 2. The earliest transaction that is not yet in a group starts a new group. 3. Go through the transactions after it in date order. Add one to the group when it is not in a group yet, its date is 28 to 32 days (inclusive) after the group's most recently added transaction, and its amount differs from that transaction's amount by at most `1.0` (inclusive). A transaction that fails the test is skipped and the pass continues to the end. 4. Repeat steps 2 and 3 until every transaction of the merchant belongs to a group, possibly a group of one. After grouping: - Each transaction is in at most one group, and groups of one transaction are dropped. - Inside a group the IDs are in the order they were added, which is date order. - The outer list is sorted by each group's first ID with plain string comparison, so `"t10"` comes before `"t2"`. - Return an empty list when there is no recurring group. ### Constraints - `1 <= transactions.length <= 1000` - Each transaction is `[id, amount, date, merchant]` - `id` is a unique string - `amount` is a number (integer or float) - `date` is in `'YYYY-MM-DD'` format - `merchant` is a non-empty string - Amount tolerance: `1.0` - Date interval: `30` days with +/- `2` day tolerance (`28`-`32` days between consecutive transactions) ### Examples ```text Input: transactions = [ ["t1", 50, "2024-01-01", "Netflix"], ["t2", 50, "2024-01-31", "Netflix"], ["t3", 50, "2024-03-01", "Netflix"], ["t4", 25, "2024-01-15", "Spotify"], ["t5", 25.5, "2024-02-14", "Spotify"], ["t6", 100, "2024-01-10", "Amazon"] ] Output: [["t1", "t2", "t3"], ["t4", "t5"]] ``` The Netflix charges are 30 days apart each time (2024 is a leap year, so January 31 to March 1 is 30 days) with equal amounts, and the two Spotify charges are 30 days apart with amounts 0.5 apart. The single Amazon charge cannot form a group, and the outer list puts "t1" before "t4". ```text Input: transactions = [ ["a1", 10, "2024-03-01", "Gym"], ["a2", 10.5, "2024-03-31", "Gym"], ["a3", 10.25, "2024-04-30", "Gym"], ["a4", 10, "2024-05-30", "Gym"] ] Output: [["a1", "a2", "a3", "a4"]] ``` Each step is 30 days (March 1, March 31, April 30, May 30), and consecutive amounts differ by 0.5, 0.25 and 0.25. ```text Input: transactions = [ ["p3", 15, "2024-03-02", "Phone"], ["p1", 15, "2024-01-01", "Phone"], ["p2", 15, "2024-01-20", "Phone"], ["p4", 15, "2024-01-31", "Phone"], ["w1", 9.99, "2024-02-01", "Water"], ["w2", 12, "2024-03-02", "Water"] ] Output: [["p1", "p4", "p3"]] ``` In date order the Phone charges are p1 (January 1), p2 (January 20), p4 (January 31) and p3 (March 2): p2 is only 19 days after p1 and is skipped, p4 joins 30 days after p1, p3 joins 31 days after p4, and p2 is left in a group of one. The Water charges are 30 days apart, but their amounts differ by 2.01.

Overview: Group a list of transactions into recurring series: same merchant, amounts within 1.0 of the previous charge, and charges 28 to 32 days apart. It tests grouping and sorting, calendar day arithmetic across month ends and leap years, and following a precise greedy rule with inclusive bounds and a fixed output order.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of transactions, where each transaction is [id, amount, date, merchant]. Identify the groups of recurring transactions.

A recurring group is a set of transactions that share the same merchant, have amounts within a tolerance of 1.0, and occur at roughly regular intervals of 30 days, with +/- 2 days allowed between consecutive transactions in the group (so 28 to 32 days apart).

Return a list of lists, where each inner list holds the transaction IDs of one recurring group, sorted by date. Only include groups with 2 or more transactions. Sort the outer list by the first transaction ID in each group.

Function Signature

def find_recurring_transactions(transactions: list[list]) -> list[list[str]]:
    ...

Rules

  • id is a string, amount is an int or a float, date is a "YYYY-MM-DD" string and merchant is a string. The input can be in any order.
  • Day gaps are calendar days, so month lengths and leap years count.

Groups are built one merchant at a time:

  1. Order the merchant's transactions by date. Transactions on the same date keep their input order.
  2. The earliest transaction that is not yet in a group starts a new group.
  3. Go through the transactions after it in date order. Add one to the group when it is not in a group yet, its date is 28 to 32 days (inclusive) after the group's most recently added transaction, and its amount differs from that transaction's amount by at most 1.0 (inclusive). A transaction that fails the test is skipped and the pass continues to the end.
  4. Repeat steps 2 and 3 until every transaction of the merchant belongs to a group, possibly a group of one.

After grouping:

  • Each transaction is in at most one group, and groups of one transaction are dropped.
  • Inside a group the IDs are in the order they were added, which is date order.
  • The outer list is sorted by each group's first ID with plain string comparison, so "t10" comes before "t2" .
  • Return an empty list when there is no recurring group.

Constraints

  • 1 <= transactions.length <= 1000
  • Each transaction is [id, amount, date, merchant]
  • id is a unique string
  • amount is a number (integer or float)
  • date is in 'YYYY-MM-DD' format
  • merchant is a non-empty string
  • Amount tolerance: 1.0
  • Date interval: 30 days with +/- 2 day tolerance ( 28 - 32 days between consecutive transactions)

Examples

Input: transactions = [
  ["t1", 50, "2024-01-01", "Netflix"],
  ["t2", 50, "2024-01-31", "Netflix"],
  ["t3", 50, "2024-03-01", "Netflix"],
  ["t4", 25, "2024-01-15", "Spotify"],
  ["t5", 25.5, "2024-02-14", "Spotify"],
  ["t6", 100, "2024-01-10", "Amazon"]
]
Output: [["t1", "t2", "t3"], ["t4", "t5"]]

The Netflix charges are 30 days apart each time (2024 is a leap year, so January 31 to March 1 is 30 days) with equal amounts, and the two Spotify charges are 30 days apart with amounts 0.5 apart. The single Amazon charge cannot form a group, and the outer list puts "t1" before "t4".

Input: transactions = [
  ["a1", 10, "2024-03-01", "Gym"],
  ["a2", 10.5, "2024-03-31", "Gym"],
  ["a3", 10.25, "2024-04-30", "Gym"],
  ["a4", 10, "2024-05-30", "Gym"]
]
Output: [["a1", "a2", "a3", "a4"]]

Each step is 30 days (March 1, March 31, April 30, May 30), and consecutive amounts differ by 0.5, 0.25 and 0.25.

Input: transactions = [
  ["p3", 15, "2024-03-02", "Phone"],
  ["p1", 15, "2024-01-01", "Phone"],
  ["p2", 15, "2024-01-20", "Phone"],
  ["p4", 15, "2024-01-31", "Phone"],
  ["w1", 9.99, "2024-02-01", "Water"],
  ["w2", 12, "2024-03-02", "Water"]
]
Output: [["p1", "p4", "p3"]]

In date order the Phone charges are p1 (January 1), p2 (January 20), p4 (January 31) and p3 (March 2): p2 is only 19 days after p1 and is skipped, p4 joins 30 days after p1, p3 joins 31 days after p4, and p2 is left in a group of one. The Water charges are 30 days apart, but their amounts differ by 2.01.