KatInterview

Multi-Year Sales Analysis by Customer Demographics

SQL & DataAuto-graded · 14 test cases
MediumAuto-gradedSQL & Datadata-manipulationaggregationcohort-analysis

Last updated

Tests data joining, deduplication, cohort segmentation, conditional aggregation, and percentage calculations across multiple tables.

You are given four tables representing an e-commerce database:

orders: [order_id, user_id, order_date]
order_items: [order_id, product_id, qty]
products: [product_id, name, category]
users: [user_id, age, location]

Each table is provided as a list of rows. Date strings are in ISO format (YYYY-MM-DD).

Compute three metrics and return them in a dictionary:

Part 1: Subscription Order Percentage (2024)

Calculate what percentage of orders placed in calendar year 2024 included at least one product with category = 'Subscription'. An order containing multiple subscription products should be counted only once. Round the result to two decimal places.

Return this value in the key pct_subscription_2024.

Part 2: Year-over-Year Order Growth by Demographic Cohort

Segment users into age groups:

  • '18-29': ages 18 through 29 inclusive
  • '30-44': ages 30 through 44 inclusive
  • '45+': ages 45 and above

For each combination of (location, age_group) that appears in the data, compute:

  • The count of distinct orders placed in 2023
  • The count of distinct orders placed in 2024
  • The year-over-year percentage change: (orders_2024 - orders_2023) / orders_2023

If orders_2023 is zero, set yoy_pct_change to None (null). An order is attributed to the user's current age and location.

Return a list of dictionaries sorted by (location, age_group), each containing:

{'location': str, 'age_group': str, 'orders_2023': int, 'orders_2024': int, 'yoy_pct_change': float or None}

Store this list in the key yoy_by_cohort.

Part 3: Unique Users Who Purchased "Pro" Products in 2024

Identify all users who purchased at least one product in 2024 whose name contains the substring 'pro' (case-insensitive). For each (location, age_group) cohort that has such users, count the number of unique users.

Return a list of dictionaries sorted by:

  1. unique_users descending
  2. location ascending (tie-breaker)
  3. age_group ascending (tie-breaker)

Each dictionary contains:

{'location': str, 'age_group': str, 'unique_users': int}

Store this list in the key python_pro_users_by_cohort.

Constraints

  • 1 ≤ number of orders ≤ 10,000
  • 1 ≤ number of order_items ≤ 50,000
  • 1 ≤ number of products ≤ 1,000
  • 1 ≤ number of users ≤ 10,000
  • All IDs are positive integers
  • Dates are valid ISO strings (YYYY-MM-DD)
  • Ages are between 18 and 100
  • An order may have zero items (edge case)
  • A product_id in order_items may not exist in products (handle gracefully)
  • A user_id in orders may not exist in users (skip such orders)

Examples

Example 1

Input: analyze_sales_by_demographics([[1, 101, '2023-01-10'], [2, 102, '2023-05-03'], [3, 101, '2024-02-12'], [4, 103, '2024-11-20'], [5, 104, '2024-12-28']], [[1, 10, 1], [1, 11, 2], [2, 11, 1], [3, 12, 1], [4, 10, 1], [5, 13, 1]], [[10, 'Widget Pro', 'Subscription'], [11, 'Widget', 'Standard'], [12, 'Gadget Pro', 'Subscription'], [13, 'Service', 'Standard']], [[101, 27, 'NY'], [102, 42, 'CA'], [103, 35, 'NY'], [104, 23, 'TX']])

Output: {'pct_subscription_2024': 66.67, 'yoy_by_cohort': [{'location': 'CA', 'age_group': '30-44', 'orders_2023': 1, 'orders_2024': 0, 'yoy_pct_change': -1}, {'location': 'NY', 'age_group': '18-29', 'orders_2023': 1, 'orders_2024': 1, 'yoy_pct_change': 0}, {'location': 'NY', 'age_group': '30-44', 'orders_2023': 0, 'orders_2024': 1, 'yoy_pct_change': None}, {'location': 'TX', 'age_group': '18-29', 'orders_2023': 0, 'orders_2024': 1, 'yoy_pct_change': None}], 'python_pro_users_by_cohort': [{'location': 'NY', 'age_group': '18-29', 'unique_users': 1}, {'location': 'NY', 'age_group': '30-44', 'unique_users': 1}]}

Hints

Hint 1

Joining orders to order_items creates one row per item. Use distinct order_id after filtering to avoid double-counting orders with multiple subscription products.

Hint 2

Build lookup dictionaries for products and users by ID to avoid repeated linear scans. Use defaultdict to group items by order_id upfront.

Hint 3

For Part 2, collect order IDs into sets indexed by (location, age_group, year) so deduplication is automatic. For Part 3, collect user IDs into sets to ensure each user is counted once per cohort.