KatInterview

Rolling ETA median, DiD and CUPED uplift on ride-request data

SQL & DataModel answer
MediumModel answerSQL & Datapostgresqlwindow-functionsdate-spine

Last updated

Build a gap-free daily city series with a 7-day rolling median ETA and conversion in PostgreSQL, then estimate uplift with city-level DiD and a CUPED-adjusted, cluster-robust estimate in Python.

You are analysing a ride-hailing A/B test (experiment_id = 42) with PostgreSQL and a little Python. Treat today as 2025-09-01.

Schema

  • trips(trip_id BIGINT, request_ts TIMESTAMP, city_id INT, rider_id BIGINT, driver_id BIGINT, shown_eta_sec INT, pickup_eta_sec INT, is_completed BOOLEAN, is_canceled BOOLEAN, surge_multiplier NUMERIC, is_treatment BOOLEAN, experiment_id INT)
  • riders(rider_id BIGINT, signup_dt DATE, city_id INT)
  • city_dim(city_id INT, city_name TEXT, tier TEXT)
  • incentives(rider_id BIGINT, offer_start_dt DATE, offer_end_dt DATE, percent_off INT, cap_usd NUMERIC)
  • day_weather(city_id INT, dt DATE, precip_mm NUMERIC, temp_c NUMERIC)

Sample rows

trips
trip_id | request_ts          | city_id | rider_id | driver_id | shown_eta_sec | pickup_eta_sec | is_completed | is_canceled | surge_multiplier | is_treatment | experiment_id
1       | 2025-08-01 08:01:00 | 1       | 101      | 9001      | 420           | 480            | t            | f           | 1.0              | t            | 42
2       | 2025-08-01 08:03:00 | 1       | 102      | 9002      | 360           | 360            | f            | t           | 1.2              | f            | 42
3       | 2025-08-15 18:30:00 | 2       | 103      | 9003      | 300           | 420            | t            | f           | 1.1              | f            | 42
4       | 2025-08-20 09:00:00 | 1       | 101      | 9001      | 330           | 360            | t            | f           | 1.0              | t            | 42
5       | 2025-08-28 22:10:00 | 2       | 104      | 9004      | 600           | 660            | f            | t           | 1.5              | t            | 42

riders
rider_id | signup_dt  | city_id
101      | 2025-05-01 | 1
102      | 2025-01-15 | 1
103      | 2025-06-20 | 2
104      | 2025-03-10 | 2

city_dim
city_id | city_name | tier
1       | Alpha     | T1
2       | Beta      | T2

incentives
rider_id | offer_start_dt | offer_end_dt | percent_off | cap_usd
101      | 2025-08-15     | 2025-08-31   | 20          | 10

Tasks

  1. Daily series (SQL). For each city_id and calendar date from 2025-08-01 to 2025-09-01, return the 7-day rolling median of shown_eta_sec and the request-to-trip conversion rate (completed / requests). Days with no requests must still appear, with conversion NULL. Keep only riders with signup_dt < 2025-07-01. Bucket days by truncating request_ts at UTC midnight, and say how you handle time zones. The intent is a median computed with percentile_cont(0.5) over a 7-day window.
  2. Difference-in-differences (SQL). At city level, compare treated and control conversion for the post period (2025-08-15 to 2025-09-01) against the pre period (2025-08-01 to 2025-08-14). Output city_id, pre_treat_conv, pre_ctrl_conv, post_treat_conv, post_ctrl_conv, did_uplift, where did_uplift = (post_treat_conv - pre_treat_conv) - (post_ctrl_conv - pre_ctrl_conv). A rider who saw both arms must be counted by the exposure of each individual trip.
  3. CUPED (Python, pseudocode acceptable). At rider level, let X be pre-period conversion and Y post-period conversion. Compute theta = cov(Y, X) / var(X) and Y_cuped = Y - theta * (X - mean(X)). Estimate the treated-vs-control uplift on Y_cuped and report its standard error, cluster-robust by rider_id or city_id.

State any assumptions you make (for example, which filters carry over to Tasks 2 and 3), and explain the pitfalls you see.

Hints

Hint 1

Check whether PostgreSQL lets you write percentile_cont(...) WITHIN GROUP (...) OVER (...). If it does not, think about joining each spine row to the trips from the previous 7 calendar days and using it as a plain grouped aggregate.

Hint 2

Fetch 6 extra days of trips before 2025-08-01 so the earliest windows are full, and compute the median over raw trip values, not over daily medians. Medians of medians are not the median of the union.

Hint 3

For DiD, label each trip with its own period and its own is_treatment flag, then use AVG(...) FILTER (WHERE ...) to get the four cells in one pass. For CUPED, decide up front how to treat riders with mixed exposure and riders with no pre-period trips.

Hint 4

In Task 3, fit theta once on the pooled sample, then regress Y_cuped on the treatment indicator with cov_type='cluster'. Think about whether having only two cities makes city-level clustering trustworthy.