Last-Touch Ad Click Attribution with a Seven-Day Window
Last updated: November 29, 2026
Quick Overview
Given purchase events and ad click events, write pandas code that attributes each purchase to the user's most recent ad click in the 7 days before it, and reports purchases and revenue per campaign plus unattributed.
Meta
November 29, 20260
0
0 solved
Given purchase events and ad click events, write pandas code that attributes each purchase to the user's most recent ad click in the 7 days before it, and reports purchases and revenue per campaign plus unattributed.
Attribution logic underlies the conversion numbers Meta Ads reports to advertisers, so DS candidates on Ads teams get asked to implement it and to explain what it does not measure. The interviewer checks a time-aware join (merge_asof), the per-user constraint, the window tolerance, and handling of unattributed purchases.
What the Interviewer Expects
- Uses merge_asof with by and direction backward instead of a cross join
- Sorts both frames on the join key as merge_asof requires
- Applies a 7-day tolerance and keeps unattributed purchases
- Excludes clicks that happen after the purchase
- Explains the gap between attribution and incrementality
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you implement linear multi-touch attribution instead?
- How would you include view-through attribution with a 1-day window?
- Why are attributed conversions not the same as causal conversions?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
purchases(purchase_id, user_id, ts, value)clicks(user_id, campaign_id, ts)
Rule: a purchase is credited to the latest click by the same user at or before the purchase time, if that click is ...
Approach
A cross join per user then filtering is per user and blows up for heavy users. pd.merge_asof does exactly the "nearest earlier row" join in sorted order: `...