KatInterview

Top Two Spending Categories per Shopper with Share of Spend

SQL & DataAuto-graded · 16 test cases
MediumAuto-gradedSQL & Dataaggregationgroupingranking

Last updated

Aggregate line-item spend by shopper and category, rank with ties, keep the top two ranks, add spend share, and handle shoppers with no purchases.

Top Categories per Shopper

You are given four tables as lists of dictionaries: shoppers, purchases (orders), line items, and a product catalog. Build a report with each shopper's two highest-spend product categories, including ties, plus a list of shoppers whose spending is spread out.

Input tables

Every table is a list of dictionaries with these keys:

  • shoppers: id, name
  • purchases: id, customer_id (a shopper id), order_date (an ISO YYYY-MM-DD string)
  • line_items: id, order_id (a purchase id), product_id, qty, unit_price
  • catalog: id (a product id), category, name

Aggregation

A line item counts only if its order_id matches a purchase and its product_id matches a catalog product. Skip every other line item. Each counted line item belongs to the purchase's customer_id and to the product's category.

For each (customer_id, category) pair, compute:

  • total_spend: the sum of qty * unit_price over its counted line items.
  • earliest_order_date: the smallest order_date among those line items. Compare the ISO strings directly.

Ranking

Within each customer, order the categories by total_spend descending, then earliest_order_date ascending. Assign ranks like SQL RANK():

  • Categories with identical (total_spend, earliest_order_date) share a rank.
  • The next distinct category gets rank = its 1-based position in the sorted order, so ranks can skip numbers after a tie.

Keep only categories with rank <= 2. Every category tied at rank 2 is kept, so a customer can have more than two rows. If several categories tie at rank 1, the next rank is 3, so only the tied rank-1 categories remain.

Share of spend

For each kept row, percent_of_total is total_spend divided by the customer's total spend over all their categories, not only the kept ones. If the customer's overall total is not greater than 0, percent_of_total is 0.

Shoppers without purchases

Every shopper in shoppers who has no counted line items (no purchases at all, or only purchases whose items were skipped) gets exactly one row:

category = None, total_spend = 0, earliest_order_date = None, rank_in_customer = None, percent_of_total = 0.

A customer id that appears in purchases but not in shoppers is still reported normally if it has counted line items.

Output

Return a dictionary with two keys:

  1. "rows": a list of dictionaries with keys customer_id, category, total_spend, earliest_order_date, rank_in_customer, percent_of_total. Order the rows as follows:
    • Customers with counted line items come first, in order of the first appearance of any of their counted line items in line_items.
    • Within a customer, rows go in ranked order (spend descending, then date ascending). Rows that tie completely stay in order of the first appearance of their (customer, category) pair in line_items.
    • The no-purchase shoppers follow, in the order they appear in shoppers.
  2. "customer_ids_with_top_category_below_half": a sorted ascending list of customer ids whose rank-1 row has percent_of_total < 0.5. Shoppers with no counted purchases have no rank-1 row and are never included.

Constraints

  • All four tables may be empty.
  • Ids are integers. qty is a non-negative integer. unit_price is a non-negative number.
  • Dates are valid ISO strings, so lexicographic order equals chronological order.
  • Table sizes are small enough for a single pass in memory, up to about 10^5 line items.

Examples

Example 1

Input: top_categories_by_shopper([{'id': 1, 'name': 'Alice'}, {'id': 2, 'name': 'Bob'}, {'id': 3, 'name': 'Cara'}, {'id': 4, 'name': 'Dana'}], [{'id': 101, 'customer_id': 1, 'order_date': '2025-07-02'}, {'id': 102, 'customer_id': 1, 'order_date': '2025-07-15'}, {'id': 103, 'customer_id': 2, 'order_date': '2025-07-20'}, {'id': 104, 'customer_id': 2, 'order_date': '2025-07-21'}, {'id': 105, 'customer_id': 3, 'order_date': '2025-07-22'}], [{'id': 1001, 'order_id': 101, 'product_id': 201, 'qty': 2, 'unit_price': 10}, {'id': 1002, 'order_id': 101, 'product_id': 202, 'qty': 1, 'unit_price': 20}, {'id': 1003, 'order_id': 102, 'product_id': 201, 'qty': 1, 'unit_price': 10}, {'id': 1004, 'order_id': 102, 'product_id': 203, 'qty': 5, 'unit_price': 5}, {'id': 1005, 'order_id': 103, 'product_id': 202, 'qty': 2, 'unit_price': 20}, {'id': 1006, 'order_id': 104, 'product_id': 203, 'qty': 10, 'unit_price': 5}, {'id': 1007, 'order_id': 105, 'product_id': 204, 'qty': 1, 'unit_price': 100}], [{'id': 201, 'category': 'CatA', 'name': 'A'}, {'id': 202, 'category': 'CatA', 'name': 'B'}, {'id': 203, 'category': 'CatB', 'name': 'C'}, {'id': 204, 'category': 'CatC', 'name': 'D'}])

Output: {'rows': [{'customer_id': 1, 'category': 'CatA', 'total_spend': 50, 'earliest_order_date': '2025-07-02', 'rank_in_customer': 1, 'percent_of_total': 0.6666666666666666}, {'customer_id': 1, 'category': 'CatB', 'total_spend': 25, 'earliest_order_date': '2025-07-15', 'rank_in_customer': 2, 'percent_of_total': 0.3333333333333333}, {'customer_id': 2, 'category': 'CatB', 'total_spend': 50, 'earliest_order_date': '2025-07-21', 'rank_in_customer': 1, 'percent_of_total': 0.5555555555555556}, {'customer_id': 2, 'category': 'CatA', 'total_spend': 40, 'earliest_order_date': '2025-07-20', 'rank_in_customer': 2, 'percent_of_total': 0.4444444444444444}, {'customer_id': 3, 'category': 'CatC', 'total_spend': 100, 'earliest_order_date': '2025-07-22', 'rank_in_customer': 1, 'percent_of_total': 1}, {'customer_id': 4, 'category': None, 'total_spend': 0, 'earliest_order_date': None, 'rank_in_customer': None, 'percent_of_total': 0}], 'customer_ids_with_top_category_below_half': []}

Hints

Hint 1

Build two dictionaries first: product id to category, and purchase id to (customer_id, order_date). Skip line items that fail either lookup.

Hint 2

Accumulate the sum and the minimum date per (customer_id, category) in one pass. Also keep a per-customer grand total over all categories, since the share must not be computed from only the top two.

Hint 3

Sort each customer's categories by (-spend, date) and assign ranks as RANK() does, so tied entries share a rank and the next rank jumps to the position index plus one. Add no-purchase shoppers at the end.