Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using core interview patterns strategy.
Table: customer_transactions
+------------------+---------+ | Column Name | Type | +------------------+---------+ | transaction_id | int | | customer_id | int | | transaction_date | date | | amount | decimal | | transaction_type | varchar | +------------------+---------+ transaction_id is the unique identifier for this table. transaction_type can be either 'purchase' or 'refund'.
Write a solution to find loyal customers. A customer is considered loyal if they meet ALL the following criteria:
3 purchase transactions.30 days.20% .Refund rate is the proportion of transactions that are refunds, calculated as the number of refund transactions divided by the total number of transactions (purchases plus refunds).
Return the result table ordered by customer_id in ascending order.
The result format is in the following example.
Example:
Input:
customer_transactions table:
+----------------+-------------+------------------+--------+------------------+ | transaction_id | customer_id | transaction_date | amount | transaction_type | +----------------+-------------+------------------+--------+------------------+ | 1 | 101 | 2024-01-05 | 150.00 | purchase | | 2 | 101 | 2024-01-15 | 200.00 | purchase | | 3 | 101 | 2024-02-10 | 180.00 | purchase | | 4 | 101 | 2024-02-20 | 250.00 | purchase | | 5 | 102 | 2024-01-10 | 100.00 | purchase | | 6 | 102 | 2024-01-12 | 120.00 | purchase | | 7 | 102 | 2024-01-15 | 80.00 | refund | | 8 | 102 | 2024-01-18 | 90.00 | refund | | 9 | 102 | 2024-02-15 | 130.00 | purchase | | 10 | 103 | 2024-01-01 | 500.00 | purchase | | 11 | 103 | 2024-01-02 | 450.00 | purchase | | 12 | 103 | 2024-01-03 | 400.00 | purchase | | 13 | 104 | 2024-01-01 | 200.00 | purchase | | 14 | 104 | 2024-02-01 | 250.00 | purchase | | 15 | 104 | 2024-02-15 | 300.00 | purchase | | 16 | 104 | 2024-03-01 | 350.00 | purchase | | 17 | 104 | 2024-03-10 | 280.00 | purchase | | 18 | 104 | 2024-03-15 | 100.00 | refund | +----------------+-------------+------------------+--------+------------------+
Output:
+-------------+ | customer_id | +-------------+ | 101 | | 104 | +-------------+
Explanation:
The result table is ordered by customer_id in ascending order.
Problem summary: Table: customer_transactions +------------------+---------+ | Column Name | Type | +------------------+---------+ | transaction_id | int | | customer_id | int | | transaction_date | date | | amount | decimal | | transaction_type | varchar | +------------------+---------+ transaction_id is the unique identifier for this table. transaction_type can be either 'purchase' or 'refund'. Write a solution to find loyal customers. A customer is considered loyal if they meet ALL the following criteria: Made at least 3 purchase transactions. Have been active for at least 30 days. Their refund rate is less than 20% . Refund rate is the proportion of transactions that are refunds, calculated as the number of refund transactions divided by the total number of transactions (purchases plus refunds). Return the result table ordered by customer_id in ascending order. The result format is in the following
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
{"headers":{"customer_transactions":["transaction_id","customer_id","transaction_date","amount","transaction_type"]},"rows":{"customer_transactions":[[1,101,"2024-01-05",150.00,"purchase"],[2,101,"2024-01-15",200.00,"purchase"],[3,101,"2024-02-10",180.00,"purchase"],[4,101,"2024-02-20",250.00,"purchase"],[5,102,"2024-01-10",100.00,"purchase"],[6,102,"2024-01-12",120.00,"purchase"],[7,102,"2024-01-15",80.00,"refund"],[8,102,"2024-01-18",90.00,"refund"],[9,102,"2024-02-15",130.00,"purchase"],[10,103,"2024-01-01",500.00,"purchase"],[11,103,"2024-01-02",450.00,"purchase"],[12,103,"2024-01-03",400.00,"purchase"],[13,104,"2024-01-01",200.00,"purchase"],[14,104,"2024-02-01",250.00,"purchase"],[15,104,"2024-02-15",300.00,"purchase"],[16,104,"2024-03-01",350.00,"purchase"],[17,104,"2024-03-10",280.00,"purchase"],[18,104,"2024-03-15",100.00,"refund"]]}}Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3657: Find Loyal Customers
// Auto-generated Java example from py.
class Solution {
public void exampleSolution() {
}
}
// Reference (py):
// # Accepted solution for LeetCode #3657: Find Loyal Customers
// import pandas as pd
//
//
// def find_loyal_customers(customer_transactions: pd.DataFrame) -> pd.DataFrame:
// customer_transactions["transaction_date"] = pd.to_datetime(
// customer_transactions["transaction_date"]
// )
// grouped = customer_transactions.groupby("customer_id")
// agg_df = grouped.agg(
// total_transactions=("transaction_type", "size"),
// refund_count=("transaction_type", lambda x: (x == "refund").sum()),
// min_date=("transaction_date", "min"),
// max_date=("transaction_date", "max"),
// ).reset_index()
// agg_df["date_diff"] = (agg_df["max_date"] - agg_df["min_date"]).dt.days
// agg_df["refund_ratio"] = agg_df["refund_count"] / agg_df["total_transactions"]
// result = (
// agg_df[
// (agg_df["total_transactions"] >= 3)
// & (agg_df["refund_ratio"] < 0.2)
// & (agg_df["date_diff"] >= 30)
// ][["customer_id"]]
// .sort_values("customer_id")
// .reset_index(drop=True)
// )
// return result
// Accepted solution for LeetCode #3657: Find Loyal Customers
// Auto-generated Go example from py.
func exampleSolution() {
}
// Reference (py):
// # Accepted solution for LeetCode #3657: Find Loyal Customers
// import pandas as pd
//
//
// def find_loyal_customers(customer_transactions: pd.DataFrame) -> pd.DataFrame:
// customer_transactions["transaction_date"] = pd.to_datetime(
// customer_transactions["transaction_date"]
// )
// grouped = customer_transactions.groupby("customer_id")
// agg_df = grouped.agg(
// total_transactions=("transaction_type", "size"),
// refund_count=("transaction_type", lambda x: (x == "refund").sum()),
// min_date=("transaction_date", "min"),
// max_date=("transaction_date", "max"),
// ).reset_index()
// agg_df["date_diff"] = (agg_df["max_date"] - agg_df["min_date"]).dt.days
// agg_df["refund_ratio"] = agg_df["refund_count"] / agg_df["total_transactions"]
// result = (
// agg_df[
// (agg_df["total_transactions"] >= 3)
// & (agg_df["refund_ratio"] < 0.2)
// & (agg_df["date_diff"] >= 30)
// ][["customer_id"]]
// .sort_values("customer_id")
// .reset_index(drop=True)
// )
// return result
# Accepted solution for LeetCode #3657: Find Loyal Customers
import pandas as pd
def find_loyal_customers(customer_transactions: pd.DataFrame) -> pd.DataFrame:
customer_transactions["transaction_date"] = pd.to_datetime(
customer_transactions["transaction_date"]
)
grouped = customer_transactions.groupby("customer_id")
agg_df = grouped.agg(
total_transactions=("transaction_type", "size"),
refund_count=("transaction_type", lambda x: (x == "refund").sum()),
min_date=("transaction_date", "min"),
max_date=("transaction_date", "max"),
).reset_index()
agg_df["date_diff"] = (agg_df["max_date"] - agg_df["min_date"]).dt.days
agg_df["refund_ratio"] = agg_df["refund_count"] / agg_df["total_transactions"]
result = (
agg_df[
(agg_df["total_transactions"] >= 3)
& (agg_df["refund_ratio"] < 0.2)
& (agg_df["date_diff"] >= 30)
][["customer_id"]]
.sort_values("customer_id")
.reset_index(drop=True)
)
return result
// Accepted solution for LeetCode #3657: Find Loyal Customers
// Rust example auto-generated from py reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (py):
// # Accepted solution for LeetCode #3657: Find Loyal Customers
// import pandas as pd
//
//
// def find_loyal_customers(customer_transactions: pd.DataFrame) -> pd.DataFrame:
// customer_transactions["transaction_date"] = pd.to_datetime(
// customer_transactions["transaction_date"]
// )
// grouped = customer_transactions.groupby("customer_id")
// agg_df = grouped.agg(
// total_transactions=("transaction_type", "size"),
// refund_count=("transaction_type", lambda x: (x == "refund").sum()),
// min_date=("transaction_date", "min"),
// max_date=("transaction_date", "max"),
// ).reset_index()
// agg_df["date_diff"] = (agg_df["max_date"] - agg_df["min_date"]).dt.days
// agg_df["refund_ratio"] = agg_df["refund_count"] / agg_df["total_transactions"]
// result = (
// agg_df[
// (agg_df["total_transactions"] >= 3)
// & (agg_df["refund_ratio"] < 0.2)
// & (agg_df["date_diff"] >= 30)
// ][["customer_id"]]
// .sort_values("customer_id")
// .reset_index(drop=True)
// )
// return result
// Accepted solution for LeetCode #3657: Find Loyal Customers
// Auto-generated TypeScript example from py.
function exampleSolution(): void {
}
// Reference (py):
// # Accepted solution for LeetCode #3657: Find Loyal Customers
// import pandas as pd
//
//
// def find_loyal_customers(customer_transactions: pd.DataFrame) -> pd.DataFrame:
// customer_transactions["transaction_date"] = pd.to_datetime(
// customer_transactions["transaction_date"]
// )
// grouped = customer_transactions.groupby("customer_id")
// agg_df = grouped.agg(
// total_transactions=("transaction_type", "size"),
// refund_count=("transaction_type", lambda x: (x == "refund").sum()),
// min_date=("transaction_date", "min"),
// max_date=("transaction_date", "max"),
// ).reset_index()
// agg_df["date_diff"] = (agg_df["max_date"] - agg_df["min_date"]).dt.days
// agg_df["refund_ratio"] = agg_df["refund_count"] / agg_df["total_transactions"]
// result = (
// agg_df[
// (agg_df["total_transactions"] >= 3)
// & (agg_df["refund_ratio"] < 0.2)
// & (agg_df["date_diff"] >= 30)
// ][["customer_id"]]
// .sort_values("customer_id")
// .reset_index(drop=True)
// )
// return result
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.