KatInterview

Filter Financial Logs by Active Premium Periods

Greedy & IntervalsAuto-graded · 13 test cases
EasyAuto-gradedGreedy & Intervalsintervalsdata-filteringhash-table

Last updated

Filter a chronological sequence of transaction records based on whether they fall within active premium membership intervals.

Given a list of transaction records transactions and a list of subscription records memberships, extract and return all transactions that occurred while the corresponding user held an active "golden" tier subscription.

Each record in transactions is a dictionary containing:

  • "trans_id": Unique integer identifier for the transaction.
  • "user_id": Integer identifier for the user.
  • "trans_date": Date string formatted as "YYYY-MM-DD".
  • "amount": Numeric transaction amount.

Each record in memberships is a dictionary containing:

  • "user_id": Integer identifier for the user.
  • "tier": Subscription level (e.g., "golden", "silver").
  • "start_date": Date string formatted as "YYYY-MM-DD" marking the start of the tier interval.
  • "end_date": Date string formatted as "YYYY-MM-DD" marking the end of the tier interval, or None if the subscription is still ongoing.

A transaction is considered to take place during a "golden" subscription period if:

  1. The user's tier is "golden".
  2. start_date <= trans_date.
  3. end_date is None or trans_date <= end_date.

Both start and end dates are inclusive boundaries.

Input Constraints

  • Each string date is formatted strictly as "YYYY-MM-DD", allowing standard string comparison operations to determine temporal ordering.
  • A user may have zero, one, or multiple membership records.
  • Output records must preserve their original relative order from transactions.
  • If a transaction matches multiple overlapping "golden" periods for the same user, it should only appear once in the output.

Constraints

  • $1 \le \text{len(transactions)} \le 5000$
  • $1 \le \text{len(memberships)} \le 5000$

Examples

Example 1

Input: transactions_during_golden_membership([{'trans_id': 1, 'user_id': 101, 'trans_date': '2023-01-05', 'amount': 200}, {'trans_id': 2, 'user_id': 102, 'trans_date': '2023-01-07', 'amount': 150.5}, {'trans_id': 3, 'user_id': 101, 'trans_date': '2023-02-03', 'amount': 75}, {'trans_id': 4, 'user_id': 103, 'trans_date': '2023-02-10', 'amount': 50}], [{'user_id': 101, 'tier': 'golden', 'start_date': '2022-12-15', 'end_date': None}, {'user_id': 102, 'tier': 'silver', 'start_date': '2023-01-01', 'end_date': '2023-02-01'}, {'user_id': 103, 'tier': 'golden', 'start_date': '2023-02-01', 'end_date': '2023-03-01'}, {'user_id': 104, 'tier': 'golden', 'start_date': '2023-01-20', 'end_date': None}])

Output: [{'trans_id': 1, 'user_id': 101, 'trans_date': '2023-01-05', 'amount': 200}, {'trans_id': 3, 'user_id': 101, 'trans_date': '2023-02-03', 'amount': 75}, {'trans_id': 4, 'user_id': 103, 'trans_date': '2023-02-10', 'amount': 50}]

Hints

Hint 1

Map each user_id to a list of valid (start_date, end_date) ranges where tier == 'golden'.

Hint 2

String comparisons using standard ordering operators (<=, >=) work directly on 'YYYY-MM-DD' formatted strings.

Hint 3

Ensure that if end_date is None, the transaction is valid as long as it occurs on or after start_date.