cswork / libraryOpen practice workspace

Coding questions · Moloco

Find top advertisers and cost per KPI

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

ColumnTypeDescription
dateDATECalendar date in UTC.
advertiser_idBIGINTAdvertiser identifier.
campaign_idBIGINTCampaign identifier.
creative_groupVARCHARCreative group within a campaign.
ad_spendDECIMALAmount spent on ads for the row.
impressionsBIGINTNumber of ad impressions.

Table: campaign_conversion

ColumnTypeDescription
dateDATECalendar date in UTC.
advertiser_idBIGINTAdvertiser identifier.
campaign_idBIGINTCampaign identifier.
creative_groupVARCHARCreative group within a campaign.
installsBIGINTNumber of installs attributed to the campaign.
kpi_cntBIGINTFinal 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

ColumnTypeDescription
campaign_idBIGINTCampaign identifier.
campaign_goalVARCHARCampaign optimization goal, such as install, purchase, signup, or other KPI type.
campaign_launch_dateDATECampaign 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:

  1. Find the top 3 spending advertisers for each day.

    • Aggregate spend at the date and advertiser_id level.
    • 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.
  2. 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_cnt is zero, return NULL for cost_per_kpi instead of dividing by zero.
    • Required output columns: campaign_goal, total_ad_spend, total_kpi_cnt, cost_per_kpi.

Find top advertisers and cost per KPI

Table campaign_spend

columntypeconstraints
dateDATE
advertiser_idBIGINT
campaign_idBIGINT
creative_groupVARCHAR
ad_spendDECIMAL
impressionsBIGINT

Sample data:

ad_spendadvertiser_idcampaign_idcreative_groupdateimpressions
120101001A2024-01-0510000
80201002B2024-01-057000
30101003C2024-01-054000

Table campaign_conversion

columntypeconstraints
dateDATE
advertiser_idBIGINT
campaign_idBIGINT
creative_groupVARCHAR
installsBIGINT
kpi_cntBIGINT

Sample data:

advertiser_idcampaign_idcreative_groupdateinstallskpi_cnt
101001A2024-01-051010
201002B2024-01-0542
101003C2024-01-0501

Table campaign_goal

columntypeconstraints
campaign_idBIGINT
campaign_goalVARCHAR
campaign_launch_dateDATE

Sample data:

campaign_goalcampaign_idcampaign_launch_date
install10012024-01-01
purchase10022024-01-01
purchase10032024-01-01

Expected output

Single result set with two summary rows to satisfy validation expectations. Each row labels a sub-result and reports how many rows that sub-result would return on the provided sample data.

result_namerows
q1_top_advertisers_rows2
q2_cost_per_kpi_rows2

Hints

  1. Use SUM(ad_spend) grouped by date and advertiser_id to get daily totals, then RANK() OVER (PARTITION BY date ORDER BY daily_ad_spend DESC) to include ties.
  2. For cost per KPI, join spend and conversions to campaign_goal separately by campaign_id and filter by date >= campaign_launch_date before aggregating.
  3. Left join aggregated spend/conversion to the distinct set of goals to ensure every campaign_goal appears, even if it has no data.
  4. Avoid division by zero using CASE or NULLIF when calculating cost_per_kpi.