cswork / libraryOpen practice workspace

Coding questions · TikTok

Calculate Conversion Rate from Ad Clicks to Page Visits

This question evaluates a candidate's competency in event-level data manipulation and metric computation using SQL or Python, including time-based aggregation, deduplication, and joining or windowing of event streams.

user_events

user_idevent_timeevent_typead_id
12023-01-01 10:00:00ad_click101
12023-01-01 10:05:00page_visitNULL
22023-01-01 11:00:00ad_click102
32023-01-01 11:30:00page_visitNULL
22023-01-01 11:15:00page_visitNULL
Scenario

An online advertising platform wants to measure how well ad clicks convert into subsequent page visits.

Question

Given the user_events table, write SQL that computes the overall conversion rate defined as the share of ad_click events that are followed by at least one page_visit by the same user on the same calendar day. Clearly state the numerator and denominator you use in the metric definition.

Hints

Use distinct user_id and event_type, date truncation, and a left join or window functions to link clicks to visits.