KatInterview

Daily Campaign Performance Summary Over a Rolling Window

SQL & DataAuto-graded · 14 test cases
MediumAuto-gradedSQL & Dataaggregationjoindata-manipulation

Last updated

Tests conditional aggregation across multiple related tables, time-window filtering, fan-out avoidance, and safe division when computing per-day engagement rates.

Daily Campaign Performance Summary Over a Rolling Window

You are given three collections of records describing a set of ads, their exposure and engagement events, and downstream conversions.

Input tables

ads — one entry per ad

FieldTypeDescription
ad_idintUnique ad identifier
advertiser_idintOwner of the ad
ad_typestrFormat of the ad (e.g. "image", "video")

events — one entry per exposure or click event

FieldTypeDescription
event_idintUnique event identifier
ad_idintAd that was shown or clicked
is_clickint / bool1 (or True) if the user clicked, 0 (or False) for a plain view
event_timestrCalendar date of the event, "YYYY-MM-DD"

conversions — one entry per conversion that followed an event

FieldTypeDescription
conv_idintUnique conversion identifier
event_idintThe originating event
ad_idintAd that drove the conversion
conv_timestrCalendar date of the conversion, "YYYY-MM-DD"

Task

Given a reference date snapshot_date ("YYYY-MM-DD"), produce a summary covering the 7-day window [snapshot_date − 6 days, snapshot_date], both endpoints inclusive.

For every (calendar_date, ad_id) pair that has at least one event or conversion within the window, return a record containing:

  • date — the calendar date ("YYYY-MM-DD")
  • ad_id — the ad identifier
  • impressions — total event rows for that ad on that date (every row counts, whether or not it is a click)
  • clicks — event rows where is_click is truthy
  • conversions — conversion rows whose conv_time falls on that date for that ad (attributed to the conversion date, not the originating event's date)
  • ctr — click-through rate: clicks / impressions, or 0.0 when impressions is 0
  • conversion_rate — conversions / clicks, or 0.0 when clicks is 0

Only include rows for ad_id values present in ads. Do not emit zero-filled rows for ads or dates with no activity at all. Conversions may exceed clicks on a given day (e.g. from repeat conversions on a later date); do not cap the rate.

Return the list sorted by (date, ad_id) ascending.

Constraints

  • 0 ≤ len(ads) ≤ 10 000
  • 0 ≤ len(events) ≤ 500 000
  • 0 ≤ len(conversions) ≤ 500 000
  • All dates are valid ISO-8601 strings ("YYYY-MM-DD").
  • snapshot_date is always a valid date; data outside the 7-day window should be ignored.
  • ad_id values in events or conversions that do not appear in ads must be ignored.
  • is_click is 0/1 or False/True; treat it as a boolean.
  • The same conversion can appear on a different calendar date than its originating event.
  • ctr and conversion_rate are plain Python floats; no rounding is required.

Examples

Example 1

Input: compute_daily_ad_performance([{'ad_id': 101, 'advertiser_id': 1001, 'ad_type': 'image'}, {'ad_id': 102, 'advertiser_id': 1002, 'ad_type': 'video'}, {'ad_id': 103, 'advertiser_id': 1001, 'ad_type': 'carousel'}], [{'event_id': 1, 'ad_id': 101, 'is_click': 0, 'event_time': '2023-07-01'}, {'event_id': 2, 'ad_id': 101, 'is_click': 1, 'event_time': '2023-07-01'}, {'event_id': 3, 'ad_id': 102, 'is_click': 0, 'event_time': '2023-07-01'}, {'event_id': 4, 'ad_id': 103, 'is_click': 1, 'event_time': '2023-07-02'}], [{'conv_id': 900, 'event_id': 2, 'ad_id': 101, 'conv_time': '2023-07-01'}, {'conv_id': 901, 'event_id': 4, 'ad_id': 103, 'conv_time': '2023-07-02'}, {'conv_id': 902, 'event_id': 2, 'ad_id': 101, 'conv_time': '2023-07-03'}], '2023-07-03')

Output: [{'date': '2023-07-01', 'ad_id': 101, 'impressions': 2, 'clicks': 1, 'conversions': 1, 'ctr': 0.5, 'conversion_rate': 1}, {'date': '2023-07-01', 'ad_id': 102, 'impressions': 1, 'clicks': 0, 'conversions': 0, 'ctr': 0, 'conversion_rate': 0}, {'date': '2023-07-02', 'ad_id': 103, 'impressions': 1, 'clicks': 1, 'conversions': 1, 'ctr': 1, 'conversion_rate': 1}, {'date': '2023-07-03', 'ad_id': 101, 'impressions': 0, 'clicks': 0, 'conversions': 1, 'ctr': 0, 'conversion_rate': 0}]

Hints

Hint 1

Build a set of valid ad IDs from the ads list first so you can filter both tables in O(1) per row.

Hint 2

Aggregate the events table and the conversions table independently into a shared (date, ad_id) dictionary — do not join them row by row, or a single event with multiple conversions will inflate your impression and click counts.

Hint 3

Compute CTR and conversion rate only after all counts are finalised, and guard each division with a check that the denominator is non-zero before dividing.