#2752
Hard Database Customers with maximum number of transactions on consecutive days
Database
42.4% acceptance
Mar 31, 2026
16
35
No description available.
Solution
Pandas
Time O(1)
Space O(1)
# Table: Transactions
#
# +------------------+------+
# | Column Name | Type |
# +------------------+------+
# | transaction_id | int |
# | customer_id | int |
# | transaction_date | date |
# | amount | int |
# +------------------+------+
# transaction_id is the column with unique values of this table.
# Each row contains information about transactions that includes unique (customer_id, transaction_date) along with the corresponding customer_id and amount.
#
# Write a solution to find all customer_id who made the maximum number of transactions on consecutive days.
#
# Return all customer_id with the maximum number of consecutive transactions. Order the result table by customer_id in ascending order.
#
# The result format is in the following example.
#
# Example 1:
# Input:
# Transactions table:
# +----------------+-------------+------------------+--------+
# | transaction_id | customer_id | transaction_date | amount |
# +----------------+-------------+------------------+--------+
# | 1 | 101 | 2023-05-01 | 100 |
# | 2 | 101 | 2023-05-02 | 150 |
# | 3 | 101 | 2023-05-03 | 200 |
# | 4 | 102 | 2023-05-01 | 50 |
# | 5 | 102 | 2023-05-03 | 100 |
# | 6 | 102 | 2023-05-04 | 200 |
# | 7 | 105 | 2023-05-01 | 100 |
# | 8 | 105 | 2023-05-02 | 150 |
# | 9 | 105 | 2023-05-03 | 200 |
# +----------------+-------------+------------------+--------+
# Output:
# +-------------+
# | customer_id |
# +-------------+
# | 101 |
# | 105 |
# +-------------+
# Explanation:
# - customer_id 101 has a total of 3 transactions, and all of them are consecutive.
# - customer_id 102 has a total of 3 transactions, but only 2 of them are consecutive.
# - customer_id 105 has a total of 3 transactions, and all of them are consecutive.
# In total, the highest number of consecutive transactions is 3, achieved by customer_id 101 and 105. The customer_id are sorted in ascending order.
import pandas as pd
def find_customers(transactions: pd.DataFrame) -> pd.DataFrame:
transactions['transaction_date'] = pd.to_datetime(transactions['transaction_date'])
transactions = transactions.sort_values(['customer_id', 'transaction_date'])
# Assign group: for each customer, consecutive dates belong to same group
transactions['day_rank'] = transactions.groupby('customer_id')['transaction_date'].rank(method='dense')
transactions['group'] = transactions['transaction_date'] - pd.to_timedelta(transactions['day_rank'], unit='D')
# Count consecutive days per group
consecutive = transactions.groupby(['customer_id', 'group']).size().reset_index(name='consecutive_days')
# Find overall max consecutive days
max_consecutive = consecutive['consecutive_days'].max()
# Return one row per qualifying group (one customer_id per group that hits the max)
result = consecutive[consecutive['consecutive_days'] == max_consecutive][['customer_id']]
return result.sort_values('customer_id').reset_index(drop=True)