KatInterview

Building Non-Leaky User Features and Interpreting Regression Outputs

SQL & DataModel answer
MediumModel answerSQL & DataSQLPandasRegression

Last updated

Aggregate raw event data into daily user-level features without target leakage using SQL and Python, then fit regression models and interpret coefficients correctly.

You are analyzing user interaction events from an web application to evaluate the effect of an experimental feature assignment (variant) on conversion.

You are given a raw event table named user_metrics:

+---------+-------------+---------+--------+-----------+
| user_id | activity_dt | variant | clicks | purchases |
+---------+-------------+---------+--------+-----------+
| 101     | 2023-05-01  | A       | 12     | 1         |
| 102     | 2023-05-01  | B       | 4      | 0         |
| 103     | 2023-05-02  | A       | 6      | 1         |
| 104     | 2023-05-02  | B       | 9      | 2         |
| 105     | 2023-05-03  | A       | 3      | 0         |
+---------+-------------+---------+--------+-----------+

Tasks

  1. SQL Feature Engineering: Write a SQL query that aggregates raw event data to one row per (user_id, activity_dt) pair. Calculate the target variable converted ($1$ if purchases > 0, else $0$), along with rolling feature calculations that avoid target leakage:

    • Total clicks within the prior 7 calendar days (excluding the current day).
    • Number of prior active days (excluding the current day).
    • Days elapsed since the user's previous active date.
  2. Python Implementation & Interpretation:

    • Write Python code (using pandas and statsmodels) to fit appropriate regression models (e.g., Logistic Regression or OLS).
    • Explain how to interpret the regression coefficients for numeric metrics and categorical predictors.
    • Highlight potential pitfalls in feature selection (e.g., controlling for post-treatment variables in experimental analysis).