cswork / libraryOpen practice workspace

Coding questions · Meta

Determine Top Advertisers by Conversion Rate and CTR Analysis

This question evaluates a candidate's competency in SQL/Python data manipulation and applied statistical analysis for advertising metrics, specifically computing click-through rates, conversion rates, time-based aggregations, and performing proportion tests for peak versus non-peak hours.

ads

+-------+---------------+------------+
| ad_id | advertiser_id | created_at |
+-------+---------------+------------+
| 1     | 101           | 2023-07-01 |
| 2     | 102           | 2023-07-02 |
| 3     | 101           | 2023-07-03 |
+-------+---------------+------------+

ads_impressions

+------------+-------+---------------------+
| impression_id | ad_id | timestamp           |
+------------+-------+---------------------+
| 9001          | 1     | 2023-07-10 18:05 |
| 9002          | 2     | 2023-07-10 14:22 |
| 9003          | 1     | 2023-07-11 19:10 |
+------------+-------+---------------------+

ads_conversions

+--------------+------------+---------------------+
| conversion_id | impression_id | timestamp           |
+--------------+------------+---------------------+
| 5001          | 9001         | 2023-07-10 18:06 |
| 5002          | 9003         | 2023-07-11 19:11 |
+--------------+------------+---------------------+
Scenario

Three tables – ads, ads_impressions, ads_conversions – are provided to evaluate ad performance.

Question

Write a query to compute overall click-through-rate (CTR) per ad for the last 7 days. Return the top 3 advertisers by conversion rate in the most recent week. For each hour of day, compare CTR at peak hours (18:00-22: 00) versus non-peak; output the p-value for difference in proportions. Suggest additional data or tests to confirm: "Ads exposed at peak hours perform better than at non-peak hours."

Hints

Think joins, filtering by timestamps, aggregation, proportion tests.

CTR per ad, last 7d

Compute conversion-through rate per ad over the most recent 7-day window anchored to the latest impression timestamp. Treat an impression as converted if it has at least one row in ads_conversions; ctr_7d is converted impressions divided by total impressions for the ad. Return ad_id, advertiser_id, impressions_7d, conversions_7d, and ctr_7d rounded to 4 decimals, ordered by ad_id.

Table ads

columntypeconstraints
ad_idINTEGERPRIMARY KEY, NOT NULL
advertiser_idINTEGERNOT NULL
created_atDATENOT NULL

Sample data:

ad_idadvertiser_idcreated_at
11012023-07-01
21022023-07-02
31012023-07-03

Table ads_impressions

columntypeconstraints
impression_idINTEGERPRIMARY KEY, NOT NULL
ad_idINTEGERNOT NULL, FOREIGN KEY -> ads.ad_id
timestampTIMESTAMPNOT NULL

Sample data:

ad_idimpression_idtimestamp
190012023-07-10 18:05
290022023-07-10 14:22
190032023-07-11 19:10

Table ads_conversions

columntypeconstraints
conversion_idINTEGERPRIMARY KEY, NOT NULL
impression_idINTEGERNOT NULL, FOREIGN KEY -> ads_impressions.impression_id
timestampTIMESTAMPNOT NULL

Sample data:

conversion_idimpression_idtimestamp
500190012023-07-10 18:06
500290032023-07-11 19:11

Expected output

CTR per ad over the last 7 days anchored to the latest impression timestamp. CTR = conversions_7d / impressions_7d.

ad_idadvertiser_idconversions_7dctr_7dimpressions_7d
1101212
2102001

Hints

  1. Anchor the 7-day window to MAX(ads_impressions.timestamp).
  2. Treat any impression with at least one conversion as converted once.

Top advertisers by CVR

List the top 3 advertisers by conversion rate over the most recent 7-day window anchored to the latest impression timestamp. Conversion rate is conversions divided by impressions after mapping impressions to advertisers through ads. Break ties by conversions descending, impressions descending, then advertiser_id ascending. Return advertiser_id, impressions_7d, conversions_7d, conversion_rate_7d, and rank.

Table ads

columntypeconstraints
ad_idINTEGERPRIMARY KEY, NOT NULL
advertiser_idINTEGERNOT NULL
created_atDATENOT NULL

Sample data:

ad_idadvertiser_idcreated_at
11012023-07-01
21022023-07-02
31012023-07-03

Table ads_impressions

columntypeconstraints
impression_idINTEGERPRIMARY KEY, NOT NULL
ad_idINTEGERNOT NULL, FOREIGN KEY -> ads.ad_id
timestampTIMESTAMPNOT NULL

Sample data:

ad_idimpression_idtimestamp
190012023-07-10 18:05
290022023-07-10 14:22
190032023-07-11 19:10

Table ads_conversions

columntypeconstraints
conversion_idINTEGERPRIMARY KEY, NOT NULL
impression_idINTEGERNOT NULL, FOREIGN KEY -> ads_impressions.impression_id
timestampTIMESTAMPNOT NULL

Sample data:

conversion_idimpression_idtimestamp
500190012023-07-10 18:06
500290032023-07-11 19:11

Expected output

Top 3 advertisers by conversion rate over the last 7 days anchored to the latest impression timestamp, with ties broken by conversions then impressions.

advertiser_idconversion_rate_7dconversions_7dimpressions_7drank
1011221
1020012

Hints

  1. Aggregate at advertiser level after mapping impressions to ads.
  2. Use ROW_NUMBER with ordered tie-breakers.

Hourly peak vs non-peak CTR

For each hour-of-day in the most recent 7-day window anchored to the latest impression timestamp, compute the hour's CTR and compare it to the opposite group (peak hours 18–22 vs non-peak aggregate) using a two-proportion z-test. Return hour_of_day, is_peak, impressions_hour, conversions_hour, ctr_hour, comparison_group, comp_impressions, comp_conversions, and the two-tailed p_value.

Table ads

columntypeconstraints
ad_idINTEGERPRIMARY KEY, NOT NULL
advertiser_idINTEGERNOT NULL
created_atDATENOT NULL

Sample data:

ad_idadvertiser_idcreated_at
11012023-07-01
21022023-07-02
31012023-07-03

Table ads_impressions

columntypeconstraints
impression_idINTEGERPRIMARY KEY, NOT NULL
ad_idINTEGERNOT NULL, FOREIGN KEY -> ads.ad_id
timestampTIMESTAMPNOT NULL

Sample data:

ad_idimpression_idtimestamp
190012023-07-10 18:05
290022023-07-10 14:22
190032023-07-11 19:10

Table ads_conversions

columntypeconstraints
conversion_idINTEGERPRIMARY KEY, NOT NULL
impression_idINTEGERNOT NULL, FOREIGN KEY -> ads_impressions.impression_id
timestampTIMESTAMPNOT NULL

Sample data:

conversion_idimpression_idtimestamp
500190012023-07-10 18:06
500290032023-07-11 19:11

Expected output

Hourly CTR and two-proportion z-test comparing each hour to the opposite group (peak vs non-peak) over the last 7 days anchored to the latest impression timestamp.

comp_conversionscomp_impressionscomparison_groupconversions_hourctr_hourhour_of_dayimpressions_houris_peakp_value
22peak00141false0.0833
01non-peak11181true0.1573
01non-peak11191true0.1573

Hints

  1. Compute per-hour tallies, then aggregate totals for peak and non-peak groups.
  2. Use a pooled two-proportion z-test: z = (p1 - p2) / sqrt(p*(1-p)*(1/n1 + 1/n2)).