Consecutive Monthly Purchases

HARD

You are given a dataset (e.g., a Pandas DataFrame) representing customer transactions, with fields like customer_id, transaction_date, and transaction_amount. Write a Python function to identify customers who have made purchases in three consecutive months.

The function should return a list or set of customer_ids for these customers.

Example Input DataFrame (transactions_df):

# Assume transaction_date is already in datetime format or can be converted.
data = {
    'customer_id': ['C1', 'C1', 'C1', 'C1', 'C2', 'C2', 'C1', 'C3', 'C3', 'C3', 'C3'],
    'transaction_date': [
        '2023-01-10', '2023-01-20', '2023-02-05', '2023-03-15', # C1: Jan, Feb, Mar
        '2023-01-01', '2023-03-01',                         # C2: Jan, Mar (not consecutive)
        '2023-05-10',                                     # C1: May (breaks sequence)
        '2023-11-15', '2023-12-20', '2024-01-05', '2024-02-01' # C3: Nov, Dec, Jan (consecutive!) then Feb
    ],
    'transaction_amount': [100, 50, 75, 200, 30, 40, 60, 90, 10, 20, 50]
}
# transactions_df = pd.DataFrame(data)
# transactions_df['transaction_date'] = pd.to_datetime(transactions_df['transaction_date'])

Expected Output: {'C1', 'C3'} (or a list ['C1', 'C3'])

Constraints:

  • Input is a Pandas DataFrame.
  • transaction_date column can be parsed into datetime objects.

Function Signature (Python):

import pandas as pd
from typing import List, Set

class Solution:
    def find_consecutive_monthly_purchasers(self, transactions_df: pd.DataFrame) -> Set[str]:
        # Your code here
        pass

 

Nerchuko Academy · Free DS Interview Prep