Data Preprocessing — ML Technical Interview Questions & Answers
Cleaning, encoding and scaling data before training.
Data Preprocessing
- Simple meaning: Raw sheets are messy. Preprocessing is cleaning before train: fill or drop missing marks, fix “Pass ” with a space, turn city names into numbers, scale hours and income so one column does not dominate.
- Picture: Column hours is 1–10. Column fees is 20,000–80,000. KNN will think fees is “more important” only because the numbers are bigger. Scale both (StandardScaler / MinMax) on train, then apply the same scaler to test.
- Typical steps: drop junk rows → handle NA → encode categories (one-hot or label) → scale numeric columns → then fit the model.
- Trap: fitting the scaler on train+test together (leak). Trap: one-hot encoding after the split in a way that test gets new columns the model never saw — plan categories on train.
Technical Interview Tip: Clean → encode → scale on train. Transform test with the same fit.
Code Examples for Data Preprocessing
Data Preprocessing — working example
xs = [10, 20, 30, 40]
mean = sum(xs) / len(xs)
var = sum((x - mean) ** 2 for x in xs) / len(xs)
std = var ** 0.5
z = [(x - mean) / std for x in xs]
lo, hi = min(xs), max(xs)
minmax = [(x - lo) / (hi - lo) for x in xs]
print([round(v, 3) for v in z])
print(minmax)
# sklearn.preprocessing.StandardScaler fits mean/std on train onlyData Preprocessing — scaling keeps features comparable — critical for distance-based models and many optimizers.
How to Explain Data Preprocessing to an Interviewer
Why scale?
So a big column doesn’t dominate distance models KNN, SVM, gradient descent care
Where do you fit the scaler?
Train only Then transform test