Coding questions · Meta
Compute invalid event percentage by pixel
This question evaluates the ability to aggregate time-series event data and compute proportions per entity, specifically summing events across signal types, filtering by a recent date window, and dealing with zero or missing denominators; it is in the Data Manipulation (SQL/Python) category for a Data Scientist role.
Context
You work on an ads pixel instrumentation platform. Each pixel emits events throughout the day; some events are missing (not observed) and some are invalid (observed but fails validation).
Tables
pixel_event_daily
One row per (event_date, pixel_id, signal_type).
event_dateDATE — calendar date in UTCpixel_idSTRINGsignal_typeSTRING — e.g.,page_view,purchasevalid_event_countBIGINT — number of valid events recorded that dayinvalid_event_countBIGINT — number of invalid events recorded that daymissing_event_countBIGINT — number of expected-but-missing events that day
Assumptions:
- Treat
valid_event_count,invalid_event_count,missing_event_countas non-negative integers. - “Yesterday” means the most recent complete UTC day.
Task
For yesterday, compute the invalid event percentage for each pixel_id.
- Define:
total_observed = valid_event_count + invalid_event_count(exclude missing from the denominator)invalid_pct = invalid_event_count / total_observed
- If
total_observed = 0, returninvalid_pct = NULL.
Output
Return a result with:
pixel_idinvalid_events(sum over all signal types yesterday)total_observed_events(sum over all signal types yesterday)invalid_pct(a decimal between 0 and 1)
Order by invalid_pct descending (NULLs last).
Invalid event percentage by pixel for a specific day
You are given a daily aggregated table of pixel signal events. Each row records how many events of a given status occurred for a pixel on a date.
Statuses can be:
- 'VALID'
- 'INVALID'
- 'MISSING'
For the date 2025-05-31, compute the invalid event percentage for each pixel, defined as:
invalid_percentage = 100 * (INVALID event_count) / (VALID + INVALID + MISSING event_count)
Return one row per pixel with columns: pixel_id, invalid_percentage (as a percentage, not a fraction).
Table pixel_events_daily
| column | type | constraints |
|---|---|---|
| event_id | INT | PRIMARY KEY |
| pixel_id | INT | NOT NULL |
| event_date | DATE | NOT NULL |
| status | VARCHAR(10) | NOT NULL |
| event_count | INT | NOT NULL |
Sample data:
| event_count | event_date | event_id | pixel_id | status |
|---|---|---|---|---|
| 950 | 2025-05-15 | 1 | 101 | VALID |
| 30 | 2025-05-15 | 2 | 101 | INVALID |
| 20 | 2025-05-15 | 3 | 101 | MISSING |
| 900 | 2025-05-31 | 4 | 101 | VALID |
| 50 | 2025-05-31 | 5 | 101 | INVALID |
| 50 | 2025-05-31 | 6 | 101 | MISSING |
| 800 | 2025-05-15 | 7 | 102 | VALID |
| 150 | 2025-05-15 | 8 | 102 | INVALID |
| 50 | 2025-05-15 | 9 | 102 | MISSING |
| 700 | 2025-05-31 | 10 | 102 | VALID |
| 200 | 2025-05-31 | 11 | 102 | INVALID |
| 100 | 2025-05-31 | 12 | 102 | MISSING |
| 450 | 2025-05-15 | 13 | 103 | VALID |
| 10 | 2025-05-15 | 14 | 103 | INVALID |
| 40 | 2025-05-15 | 15 | 103 | MISSING |
| 400 | 2025-05-31 | 16 | 103 | VALID |
| 0 | 2025-05-31 | 17 | 103 | INVALID |
| 100 | 2025-05-31 | 18 | 103 | MISSING |
Expected output
One row per pixel for 2025-05-31, with invalid events divided by total events (valid + invalid + missing), multiplied by 100.
| invalid_percentage | pixel_id |
|---|---|
| 5 | 101 |
| 20 | 102 |
| 0 | 103 |
Hints
- Use conditional aggregation (SUM(CASE WHEN ... THEN ... END)).
- Use NULLIF in the denominator to avoid division by zero.
Show business impact by comparing ROI for high vs low quality pixels
You are given:
- A daily aggregated table of pixel signal events (VALID/INVALID/MISSING).
- A daily table of ad performance by pixel with spend and revenue.
For the date range 2025-05-01 to 2025-05-31 (inclusive):
Step A: Compute per-pixel rates over the whole period:
- invalid_rate = INVALID / (VALID + INVALID + MISSING)
- missing_rate = MISSING / (VALID + INVALID + MISSING)
Step B: Classify each pixel as:
- 'high_quality' if invalid_rate <= 0.05 AND missing_rate <= 0.10
- otherwise 'low_quality'
Step C: Using the classification from Step B, aggregate ad performance over the same date range and return, per quality group:
- quality_group
- total_spend
- total_revenue
- roi = total_revenue / total_spend
This comparison is a simple way to support the claim that higher-quality pixels are associated with better business outcomes.
Table pixel_events_daily
| column | type | constraints |
|---|---|---|
| event_id | INT | PRIMARY KEY |
| pixel_id | INT | NOT NULL |
| event_date | DATE | NOT NULL |
| status | VARCHAR(10) | NOT NULL |
| event_count | INT | NOT NULL |
Sample data:
| event_count | event_date | event_id | pixel_id | status |
|---|---|---|---|---|
| 950 | 2025-05-15 | 1 | 101 | VALID |
| 30 | 2025-05-15 | 2 | 101 | INVALID |
| 20 | 2025-05-15 | 3 | 101 | MISSING |
| 900 | 2025-05-31 | 4 | 101 | VALID |
| 50 | 2025-05-31 | 5 | 101 | INVALID |
| 50 | 2025-05-31 | 6 | 101 | MISSING |
| 800 | 2025-05-15 | 7 | 102 | VALID |
| 150 | 2025-05-15 | 8 | 102 | INVALID |
| 50 | 2025-05-15 | 9 | 102 | MISSING |
| 700 | 2025-05-31 | 10 | 102 | VALID |
| 200 | 2025-05-31 | 11 | 102 | INVALID |
| 100 | 2025-05-31 | 12 | 102 | MISSING |
| 450 | 2025-05-15 | 13 | 103 | VALID |
| 10 | 2025-05-15 | 14 | 103 | INVALID |
| 40 | 2025-05-15 | 15 | 103 | MISSING |
| 400 | 2025-05-31 | 16 | 103 | VALID |
| 0 | 2025-05-31 | 17 | 103 | INVALID |
| 100 | 2025-05-31 | 18 | 103 | MISSING |
Table ad_performance_daily
| column | type | constraints |
|---|---|---|
| perf_id | INT | PRIMARY KEY |
| perf_date | DATE | NOT NULL |
| pixel_id | INT | NOT NULL |
| ad_id | INT | NOT NULL |
| spend | DECIMAL(12,2) | NOT NULL |
| revenue | DECIMAL(12,2) | NOT NULL |
Sample data:
| ad_id | perf_date | perf_id | pixel_id | revenue | spend |
|---|---|---|---|---|---|
| 201 | 2025-05-15 | 1 | 101 | 700 | 400 |
| 201 | 2025-05-31 | 2 | 101 | 900 | 600 |
| 202 | 2025-05-15 | 3 | 102 | 600 | 500 |
| 202 | 2025-05-31 | 4 | 102 | 700 | 700 |
| 203 | 2025-05-15 | 5 | 103 | 350 | 300 |
| 203 | 2025-05-31 | 6 | 103 | 450 | 500 |
Expected output
Two rows (high_quality and low_quality), with aggregated spend/revenue over 2025-05-01 to 2025-05-31 and ROI computed as total_revenue / total_spend.
| quality_group | roi | total_revenue | total_spend |
|---|---|---|---|
| high_quality | 1.6 | 1600 | 1000 |
| low_quality | 1.05 | 2100 | 2000 |
Hints
- Compute per-pixel invalid/missing rates first, then classify into a quality bucket.
- Join the bucketed pixels to ad performance and aggregate spend/revenue by bucket.