cswork / libraryOpen practice workspace

Coding questions · Meta

Compute ad impression conversion rates

This question evaluates proficiency in time-based event attribution, aggregation of business metrics (impressions, conversion rates, spend and revenue), timestamp handling, and user/ad-level matching within the Data Manipulation (SQL/Python) domain, emphasizing practical application over abstract theory.

You are given two tables for an ads product.

Table: ad_impressions

ColumnTypeDescription
impression_idSTRINGUnique identifier for an ad impression. Primary key.
user_idSTRINGUser who saw the ad.
ad_idSTRINGAd that was shown.
campaign_idSTRINGCampaign that owns the ad.
impression_tsTIMESTAMPTime the impression was served, stored in UTC.
cost_usdNUMERICAdvertiser cost for the impression in USD.

Table: ad_conversions

ColumnTypeDescription
conversion_idSTRINGUnique identifier for a conversion event. Primary key.
user_idSTRINGUser who converted.
ad_idSTRINGAd associated with the conversion event.
conversion_tsTIMESTAMPTime the conversion occurred, stored in UTC.
revenue_usdNUMERICConversion value in USD.

Attribution rule:

  • A conversion can be attributed only to an impression from the same user_id and ad_id.
  • The impression must occur before the conversion.
  • The conversion must occur within 7 days after the impression.
  • If multiple impressions are eligible for the same conversion, attribute the conversion to the most recent eligible impression before the conversion.
  • Each conversion should be attributed at most once.

Write a SQL query for impressions served from 2024-01-01 00:00:00 UTC through 2024-01-31 23:59:59 UTC. For each campaign_id and UTC impression_date, return:

  • campaign_id
  • impression_date
  • impressions: number of impressions
  • unique_impression_users: number of distinct users with impressions
  • attributed_conversions: number of conversions attributed to those impressions
  • conversion_rate: attributed conversions divided by impressions
  • total_spend_usd: total impression cost
  • total_conversion_revenue_usd: total attributed conversion revenue
  • avg_hours_to_conversion: average hours between impression and attributed conversion, across attributed conversions only

Rows with impressions but zero attributed conversions should still appear with zero conversions and zero conversion revenue.

Compute ad impression conversion rates

Compute per-campaign, per-UTC impression_date metrics for impressions served between 2024-01-01 00:00:00 and 2024-01-31 23:59:59 UTC, attributing conversions to impressions by user_id and ad_id within a 7-day lookback, using last-touch before conversion and attributing each conversion at most once.

Table ad_impressions

columntypeconstraints
impression_idVARCHARPRIMARY KEY
user_idVARCHAR
ad_idVARCHAR
campaign_idVARCHAR
impression_tsTIMESTAMP
cost_usdNUMERIC

Sample data:

ad_idcampaign_idcost_usdimpression_idimpression_tsuser_id
a1cA0.5imp_72023-12-31 23:00:00u2
a1cA0.52imp_82024-01-01 08:00:00u2
a1cA0.5imp_12024-01-03 10:00:00u1
a1cA0.55imp_22024-01-05 09:00:00u1
a1cA0.6imp_32024-01-05 12:00:00u2
a2cA0.45imp_42024-01-15 08:30:00u3
a4cB0.7imp_52024-01-20 14:15:00u1
a3cB0.65imp_62024-01-20 16:45:00u4

Table ad_conversions

columntypeconstraints
conversion_idVARCHARPRIMARY KEY
user_idVARCHAR
ad_idVARCHAR
conversion_tsTIMESTAMP
revenue_usdNUMERIC

Sample data:

ad_idconversion_idconversion_tsrevenue_usduser_id
a1conv_12024-01-01 09:00:005u2
a1conv_22024-01-06 10:00:0012u1
a1conv_32024-01-12 12:00:008u2
a1conv_42024-01-09 08:00:004u1
a4conv_52024-01-21 10:00:0020u1
a3conv_62024-01-25 16:45:0010u4
a1conv_72024-01-05 08:00:003u1
a1conv_82024-01-01 07:30:006u2

Expected output

One row per (campaign_id, impression_date) for January 2024 impressions, including counts of impressions and unique users, last-touch 7-day attributed conversions and rate, total spend, total attributed revenue, and average hours from impression to conversion. Rows with zero attributed conversions are included with zero conversions and revenue and null average hours.

attributed_conversionsavg_hours_to_conversioncampaign_idconversion_rateimpression_dateimpressionstotal_conversion_revenue_usdtotal_spend_usdunique_impression_users
11cA12024-01-01150.521
146cA12024-01-03130.51
396cA1.52024-01-052241.152
0NULLcA02024-01-15100.451
269.875cB12024-01-202301.352

Hints

  1. Use a 7-day last-touch attribution: join conversions to impressions on user_id and ad_id with i.impression_ts <= c.conversion_ts <= i.impression_ts + interval '7 days', then pick the most recent impression per conversion via ROW_NUMBER ordered by impression_ts DESC.
  2. Determine attribution across all impressions first, then filter to those whose impression falls in January; this prevents misattribution to January impressions when a more recent eligible impression exists in late December.
  3. Filter the impression window using a half-open interval: impression_ts >= TIMESTAMP '2024-01-01 00:00:00' and impression_ts < TIMESTAMP '2024-02-01 00:00:00'.
  4. Aggregate impressions and conversions separately and left join so dates with impressions but zero conversions still appear with zeros for conversion counts and revenue.
  5. Avoid the invalid syntax TIMESTAMP('2024-01-01 00:00:00+00'); in PostgreSQL use TIMESTAMP '2024-01-01 00:00:00'.