Machine Learning · hard

How would you detect data leakage in a pipeline?

Asked in ML Engineer interviews, in the Machine Learning round.

Short answer

Check for features that use future or target information, fit transforms only on the training set, and audit unrealistically high scores.

How to answer it

Definition

Leakage is when information that would not exist at prediction time reaches the model during training. It produces excellent validation scores and a model that fails in production, which is why it is worth catching early.

The tell

A model that is too good. An AUC of 0.99 on a genuinely hard problem is far more likely to be leakage than a breakthrough, and treating it as suspicious rather than as success is most of the skill here.

The common sources

Target leakage       a feature computed from the outcome
                     "days_until_churn", or refund_amount in a fraud model

Temporal leakage     a feature whose value is only known after the prediction
                     time, such as an account status updated at resolution

Preprocessing leak   scaler, imputer or SMOTE fitted on the full dataset
                     before splitting

Group leakage        the same user in both train and test, so the model
                     recognizes the person rather than the pattern

Duplicate rows       near-duplicates split across the two sets

Tuning leakage       the test set used repeatedly for model selection until
                     it no longer measures anything

How to detect it

Key points

FAQ

The 30-second answer

Leakage is information reaching the model that would not be available at prediction time, and the symptom is a model that looks too good. So the first thing I do with a suspiciously high score is treat it as a bug rather than a result.
I would look at feature importances and interrogate whatever dominates, then ask of each feature whether that value would have existed at prediction time, with that value. The classic cases are a feature derived from the target, a field updated after the event, and preprocessing fitted before the split.
Structurally I prevent it by putting every transform inside a scikit-learn Pipeline so it is fitted per fold, using GroupKFold when the same user appears many times, and splitting by time even when the problem is not time series, because a large gap between a random split and a temporal one is usually leakage.

In the interview

Practice this for real