Implement Out-of-Fold Target Encoding With Smoothing
Last updated: November 29, 2026
Quick Overview
Implement target encoding for a categorical feature such as a Marketplace seller's city so that no row's encoding uses its own label, with smoothing toward the global mean for rare categories.
Meta
November 29, 20260
0
0 solved
Implement target encoding for a categorical feature such as a Marketplace seller's city so that no row's encoding uses its own label, with smoothing toward the global mean for rare categories.
Target encoding is a strong feature for tree models at Meta, but done naively it leaks the label and wins offline only. The interviewer expects a correct leakage-free implementation and an explanation of the smoothing parameter.
What the Interviewer Expects
- Explain the leakage in in-sample target encoding
- Implement k-fold out-of-fold encoding correctly
- Apply additive smoothing toward the prior
- Handle unseen categories with the prior
- Describe how to encode the test set
Key Topics to Cover
How to Approach This
- Understand the bias-variance trade-off. High training accuracy but low test accuracy signals overfitting.
- Choose evaluation metrics carefully based on the problem. Accuracy alone is often insufficient.
- Feature engineering is often more impactful than model selection.
- Know when to use tree-based models (tabular data) vs neural networks (unstructured data).
- Handle class imbalance with SMOTE, class weights, or appropriate loss functions.
Possible Follow-up Questions
- How would you choose the smoothing strength alpha?
- How is this different for time-ordered data?
- Why might target encoding still overfit with many rare categories?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Explore ML Interview PrepSample Answer
Clarify
I would confirm the label is binary (sold or not), whether rows are time ordered (if so, use expanding window encoding instead of random folds), and how many categories have few rows....
Approach
If a category appears once, its in-sample mean equals its own label, a perfect leak. Out-of-fold encoding splits rows into folds and encodes each fold using statistics from the other folds only. S...