State the data grain, assumptions, metric, leakage or failure risk, and how you would validate the result before discussing tools.
Question set
10 detailed answers
01What is data leakage? Give three concrete mechanisms.
middle
Short answer: Leakage means information unavailable at real prediction time seeps into training. Validation metrics look great; in production the model falls apart.
In depth:
- Preprocessing fit on the full dataset before the split — a scaler, imputer, feature selection, or SMOTE fit on train+test has already "seen" the test distribution.
- Features from the future or post-outcome — churn_reason, aggregates over the customer's entire history: the value is only known after the target happens.
- Duplicates and linked rows across splits — the same user in train and test: the model memorizes them instead of generalizing.
# Wrong: scaler fit before the split
X = StandardScaler().fit_transform(X)
X_tr, X_te, y_tr, y_te = train_test_split(X, y)
# Right: everything inside a Pipeline, fit on train only
pipe = Pipeline([("scaler", StandardScaler()),
("clf", LogisticRegression())])
pipe.fit(X_tr, y_tr)
Fix: all preprocessing inside a Pipeline fit on train folds only; time-aware splits for temporal data, GroupKFold for repeated users.
⚠️ Common mistake: saying "I split the data and train" without stating "split BEFORE preprocessing" — that ordering is exactly what the interviewer is probing.
02How do you encode categorical features, and what drives the choice of method?
middle
Short answer: There is no universal encoding: the choice is driven by the model family and the feature's cardinality. One-hot for linear models and low cardinality, label encoding only for trees, target encoding and hashing for high cardinality.
In depth:
| Encoding | When it fits | Main risk |
|---|---|---|
| One-hot | linear models, low cardinality | dimensionality explosion at thousands of categories |
| Label / ordinal | trees and boosting | imposes a false order on linear models and kNN |
| Target (mean) encoding | high cardinality | target leakage without out-of-fold |
| Frequency / counters | trees, quick baseline | distinct categories with equal frequency collide |
| Hashing trick | online learning, huge vocabularies | collisions, not reversible |
| Embeddings | neural nets, very many categories | needs data and training |
Two questions before choosing: (1) is the model sensitive to order and distances — a false order hurts linear and metric models, trees don't care; (2) how many unique values — at thousands of categories one-hot blows up the matrix, so target/hashing/embeddings.
⚠️ Common mistake: label encoding for logistic regression — the model starts believing that the city coded 17 is "greater than" the city coded 5.
03Target encoding: why is it dangerous and how do you apply it safely?
senior
Short answer: Target encoding replaces a category with the target's mean over that category — i.e. it puts the target straight into a feature. The naive version (mean over the full dataset) leaks and inflates the CV score. The safe scheme: out-of-fold encoding + smoothing toward the global mean + optionally noise.
In depth:
- The leak mechanism — a row's own target participates in its category's mean; rare categories nearly memorize the answer.
- Out-of-fold — encoding values for a fold are computed only on the other folds; for the test set, on the whole train.
- Smoothing (regularization) — shrinkage toward the global mean: enc = (n·mean_cat + m·mean_global) / (n + m); rare categories are pulled to the global mean instead of their own 2–3 observations.
- CatBoost — ordered target statistics: encodes each row using only "earlier" rows within a permutation — the same idea out of the box.
for tr, val in KFold(5).split(df):
means = df.iloc[tr].groupby("cat")["y"].mean()
df.loc[df.index[val], "cat_te"] = df.iloc[val]["cat"].map(means)
⚠️ Common mistake: proposing target encoding without saying "out-of-fold" — to the interviewer that flags a candidate this leak has never actually burned in practice.
04Which models need feature scaling and why?
junior
Short answer: Scaling matters for models that rely on distances, dot products, or gradient descent: kNN, SVM, k-means, PCA, regularized linear models, neural nets. Trees, forests, and boosting don't need it — they are invariant to monotone transforms.
In depth:
| Model | Scaling | Why |
|---|---|---|
| kNN, k-means, SVM | required | distances: a feature in meters drowns out one in kilometers |
| PCA | required | variance: components chase the feature with the largest scale |
| Linear + L1/L2 | required | the weight penalty depends on feature scale |
| Neural nets | needed | gradient descent convergence |
| Trees / RF / boosting | not needed | threshold splits; value order doesn't change |
And the key point: the scaler is part of the model. Fit on train only, transform valid/test — otherwise test statistics leak into training.
⚠️ Common mistake: scaling everything "just in case" before the split — a harmless habit turns into leakage; and conversely, wasting time scaling for XGBoost.
05How do you handle missing values, and when is a missing value itself information?
junior
Short answer: First understand the missingness mechanism, then pick the method: median/mean, model-based imputation, a separate category, plus a "was missing" indicator column. Boosting handles NaN natively. If missingness depends on the value itself (MNAR), it carries signal — you can't just paint over it.
In depth:
| Mechanism | Meaning | Example |
|---|---|---|
| MCAR | missing completely at random | sensor glitch |
| MAR | depends on other observed features | younger users skip the phone field more often |
| MNAR | depends on the value itself | income left blank by those with very high or low income |
In practice:
- Median/mean/mode — quick baseline; median is robust to outliers.
- Missingness indicator — an is_missing column preserves the MNAR signal.
- Model-based imputation (kNN, iterative) — more accurate, more expensive; fit on train only.
- Native handling — XGBoost/LightGBM/CatBoost learn which split branch to route NaN into.
⚠️ Common mistake: "drop the rows with missing values" as the whole answer: you lose data and bias the sample wherever missingness isn't MCAR.
06How do you detect and treat outliers — and when should you NOT remove them?
middle
Short answer: Detect with IQR, z-score (for roughly normal data), isolation forest for multivariate cases. But the first question is not "how do I delete them" — it's "is this an error or a signal": in fraud and anomaly detection the outliers ARE the target.
In depth:
- Diagnose — a data error (height 250 cm from a typo, price −1) or a real rare event (a customer spending 100×)?
- Errors — fix or remove, logging the reason.
- Real extremes — don't delete; reduce their influence:
| Technique | What it does |
|---|---|
| Log / Box-Cox | compresses a heavy right tail |
| Winsorizing | clipping at the 1st/99th percentile |
| Robust losses (MAE, Huber) | the model is penalized less by extremes |
| RobustScaler | scaling by median and IQR |
- Clipping bounds and statistics are computed on train — otherwise yet another leak.
⚠️ Common mistake: auto-"cleaning" outliers in a fraud or anomaly task — throwing away exactly the objects the model is supposed to catch.
07Feature selection: filter vs wrapper vs embedded — and where does leakage hide?
middle
Short answer: Filter methods score features independently of the model (correlation, mutual information), wrappers search subsets by training the model (RFE), embedded methods select during training itself (L1, tree importances). Critically: selection is part of training and must live inside the CV loop — otherwise the estimate is inflated by leakage.
In depth:
| Class | Examples | Pros | Cons |
|---|---|---|---|
| Filter | correlation, mutual info, χ² | cheap, scales | ignores feature interactions |
| Wrapper | RFE, forward/backward | accounts for the model and interactions | expensive: model × subsets |
| Embedded | L1/lasso, boosting importances | free during training | tied to the model family |
Where the leak is: picking top features by correlation with the target on ALL the data and then cross-validating — the features were already chosen while peeking into the test folds, so CV tells a fairy tale. Correct: the selector inside a Pipeline, re-selected on every train fold.
⚠️ Common mistake: "first I'll select features, then run an honest cross-validation" — the selection itself was already dishonest.
08Why is the built-in feature importance from boosting and forests unreliable, and what do you use instead?
senior
Short answer: Impurity/gain importances are biased toward continuous and high-cardinality features (more split candidates), computed on train (overfit splits look "important"), and correlated features split importance arbitrarily. More reliable: permutation importance on validation, and SHAP.
In depth:
| Method | Computed on | Weak spot |
|---|---|---|
| Impurity / gain | train | high-cardinality bias; a noise feature like an id can top the chart |
| Permutation | validation | correlated features get understated: the model falls back on the twin |
| SHAP | any set | honest contribution decomposition, but costlier; correlations still smear the picture |
In practice:
- Permutation on a held-out set — importance as the metric drop when a column is shuffled.
- SHAP — per-object contributions plus global aggregation.
- Cluster correlated features (correlation-based clustering) and assess groups, not single features.
⚠️ Common mistake: reading feature_importances_ as truth and using it to drop features or draw business conclusions.
09Multicollinearity: what exactly breaks, for which models, and what do you do about it?
middle
Short answer: Highly correlated features make linear-model coefficients unstable: huge standard errors, signs flipping from sample to sample. Predictive quality barely suffers — it's interpretation that breaks. Diagnose with VIF; treat by dropping/combining features, ridge, or PCA.
In depth:
- What breaks — the XᵀX matrix is near-singular → coefficient estimates have huge variance; you cannot read feature "importance" off a coefficient.
- Who hurts — linear and logistic regression as inference tools. Trees and boosting barely care for accuracy, but importances get smeared across duplicate features.
- Diagnosis — VIF_j = 1/(1−R²_j) from regressing feature j on the rest; VIF > 5–10 is a red flag; plus the correlation matrix.
- Treatment — drop or combine duplicates (sum, ratio), ridge (L2 stabilizes the estimates), PCA into orthogonal components — at the price of interpretability.
⚠️ Common mistake: "multicollinearity ruins predictions" — no, the forecast is usually fine; what suffers is the coefficients, their signs, and confidence intervals.
10Build features for a churn model from raw event logs — walk through the process step by step.
senior
Short answer: Start not with features but with definitions: the prediction date and the label window (e.g. "will they churn within 30 days after date X"). Then as-of-time discipline: every feature is computed only on data strictly before the prediction date. Then windowed aggregates (RFM), trends, tenure — and time-based validation.
In depth:
- Target design — prediction date + label window; one dataset row = (customer, prediction date).
- As-of-time — joins only "as of the date"; no aggregate peeks to the right.
- Windowed aggregates (RFM) — recency (days since last event), frequency (events in 7/30/90 days), monetary (spend within the window).
- Dynamics — deltas and trends: activity in the last 7 days vs the previous 30 — the drop-off is precisely the churn precursor.
- Statics — tenure, plan, number of support contacts.
- Time-based validation — train on early prediction dates, test on later ones; not a random split.
──── event logs ────────────►│ prediction date │──── label window ───►
features: only from here ▲ target: churn here
(7/30/90-day windows, RFM) └─ touch nothing right of this point
⚠️ Common mistake: any feature peeking past the prediction date (an "all-history" aggregate, subscription status at month end) — a model with 0.99 AUC offline and useless in production.
Source notes
References and review policy
RecallDeck’s interview answers are editorial material, reviewed against maintained official documentation where a primary reference is available. Tool selections use direct provider links and contain no affiliate placements. Features can change after the review date.
From reading to recall
Practice the full interview loop.
RecallDeck schedules the concepts you miss and keeps coding, design, and behavioral fundamentals available when the interviewer changes direction.