KatInterview

Build a Click-Through Rate Model for a News Feed

Machine LearningModel answer
MediumModel answerMachine Learningfeature-engineeringdata-leakagehigh-cardinality

Last updated

Design an end-to-end CTR prediction pipeline covering temporal feature engineering, leakage prevention, high-cardinality encoding, and offline-to-online metric gaps.

Setup

You are building a click-through rate (CTR) prediction model for a personalized news feed. Your dataset has 100 million rows. Each row represents one article impression and contains:

FieldTypeDescription
user_idstringUnique user identifier
article_idstringUnique article identifier
categorystringContent category (e.g., "sports", "tech")
timestampdatetimeTime the article was shown
device_typestringe.g., "mobile", "desktop"
clickedint (0/1)Whether the user clicked

Example row:

{
  "user_id": "u123",
  "article_id": "a456",
  "category": "tech",
  "timestamp": "2023-10-01 10:00:00",
  "device_type": "mobile",
  "clicked": 0
}

What you are asked to address

Part 1 — Feature engineering and leakage

  • How would you convert timestamp into model features? Pay particular attention to capturing cyclical patterns in user behavior.
  • How would you turn article_id into a useful signal?
  • A teammate proposes adding a feature article_avg_ctr, computed as the article's mean click rate over the whole training set. Explain what goes wrong and how you would fix it.
  • As a follow-up: what happens when you apply target encoding to the category column on a small dataset?

Part 2 — Encoding high-cardinality IDs

user_id and article_id each have millions of unique values. Describe a practical encoding strategy for:

  • A Logistic Regression model
  • A Gradient Boosted Decision Tree (GBDT) model

Explain why the same approach does not work equally well for both.

Part 3 — Diagnosing an offline/online gap

Your model reaches a strong AUC-ROC score on the held-out set, but after deployment the click rate in production is disappointing. Walk through how you would diagnose this discrepancy. What metrics and validation strategies would you check, and in what order?


Constraints and expectations

  • Treat the data as a time series: rows are not independent draws from a static distribution.
  • At 100 million rows, memory efficiency matters.
  • For each decision you make, briefly state the trade-off you are accepting.

Hints

Hint 1

For timestamp features, think about what "10:00" means to a model that sees hour as an integer — is 23:00 truly as far from 00:00 as it is from 12:00?

Hint 2

Before computing any article-level aggregate, ask: at prediction time, which rows would you actually have access to? Work backward from that constraint to define your training feature.

Hint 3

AUC measures global pairwise ranking across all users. Think about what question your production metric is really asking — is it the same question?