Katkat

Analyze Pirated Theme Usage

ShopifyData Scientist · Technical Screen

Last updated

Calculate monthly metrics on unlicensed theme usage and lost revenue by evaluating temporal interval overlaps between installation lifespans and calendar months.

Problem Statement

Shopify merchants can customize their storefronts using themes from the Theme Store. While most themes are purchased legitimately, some merchants upload pirated or unauthorized copies. The Trust & Safety team wants to analyze the monthly impact of unlicensed theme installations over a given year.

Write a SQL query (or Python / Pandas script) that computes monthly pirated theme metrics for each calendar month in the year 2023 (2023-01 through 2023-12).

Input Schemas

themes

ColumnTypeDescription
theme_idINTUnique identifier for the theme
theme_nameVARCHARName of the theme
priceNUMERIC(10, 2)Official purchase price in USD

theme_installs

ColumnTypeDescription
install_idINTUnique identifier for the installation instance
store_idINTStore that installed the theme
theme_idINTID of the installed theme
installed_atDATEDate the theme was installed
uninstalled_atDATEDate the theme was uninstalled (NULL if currently active)
is_licensedBOOLEANTRUE if properly purchased; FALSE if pirated/unlicensed

Output Format

Return the results ordered chronologically by month ascending:

ColumnTypeDescription
monthVARCHARMonth in YYYY-MM format
unlicensed_storesINTCount of distinct stores that had at least one pirated theme active during the month
pirated_themes_usedINTCount of distinct themes that had at least one pirated install active during the month
est_lost_revenueNUMERIC(10, 2)Sum of theme price for each distinct (store_id, theme_id) pair active without a license in that month

Overlap Definition

An installation is considered active during a calendar month if its installation span [installed_at, COALESCE(uninstalled_at, '9999-12-31')] overlaps with that month's date span [month_start, month_end]: installed_at = month_start.

Edge Cases to Consider

  • Multiple unlicensed installations of the same theme on the same store in the same month should count as a single lost license in est_lost_revenue.
  • Themes installed in 2022 that remained active into 2023 must be counted in 2023 months.
  • Months in 2023 with zero pirated activity should still appear with 0 counts and 0.00 lost revenue.

Hints

Hint 1

Generate a calendar table or CTE containing the start and end dates for all 12 months in 2023 to serve as the base for joining.

Hint 2

Join the calendar table to the unlicensed installations using the interval-overlap condition where installation start is before month end and installation end is after month start.

Hint 3

Use distinct aggregations on `store_id`, `theme_id`, and a combined key or subquery grouping to avoid double-counting revenue when a store reinstalled the same pirated theme within one month.

Follow-up questions

  • How would you optimize the query if the `theme_installs` table contains tens of millions of records?
  • If a merchant ran a pirated theme for only 2 days versus 30 days in a month, how would you modify the model to prorate the revenue impact?
  • How would you handle cases where a merchant initially used a pirated version but purchased a valid license later in the same month?