Find top advertisers and cost per KPI
MolocoData Scientist · Technical ScreenLast updated
This question evaluates proficiency in data manipulation and analytical SQL/Python skills, including aggregation, ranking with ties, dataset joins at a defined grain, date-based filtering relative to campaign launch dates, and handling division-by-zero scenarios when computing cost metrics.
You are given three advertising campaign tables.
Table: campaign_spend
| Column | Type | Description |
|---|---|---|
date | DATE | Calendar date in UTC. |
advertiser_id | BIGINT | Advertiser identifier. |
campaign_id | BIGINT | Campaign identifier. |
creative_group | VARCHAR | Creative group within a campaign. |
ad_spend | DECIMAL | Amount spent on ads for the row. |
impressions | BIGINT | Number of ad impressions. |
Table: campaign_conversion
| Column | Type | Description |
|---|---|---|
date | DATE | Calendar date in UTC. |
advertiser_id | BIGINT | Advertiser identifier. |
campaign_id | BIGINT | Campaign identifier. |
creative_group | VARCHAR | Creative group within a campaign. |
installs | BIGINT | Number of installs attributed to the campaign. |
kpi_cnt | BIGINT | Final KPI conversion count. Different campaigns may optimize for different KPIs, but this column stores the final conversion count for that campaign's KPI. |
Table: campaign_goal
| Column | Type | Description |
|---|---|---|
campaign_id | BIGINT | Campaign identifier. |
campaign_goal | VARCHAR | Campaign optimization goal, such as install, purchase, signup, or other KPI type. |
campaign_launch_date | DATE | Campaign launch date in UTC. |
Assume campaign_id maps to exactly one campaign_goal. Spend and conversion rows can be joined at the grain of date, advertiser_id, campaign_id, and creative_group when both spend and conversion information are needed.
Answer the following SQL questions:
- Find the top 3 spending advertisers for each day.
- Aggregate spend at the
dateandadvertiser_idlevel. - Rank advertisers by total daily spend in descending order.
- Return all advertisers tied within the top 3 ranks if ties occur.
- Required output columns:
date,advertiser_id,daily_ad_spend,spend_rank.
- Calculate cost per KPI for each campaign goal.
- For each
campaign_goal, aggregate total ad spend and total KPI conversions across all campaigns with that goal. - Use only spend and conversions on or after each campaign's
campaign_launch_date. - Define
cost_per_kpi = total_ad_spend / total_kpi_cnt. - If
total_kpi_cntis zero, returnNULLforcost_per_kpiinstead of dividing by zero. - Required output columns:
campaign_goal,total_ad_spend,total_kpi_cnt,cost_per_kpi.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.