KatInterview

Daily Leading Buyers Among Active Subscribers

SQL & DataAuto-graded · 15 test cases
MediumAuto-gradedSQL & Dataaggregationgroup-byties

Last updated

Filter orders by inclusive membership windows, total quantities per day and customer, and return every customer tied for each day's maximum, in sorted order.

Daily Leading Buyers Among Active Subscribers

A shop runs a subscription program. You are given two tables, each as a list of dictionaries whose values are all strings.

Subscriptions (subscriptions): each row has these keys:

  • Customer_ID
  • Start_Date
  • End_Date

A row means the customer was subscribed from Start_Date through End_Date. Both endpoints are included. A customer may have several rows, and those rows may overlap or repeat.

Purchases (purchases): each row has these keys:

  • Customer_ID
  • Order_ID
  • Order_Day
  • Product_ID
  • Quantity_Sold (an integer written as a string)

For every calendar day, find the customer or customers who bought the most units that day, counting only purchases made while the customer held an active subscription.

Rules

  1. A purchase counts only if at least one of that customer's subscription rows satisfies Start_Date <= Order_Day <= End_Date. A purchase counts once, even if several subscription rows cover its day.
  2. Purchases from customers with no subscription row, or placed outside every one of their windows, are ignored.
  3. For each (Order_Day, Customer_ID) pair, add up the Quantity_Sold of all counted purchases.
  4. For each day, keep every customer whose total equals that day's maximum. Ties produce several rows.
  5. A day with no counted purchases produces no output.

Output

Return a list of dictionaries, each with these keys:

  • Order_Day (string)
  • Customer_ID (string)
  • Total_Quantity_Sold (int)

Order the rows by Order_Day ascending, then by Customer_ID ascending. Compare Customer_ID values as strings, so "10" comes before "2". Dates are in YYYY-MM-DD format, so string comparison matches chronological order.

If there are no qualifying rows, return an empty list.

Constraints

  • Customer_ID values and Quantity_Sold values arrive as strings. Convert quantities to integers and treat IDs as strings.
  • Dates are ISO YYYY-MM-DD strings.
  • Either input list may be empty.
  • A customer may have zero, one, or many subscription rows, and those rows may overlap.
  • A customer may place several purchases on the same day.

Examples

Example 1

Input: daily_top_subscriber_buyers([{'Customer_ID': '1', 'Start_Date': '2025-01-01', 'End_Date': '2025-12-31'}, {'Customer_ID': '2', 'Start_Date': '2025-03-15', 'End_Date': '2025-10-15'}], [{'Customer_ID': '1', 'Order_ID': '1001', 'Order_Day': '2025-04-01', 'Product_ID': 'P123', 'Quantity_Sold': '3'}, {'Customer_ID': '1', 'Order_ID': '1002', 'Order_Day': '2025-04-01', 'Product_ID': 'P456', 'Quantity_Sold': '2'}, {'Customer_ID': '2', 'Order_ID': '1003', 'Order_Day': '2025-04-01', 'Product_ID': 'P123', 'Quantity_Sold': '6'}])

Output: [{'Order_Day': '2025-04-01', 'Customer_ID': '2', 'Total_Quantity_Sold': 6}]

Example 2

Input: daily_top_subscriber_buyers([{'Customer_ID': '1', 'Start_Date': '2025-01-01', 'End_Date': '2025-12-31'}, {'Customer_ID': '2', 'Start_Date': '2025-01-01', 'End_Date': '2025-12-31'}], [{'Customer_ID': '1', 'Order_ID': '1', 'Order_Day': '2025-05-01', 'Product_ID': 'A', 'Quantity_Sold': '4'}, {'Customer_ID': '2', 'Order_ID': '2', 'Order_Day': '2025-05-01', 'Product_ID': 'B', 'Quantity_Sold': '4'}])

Output: [{'Order_Day': '2025-05-01', 'Customer_ID': '1', 'Total_Quantity_Sold': 4}, {'Order_Day': '2025-05-01', 'Customer_ID': '2', 'Total_Quantity_Sold': 4}]

Hints

Hint 1

Build a map from customer to the list of their (start, end) subscription windows, then check each purchase against that list.

Hint 2

Use an any(...) style check rather than joining, so overlapping or duplicate subscription rows never count the same purchase twice.

Hint 3

Accumulate totals keyed by (day, customer), group them by day, take the maximum per day, and emit every customer matching it in sorted ID order.