cswork / libraryOpen practice workspace

Coding questions · Amazon

Compute First Order Proportions by Day and Category

This question evaluates a candidate's ability to perform event-level data manipulation and customer-level deduplication using SQL or Python, focusing on calculating the proportion of orders that represent a customer's first purchase by day and category.

ORDERS

+----------+------------+----------+-------------+
| order_id | date       | category | customer_id |
+----------+------------+----------+-------------+
| 100      | 2025-01-01 | Books    | 1           |
| 101      | 2025-01-02 | Books    | 1           |
| 102      | 2025-01-01 | Music    | 2           |
| 103      | 2025-01-03 | Music    | 3           |
| 104      | 2025-01-03 | Books    | 2           |
+----------+------------+----------+-------------+
Scenario

E-commerce platform analyzing customer purchasing behavior over time.

Question

For each calendar day and product category, compute the proportion of orders that are the customer's first order in that category. For each calendar day, compute the proportion of all orders that are a customer’s first order across any category.

Hints

Use window functions or self-joins to flag first orders, then aggregate by day and category.

Customer First Order Proportions by Day and Category

You are given an ORDERS table for an e-commerce platform:

  • order_id: unique identifier of the order
  • date: calendar date of the order
  • category: product category of the order
  • customer_id: unique identifier of the customer

Task:

  1. For each calendar day and product category, compute the proportion of orders that are the customer's first order in that category.
  2. For each calendar day, compute the proportion of all orders that are a customer’s first order across any category.

Return both metrics in a single result set. Use category = 'ALL' for the per-day overall proportion rows and label metrics using a column named metric (e.g., 'first_in_category_rate' and 'first_overall_rate').

Table ORDERS

columntypeconstraints
order_idINTEGERPRIMARY KEY, NOT NULL
dateDATENOT NULL
categoryVARCHAR(50)NOT NULL
customer_idINTEGERNOT NULL

Sample data:

categorycustomer_iddateorder_id
Books12025-01-01100
Books12025-01-02101
Music22025-01-01102
Music32025-01-03103
Books22025-01-03104

Expected output

Output a row per date and category with the proportion of orders that are the customer’s first order in that category, and a row per date with the proportion of all orders that are the customer’s first order across any category. Overall rows use category = 'ALL'. The metric column distinguishes 'first_in_category_rate' versus 'first_overall_rate'. Proportion is a numeric value between 0 and 1.

categorydatemetricproportion
Books2025-01-01first_in_category_rate1
Music2025-01-01first_in_category_rate1
ALL2025-01-01first_overall_rate1
Books2025-01-02first_in_category_rate0
ALL2025-01-02first_overall_rate0
Books2025-01-03first_in_category_rate1
Music2025-01-03first_in_category_rate1
ALL2025-01-03first_overall_rate0.5

Hints

  1. Use ROW_NUMBER() over partitions by (customer_id, category) to flag a customer’s first order in each category.
  2. Use ROW_NUMBER() over partitions by customer_id (without category) to flag each customer’s first order overall.
  3. Aggregate by date and category (and by date alone) and compute the AVG of the 0/1 flags to get proportions, then UNION ALL the two result sets.