KatInterview

Single Funnel Score for Ads from Impression and Click Logs

SQL & DataModel answer
MediumModel answerSQL & Datasqljoinswindow-functions

Last updated

Design one metric that blends click-through and purchase quality, then write SQL that attributes clicks to impressions and reports it per ad for the past week.

Scoring ads from raw event logs

An in-app advertising system writes two event tables.

impressions has one row each time an ad is shown.

user_idad_idevent_timeplatform
10190012023-07-01 10:00:00iOS
10290022023-07-01 10:00:05Android
10190032023-07-01 10:00:10Web

clicks_conversions has one row per click, with a flag showing whether that click ended in a purchase.

user_idad_idclick_timeis_purchase
10190012023-07-01 10:00:020
10290022023-07-01 10:00:061
10390042023-07-01 10:00:120

Your task

  1. Propose one metric that reflects both how often an ad gets clicked and how well those clicks turn into purchases. Explain why you chose it and why it beats looking at click-through rate or conversion rate alone.
  2. Write a SQL query that joins the two tables and returns this metric per ad_id for the last 7 days.

What to cover

  • The join keys and the timing rule that links a click to an impression.
  • How you avoid counting a click twice when a user saw the same ad several times.
  • What happens to clicks that have no matching impression, and to ads that were shown but never clicked.
  • Division-by-zero safety and integer-division pitfalls.
  • How you would treat ads with very few impressions.
  • Optionally, the same logic in pandas.

Hints

Hint 1

Write the funnel as impressions -> clicks -> purchases. Think about what you get if you multiply the rate of the first step by the rate of the second.

Hint 2

Link a click to an impression using both user_id and ad_id, require the click to come after the impression, and cap the gap with an explicit attribution window. Then keep only the most recent qualifying impression per click.

Hint 3

Aggregate impressions and attributed clicks separately and LEFT JOIN from the impression side, so ads with zero clicks still appear. Use NULLIF for ratios and multiply by 1.0 to avoid integer division.