Train and improve a binary classifier with scikit-learn
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Perplexity's technical screen or assessment, last asked Dec 2025. It tests machine learning, scikit-learn and evaluation.
You get a small labelled tabular dataset (a few thousand rows, a mix of numeric and categorical columns, some missing values, a yes/no label) and a notebook with scikit-learn. Train a binary classifier, report how good it is with a metric you can defend, then improve it step by step while explaining each choice. The interviewer watches how you work as much as the final score.
Interface
Unless you agree on a different setup with the interviewer, assume:
-
df— a pandas DataFrame with feature columns and alabelcolumn of 0 and 1. About 10% of rows are positive. -
build_model() -> sklearn.pipeline.Pipeline— preprocessing and the estimator in one pipeline. -
evaluate(model, df) -> dict[str, float]— your chosen metrics on data the model never saw while training or tuning.
Example
baseline = predict the majority class -> accuracy 0.90, recall 0.00
# accuracy hides that it never finds a positive: report ROC AUC, PR AUC,
# and precision / recall at a chosen threshold instead
model = build_model() # e.g. impute + scale + one-hot -> LogisticRegression
evaluate(model, df) -> {"roc_auc": ..., "pr_auc": ..., "recall@p0.5": ...}
Constraints
- Everything inside a
Pipeline, so preprocessing is fit on training folds only. - Finish a working baseline within the first 15 minutes, then iterate.
Clarifying questions
- What is the model for, and which mistake costs more: a false positive or a false negative?
- How imbalanced is the label, and will the class balance be the same in production?
- Are rows independent, or grouped (same customer) or ordered in time, which changes how to split?
- Are any columns only known after the label (leakage)?
- Is a probability needed (calibrated), or just a ranking?
What a strong answer covers
- A stratified train/test split (or group or time split when needed) held out until the end, and cross-validation on the training part.
- A trivial baseline first, then logistic regression in a pipeline:
SimpleImputer,StandardScaler,OneHotEncoder(handle_unknown="ignore")through aColumnTransformer. - A metric that fits the imbalance (PR AUC, ROC AUC, recall at a fixed precision) and a threshold chosen on validation data, not 0.5 by habit.
- Improvements one at a time with their effect measured: class weights, a gradient boosted model (
HistGradientBoostingClassifier), feature engineering,GridSearchCVorRandomizedSearchCVover a few parameters. - Checking for leakage and overfitting: train versus validation scores, feature importance or permutation importance that looks too good to be true.
Follow-ups
- The scores look excellent. What do you check before you believe them?
- Production has 2% positives instead of 10%. What changes in the metrics and the threshold?
- Calibrate the probabilities, and show how you would check calibration.
- Explain one prediction to a non-technical stakeholder.
Where it's asked
- Perplexity · Technical screen or assessment · Last asked Dec 2025Online assessment or live coding (often CoderPad); problems come in parts, each adding a requirement to your code · Assume no AI unless the recruiter says otherwise
What it tests
machine learning · scikit-learn · evaluation
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
