Skip to main content

Write min-max scaling and standard scaling for a feature matrix, and show how to apply them to train and test data without leakage. For example, a training column 1, 2, 3 and a test value 4 gives a min-max test value of 1.5, because the minimum and range come from training only.

Coding rounds · Intermediate 2

Short answer

Min-max scaling is (x - min) / (max - min); standard scaling is (x - mean) / std, both per column. Compute the statistics on the training rows only, store them, and reuse them to transform validation, test and live data. Guard against a zero range or zero std. Cost is O(n d).

Deeper answer

Clarify first: X is n by d and the statistics are per column, so use axis=0. Ask whether there are missing values or constant columns, and which model will use the features. Tree models do not need scaling; distance-based and gradient-based models do.

Write two steps, fit and transform. Fit stores mn = X_train.min(axis=0) and mx = X_train.max(axis=0), or mu = X_train.mean(axis=0) and sd = X_train.std(axis=0). Transform applies (X - mn) / (mx - mn) or (X - mu) / sd to any data. A constant column makes the denominator zero, so replace a zero denominator with 1 and the column becomes all zeros.

Leakage is the point of the question. If you fit on all rows, the test rows shape the mean and the range, and the test score looks better than it should. In cross-validation, fit the scaler inside each fold. Test values can fall outside 0 to 1 after min-max scaling, and that is correct. Interviewers watch for fitting on the full dataset, and for fitting a second scaler on the test set.

What a strong answer covers

Answer it out loud in the mock interview about 2.5 min

Follow-ups to expect

Common mistakes

Learn it

Feature engineering

Practice Coding rounds questions

More on this topic

Today's AI turns are used

Lessons, roadmaps, videos, practice cards and the code playground stay free with your account, with no limit.