KatInterview

Rollout Impact: Diff-in-Diff Estimate with Baseline Drift Check

Arrays & HashingAuto-graded · 14 test cases
EasyAuto-gradedArrays & Hashinghashingaggregationstatistics

Last updated

Aggregate per-group, per-window means from daily records, compute a difference-in-differences estimate, and flag baseline drift between two pre-period sub-windows.

Rollout Impact Check

A product team shipped a feature on 2023-10-01. Some users received it (the treatment group) and the others did not (the control group). You are given one record per user per day and must do two things:

  1. Estimate the feature's effect on daily_sessions with a difference-in-differences (DiD) calculation.
  2. Check whether the two groups were already drifting apart before the launch.

Input

records is a list of dictionaries, each shaped like:

{"user_id": int, "date": "YYYY-MM-DD", "group": str, "daily_sessions": float}

Windows

All bounds are inclusive. Dates are ISO strings, so they can be compared directly as strings.

WindowDates
Pre2023-09-24 to 2023-09-30
Post2023-10-01 to 2023-10-07
Early pre2023-09-24 to 2023-09-26
Late pre2023-09-27 to 2023-09-30

Early pre and Late pre together make up Pre, so a pre-period record counts toward the Pre mean and also toward exactly one of the two sub-windows. Records outside all windows are ignored. Records whose group is not exactly "treatment" or "control" are ignored.

Means

The mean for a (group, window) pair is the plain average of daily_sessions over all records (one record is one observation) in that group and window. If a pair has no records, its mean is 0.0.

Task

Write a function that returns a dictionary with two keys.

did_estimate (float):

(mean_treatment_post - mean_treatment_pre) - (mean_control_post - mean_control_pre)

pretrend_violation (bool): let

  • gap_early = mean_treatment_early - mean_control_early
  • gap_late = mean_treatment_late - mean_control_late

Then the flag is True exactly when

abs(gap_late - gap_early) / abs(gap_early) > 0.1

The comparison is strict, so a change of exactly 10% is not a violation. If gap_early is exactly 0, the flag is False and no division is performed.

Output

{"did_estimate": <float>, "pretrend_violation": <bool>}

Constraints

  • Every record has a valid YYYY-MM-DD date, a numeric daily_sessions, and a group string.
  • Use a single linear pass over the records; the list may be large.
  • Group labels are matched exactly (case-sensitive, no trimming).
  • Empty (group, window) cells give a mean of 0.0.
  • did_estimate must be returned as a float, and pretrend_violation as a bool.

Examples

Example 1

Input: analyze_rollout_effect([{'user_id': 1, 'date': '2023-09-24', 'group': 'treatment', 'daily_sessions': 10}, {'user_id': 2, 'date': '2023-09-24', 'group': 'control', 'daily_sessions': 5}, {'user_id': 1, 'date': '2023-09-30', 'group': 'treatment', 'daily_sessions': 10}, {'user_id': 2, 'date': '2023-09-30', 'group': 'control', 'daily_sessions': 5}, {'user_id': 1, 'date': '2023-10-01', 'group': 'treatment', 'daily_sessions': 15}, {'user_id': 2, 'date': '2023-10-01', 'group': 'control', 'daily_sessions': 5}])

Output: {'did_estimate': 5, 'pretrend_violation': False}

Hints

Hint 1

Keep a running (sum, count) for every combination of window and group, instead of storing the records themselves.

Hint 2

A single record can belong to several windows at once: a date in the Pre window also falls in either Early pre or Late pre. Check each window independently for every record.

Hint 3

Write a small mean helper that returns 0.0 for an empty cell, then build both results from those means. Check gap_early against zero before dividing, and use a strict > 0.1 for the flag.