cswork / libraryOpen practice workspace

Coding questions · Meta

Analyze spend cohort and source shifts

This question evaluates data manipulation and analytical SQL/Python skills, focusing on cohort analysis, time-series aggregation, joins and filtering, and comparative spend analytics across calendar years.

You work on an ads platform. Assume all timestamps are in UTC. Interpret last year as calendar year 2023 and this year as calendar year 2024.

Tables:

  • advertisers(advertiser_id BIGINT, advertiser_type VARCHAR, country VARCHAR, created_at TIMESTAMP)
  • ads(ad_id BIGINT, advertiser_id BIGINT, creation_source VARCHAR) where creation_source can take values such as MANUAL, AI_ASSISTED, IMAGE_TEMPLATE, and VIDEO_TEMPLATE
  • ad_spend_daily(spend_date DATE, ad_id BIGINT, spend_usd DECIMAL(18,2))

Key relationships:

  • One advertiser can have many ads.
  • One ad can have many daily spend records.

Questions:

  1. Identify advertisers whose total spend in 2023 exceeded 1,000 USD. Among all platform spend in 2024, what percentage of 2024 spend came from this advertiser cohort? Return the following columns: eligible_advertisers, cohort_spend_2024, total_platform_spend_2024, and cohort_share_2024.
  2. PMs noticed that spend from one ad creation source is growing and want to know whether that growth may be driven by a decline in another source. Write a query that returns monthly 2024 spend by creation_source, while excluding advertisers whose advertiser_type is in ('INTERNAL', 'POLITICAL', 'TEST'). Also include, for each month, the amount of spend from advertisers whose AI_ASSISTED spend increased versus the same month in 2023 while their MANUAL spend decreased, so the team can assess possible cannibalization between creation sources.

Analyze spend cohort and source shifts

Table advertisers

columntypeconstraints
advertiser_idBIGINTPRIMARY KEY, NOT NULL
advertiser_typeVARCHARNOT NULL
countryVARCHARNOT NULL
created_atTIMESTAMPNOT NULL

Sample data:

advertiser_idadvertiser_typecountrycreated_at
1BRANDUS2022-05-15 10:00:00
2AGENCYUK2023-01-03 12:00:00
3BRANDCA2021-07-20 09:00:00
4TESTUS2022-11-11 15:30:00

Table ads

columntypeconstraints
ad_idBIGINTPRIMARY KEY, NOT NULL
advertiser_idBIGINTNOT NULL, FOREIGN KEY REFERENCES advertisers(advertiser_id)
creation_sourceVARCHARNOT NULL

Sample data:

ad_idadvertiser_idcreation_source
1011MANUAL
1021AI_ASSISTED
2012MANUAL
3013MANUAL
3023AI_ASSISTED
4014AI_ASSISTED

Table ad_spend_daily

columntypeconstraints
spend_dateDATENOT NULL
ad_idBIGINTNOT NULL, FOREIGN KEY REFERENCES ads(ad_id)
spend_usdDECIMAL(18,2)NOT NULL

Sample data:

ad_idspend_datespend_usd
1012023-03-151000
1022023-06-20500
2012023-02-10900
3012023-04-051200
3022023-10-12800
4012023-08-01300
1012024-01-101200
1022024-02-05800
2012024-03-081000
3012024-01-201800
3022024-04-221200
4012024-02-14400

Expected output

One-row summary for Q1 showing the number of eligible advertisers (2023 spend > 1,000), their total 2024 spend, the platform's total 2024 spend, and the share of 2024 spend from this cohort. The result_set column labels this output as Q1.

cohort_share_2024cohort_spend_2024eligible_advertisersresult_settotal_platform_spend_2024
0.7812550002Q16400

Hints

  1. Treat last year as 2023 and this year as 2024: use spend_date >= '2023-01-01' and < '2024-01-01' for 2023, and spend_date >= '2024-01-01' and < '2025-01-01' for 2024.
  2. Build a 2023 advertiser spend CTE, filter to > 1000 to define the cohort, then compute 2024 spend for the cohort and for the whole platform; divide for the share.
  3. For Q2: Aggregate monthly spend in 2024 by creation_source after joining ad_spend_daily -> ads -> advertisers and excluding advertiser_type in ('INTERNAL','POLITICAL','TEST').
  4. For the cannibalization flag in Q2: compute advertiser-month spend for 2023 and 2024 by creation_source, pivot AI_ASSISTED and MANUAL, then flag advertisers where AI_2024 > AI_2023 and MANUAL_2024 < MANUAL_2023; sum their 2024 spend for the month into a separate column.