Exponentially Smoothed Daily Click-Through Rate for an Ad Set

Last updated: November 29, 2026

Quick Overview

Write a Python function that takes daily impressions and clicks for a Meta ad set and returns each day's raw CTR plus an exponentially smoothed CTR. Low-traffic days should not swing the smoothed value much.

Meta
Data Manipulation (SQL/Python)
Machine Learning Engineer
Meta
November 29, 2026
Machine Learning Engineer
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


Write a Python function that takes daily impressions and clicks for a Meta ad set and returns each day's raw CTR plus an exponentially smoothed CTR. Low-traffic days should not swing the smoothed value much.

A Python data round for ads ranking and measurement engineers, where smoothed CTR feeds dashboards and sometimes features. The interviewer is checking whether you smooth the ratio naively or smooth numerator and denominator separately, and whether you handle zero days.

What the Interviewer Expects
  • Implement the EMA recurrence correctly
  • Smooth clicks and impressions separately, then divide
  • Handle zero-impression days without division errors
  • Initialize the EMA sensibly
  • Explain the effect of alpha
Key Topics to Cover
exponential moving average
ratio smoothing
CTR
Python
numerical edge cases
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you pick alpha, or express it as a half-life?
  • How would you add a Bayesian prior toward the campaign average?
  • How would you vectorize this in pandas?
  • What if days are missing rather than zero?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

Input: a list of (date, impressions, clicks) sorted by date with one row per day, including days with zero impressions. Output: (date, daily_ctr, ema_ctr). Alpha is the weight on the newest day. I...

Approach

The naive EMA of daily CTR treats a day with 10 impressions and 5 clicks (50% CTR) the same as a day with 1,000 impressions. Instead I keep an EMA of impressions and an EMA of clicks and report their ...


Submit Your Answer
Markdown supported