Mid-level interview questionAsked at Perplexity · interview write-up
Train, evaluate and iteratively improve a scikit-learn binary classifier, handling preprocessing, validation and class imbalance.
Model answer
Put preprocessing inside the pipeline, so cross-validation fits imputers and scalers on training folds only.
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import (
StratifiedKFold, cross_val_score)
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import (
OneHotEncoder, StandardScaler)
prep = ColumnTransformer([
("num", make_pipeline(SimpleImputer(strategy="median"),
StandardScaler()), num_cols),
("cat", OneHotEncoder(handle_unknown="ignore"),
cat_cols),
])
model = make_pipeline(
prep, LogisticRegression(class_weight="balanced",
max_iter=1000))
cv = StratifiedKFold(5, shuffle=True, random_state=0)
scores = cross_val_score(model, X, y, cv=cv,
scoring="average_precision")
From this baseline, try gradient boosting and tune with the same CV. For imbalance, class weights or threshold tuning beat blind oversampling. Report PR-AUC, plus precision and recall at a threshold chosen on validation data, then read the errors.
Practise the rest
35 Training questions, 492 in all, each with a model answer. Free.
More from Perplexity interview questions.
More Training interview questions
- Implement masked cross-entropy in NumPy that stays numerically stable, with label smoothing and a temperature.
- Build a pipeline that finds math documents in web crawls and scores their quality.
- Explain KL divergence for language models: which direction is which, what happens when supports don't match, and how it's used as a regulariser.
- When would you use mean squared error versus cross-entropy? And what is LoRA?
- Explain Adam and its bias correction, and why AdamW decouples weight decay.
- Explain policy types in RL and modern policy-gradient methods such as TRPO, PPO and GAE, then compare linear attention and grouped-query attention.