KatInterview

Daily SHOP vs WEBSITE Revenue Mix and Efficiency Report

SQL & DataAuto-graded · 15 test cases
MediumAuto-gradedSQL & Dataaggregationhash-mapdate-window

Last updated

Aggregate two event tables over a 30-day window, fill missing rows with zeros, and compute safe-division shares and revenue-per-conversion per day and destination.

Daily SHOP vs WEBSITE mix report

An advertising team sends users from ads to one of two destinations: a hosted storefront ("SHOP") or the advertiser's own site ("WEBSITE"). Two logs record what each destination earned.

Write a function that turns the two logs into a daily summary for the most recent 30 days of data.

Inputs

  1. revenue_rows: a list of dicts, each with:
    • event_date: string in YYYY-MM-DD format
    • advertiser_id: integer
    • destination: "SHOP" or "WEBSITE"
    • revenue_usd: number (attributed revenue)
  2. conversion_rows: a list of dicts, each with:
    • event_date: string in YYYY-MM-DD format
    • advertiser_id: integer
    • destination: "SHOP" or "WEBSITE"
    • conversions: non-negative integer

Rules

  • Window. Let end be the latest event_date found across both inputs combined. The window is the 30 calendar days ending at end, inclusive, so it starts at end - 29 days. Rows outside the window are ignored.
  • Combining rows. Several advertisers may report for the same date and destination. Sum them to get one revenue value and one conversion value per (event_date, destination).
  • Active dates. A date is active if, inside the window, it appears in at least one of the two inputs. Report only active dates, in ascending order. Do not invent dates that have no rows at all.
  • Missing data counts as zero. If a destination has no revenue (or no conversions) on an active date, treat that value as 0.
  • Zero denominators give None. Any ratio whose denominator is zero must be None, not 0.

Output

Return a dict with two lists.

"shop_share" has one entry per active date, ascending, each with:

  • event_date: the date string
  • shop_revenue: total revenue for "SHOP" that day
  • total_revenue: total revenue across both destinations that day
  • shop_revenue_share: shop_revenue / total_revenue, or None if total_revenue is 0

"performance" has, for every active date (ascending), exactly two entries: first "SHOP", then "WEBSITE". Each entry has:

  • event_date
  • destination
  • revenue_usd: that destination's revenue that day
  • conversions: that destination's conversions that day
  • revenue_per_conversion: revenue_usd / conversions, or None if conversions is 0
  • revenue_share: revenue_usd divided by the day's total revenue across destinations, or None if that total is 0
  • conversion_share: conversions divided by the day's total conversions across destinations, or None if that total is 0

If both inputs are empty, return {"shop_share": [], "performance": []}.

Constraints

  • 0 <= len(revenue_rows), len(conversion_rows) <= 10^5
  • Dates are valid ISO dates (YYYY-MM-DD).
  • destination is always "SHOP" or "WEBSITE".
  • revenue_usd >= 0 and conversions >= 0.
  • Use ordinary true division for ratios (no rounding).

Examples

Example 1

Input: summarize_shop_mix([{'event_date': '2025-03-01', 'advertiser_id': 1, 'destination': 'SHOP', 'revenue_usd': 120}, {'event_date': '2025-03-01', 'advertiser_id': 1, 'destination': 'WEBSITE', 'revenue_usd': 80}], [{'event_date': '2025-03-01', 'advertiser_id': 1, 'destination': 'SHOP', 'conversions': 4}, {'event_date': '2025-03-01', 'advertiser_id': 1, 'destination': 'WEBSITE', 'conversions': 2}])

Output: {'shop_share': [{'event_date': '2025-03-01', 'shop_revenue': 120, 'total_revenue': 200, 'shop_revenue_share': 0.6}], 'performance': [{'event_date': '2025-03-01', 'destination': 'SHOP', 'revenue_usd': 120, 'conversions': 4, 'revenue_per_conversion': 30, 'revenue_share': 0.6, 'conversion_share': 0.6666666666666666}, {'event_date': '2025-03-01', 'destination': 'WEBSITE', 'revenue_usd': 80, 'conversions': 2, 'revenue_per_conversion': 40, 'revenue_share': 0.4, 'conversion_share': 0.3333333333333333}]}

Example 2

Input: summarize_shop_mix([{'event_date': '2025-04-10', 'advertiser_id': 2, 'destination': 'SHOP', 'revenue_usd': 0}], [{'event_date': '2025-04-10', 'advertiser_id': 2, 'destination': 'SHOP', 'conversions': 0}])

Output: {'shop_share': [{'event_date': '2025-04-10', 'shop_revenue': 0, 'total_revenue': 0, 'shop_revenue_share': None}], 'performance': [{'event_date': '2025-04-10', 'destination': 'SHOP', 'revenue_usd': 0, 'conversions': 0, 'revenue_per_conversion': None, 'revenue_share': None, 'conversion_share': None}, {'event_date': '2025-04-10', 'destination': 'WEBSITE', 'revenue_usd': 0, 'conversions': 0, 'revenue_per_conversion': None, 'revenue_share': None, 'conversion_share': None}]}

Hints

Hint 1

First find the newest date across both lists together, then subtract 29 days to get the window start. ISO date strings can be parsed with `datetime.date.fromisoformat`.

Hint 2

Build two dictionaries keyed by `(event_date, destination)` that sum revenue and conversions over all advertisers, keeping only rows in the window. Use `.get(key, 0)` so missing rows act as zeros.

Hint 3

The active dates are the union of the dates seen in both dictionaries. For each one, compute the daily totals once, then emit SHOP and WEBSITE rows, returning `None` whenever a denominator is 0.