Skip to content

Interview questions

Chapter 47 9 code blocks 2835 words

How this chapter is built

Every question has three things: a weak answer, a strong answer, and the chapter where it's covered.

The weak answers are memorised definitions. The strong answers rest on measurement. That's exactly where the difference shows in an interview.

python4 lines
import pandas as pd

def questions(rows):
    show(pd.DataFrame(rows))

Fundamentals

python18 lines
questions([
    {"Question": "What is overfitting?",
     "Weak answer": "The model is good on train and bad on test",
     "Strong answer": "That + 'I detect it with a learning curve: if the gap between train and validation widens, it's overfitting; if both are low, it's underfitting'",
     "Chapter": "11"},
    {"Question": "What's the difference between bias and variance?",
     "Weak answer": "Bias is systematic error, variance is spread",
     "Strong answer": "That + 'I train the same model on different slices of train and measure how much its predictions differ'",
     "Chapter": "11, 19"},
    {"Question": "How much data do you need?",
     "Weak answer": "The more the better",
     "Strong answer": "'I build a learning curve: once the curve flattens, new data won't help, and I need to change the model or the features'",
     "Chapter": "11"},
    {"Question": "Why three sets: train/val/test?",
     "Weak answer": "That's the convention",
     "Strong answer": "'I choose hyperparameters on val and look at test ONCE. If I make a decision by looking at test, it turns into val and I no longer have an honest estimate'",
     "Chapter": "6"},
])
QuestionWeak answerStrong answerChapter
What is overfitting?The model is good on train and bad on testThat + 'I detect it with a learning curve: if the gap between train and validation widens, it's overfitting; if both are low, it's underfitting'11
What's the difference between bias and variance?Bias is systematic error, variance is spreadThat + 'I train the same model on different slices of train and measure how much its predictions differ'11, 19
How much data do you need?The more the better'I build a learning curve: once the curve flattens, new data won't help, and I need to change the model or the features'11
Why three sets: train/val/test?That's the convention'I choose hyperparameters on val and look at test ONCE. If I make a decision by looking at test, it turns into val and I no longer have an honest estimate'6

Metrics

python18 lines
questions([
    {"Question": "When is accuracy useless?",
     "Weak answer": "On imbalanced data",
     "Strong answer": "'If 99% are negative, a model that says no to everyone gets 99% accuracy and is useless. So I compute the baseline accuracy first: if the model doesn't beat it, there is no model'",
     "Chapter": "7, 8"},
    {"Question": "How do you choose between precision and recall?",
     "Weak answer": "It depends on the task",
     "Strong answer": "'By the COST of the mistake. In disease screening an FN is expensive: recall. In a spam filter an FP is expensive (an important email gets lost): precision'",
     "Chapter": "8, 9"},
    {"Question": "ROC-AUC vs PR-AUC?",
     "Weak answer": "Both are the area under a curve",
     "Strong answer": "'Under strong imbalance ROC-AUC looks optimistic, because the TN count is large. If the rare class matters, I take PR-AUC'",
     "Chapter": "9"},
    {"Question": "Does the threshold have to be 0.5?",
     "Weak answer": "Yes, it's standard",
     "Strong answer": "'No. It's just predict()'s default. I choose it on validation according to the business goal'",
     "Chapter": "9"},
])
QuestionWeak answerStrong answerChapter
When is accuracy useless?On imbalanced data'If 99% are negative, a model that says no to everyone gets 99% accuracy and is useless. So I compute the baseline accuracy first: if the model doesn't beat it, there is no model'7, 8
How do you choose between precision and recall?It depends on the task'By the COST of the mistake. In disease screening an FN is expensive: recall. In a spam filter an FP is expensive (an important email gets lost): precision'8, 9
ROC-AUC vs PR-AUC?Both are the area under a curve'Under strong imbalance ROC-AUC looks optimistic, because the TN count is large. If the rare class matters, I take PR-AUC'9
Does the threshold have to be 0.5?Yes, it's standard'No. It's just predict()'s default. I choose it on validation according to the business goal'9

Leakage: the most frequently asked topic

python14 lines
questions([
    {"Question": "What is data leakage?",
     "Weak answer": "Test data getting into train",
     "Strong answer": "'Information reaching the model that WON'T EXIST at prediction time. It comes in several forms; the most dangerous is the one you can't tell from the column name'",
     "Chapter": "6"},
    {"Question": "What kinds of leakage do you know?",
     "Weak answer": "Mixing test into train",
     "Strong answer": "'Fitting a scaler before the split; computing target encoding on all the data; doing feature selection outside the Pipeline; splitting a time series at random; two rows of the same object landing in train and in test'",
     "Chapter": "6, 23, 35, 38"},
    {"Question": "How do you detect leakage?",
     "Weak answer": "When the result is too good",
     "Strong answer": "'If the result comes out higher than expected, I look at feature importance. If one column dominates, I ask myself: does this column exist at prediction time? Then I remove it and measure how far the result drops'",
     "Chapter": "6, 26, 45"},
])
QuestionWeak answerStrong answerChapter
What is data leakage?Test data getting into train'Information reaching the model that WON'T EXIST at prediction time. It comes in several forms; the most dangerous is the one you can't tell from the column name'6
What kinds of leakage do you know?Mixing test into train'Fitting a scaler before the split; computing target encoding on all the data; doing feature selection outside the Pipeline; splitting a time series at random; two rows of the same object landing in train and in test'6, 23, 35, 38
How do you detect leakage?When the result is too good'If the result comes out higher than expected, I look at feature importance. If one column dominates, I ask myself: does this column exist at prediction time? Then I remove it and measure how far the result drops'6, 26, 45

Models

python18 lines
questions([
    {"Question": "Random Forest vs Gradient Boosting?",
     "Weak answer": "Both are ensembles of trees",
     "Strong answer": "'RF: parallel, independent trees; it reduces variance. Boosting: sequential, each fixes the previous one's mistakes; it reduces bias and is more prone to overfitting'",
     "Chapter": "19, 20"},
    {"Question": "Which model do you start with?",
     "Weak answer": "XGBoost, it's the strongest",
     "Strong answer": "'First a baseline (DummyClassifier), then logistic regression or a single tree. They're fast and understandable. A complex model only when the simple one isn't enough'",
     "Chapter": "7"},
    {"Question": "When do you need deep learning?",
     "Weak answer": "Everyone uses it now",
     "Strong answer": "'For images, text, audio, sequences. On tabular data gradient boosting usually wins. This book MEASURED that on tabular data a neural network didn't beat simple models'",
     "Chapter": "30-36"},
    {"Question": "Which models need scaling?",
     "Weak answer": "All of them",
     "Strong answer": "'The ones that rely on distance and gradients: KNN, SVM, neural networks, regularised linear models. Trees don't need it: they split on thresholds'",
     "Chapter": "17, 21, 22"},
])
QuestionWeak answerStrong answerChapter
Random Forest vs Gradient Boosting?Both are ensembles of trees'RF: parallel, independent trees; it reduces variance. Boosting: sequential, each fixes the previous one's mistakes; it reduces bias and is more prone to overfitting'19, 20
Which model do you start with?XGBoost, it's the strongest'First a baseline (DummyClassifier), then logistic regression or a single tree. They're fast and understandable. A complex model only when the simple one isn't enough'7
When do you need deep learning?Everyone uses it now'For images, text, audio, sequences. On tabular data gradient boosting usually wins. This book MEASURED that on tabular data a neural network didn't beat simple models'30-36
Which models need scaling?All of them'The ones that rely on distance and gradients: KNN, SVM, neural networks, regularised linear models. Trees don't need it: they split on thresholds'17, 21, 22

Practical questions

python18 lines
questions([
    {"Question": "What do you do with missing values?",
     "Weak answer": "Fill them with the median",
     "Strong answer": "'First I check WHY they're missing: at random or systematically. If the gap itself is a signal, I add a flag column. The imputer goes INSIDE the Pipeline, otherwise it's leakage'",
     "Chapter": "5"},
    {"Question": "How do you encode a categorical column?",
     "Weak answer": "With LabelEncoder",
     "Strong answer": "'OneHot if there are few categories. Target encoding if there are many, but it MUST be computed on train only. LabelEncoder invents an order, which is wrong for unordered categories'",
     "Chapter": "5, 23"},
    {"Question": "How do you handle imbalanced classes?",
     "Weak answer": "With SMOTE",
     "Strong answer": "'First I fix the metric (F1, PR-AUC). Then class_weight. SMOTE only on train, inside the Pipeline, and with the result measured: it doesn't always help'",
     "Chapter": "9"},
    {"Question": "A model got worse in production. Why?",
     "Weak answer": "It needs retraining",
     "Strong answer": "'First I find the cause: did the input distribution change (drift), is the preprocessing chain identical, was the offline estimate free of leakage. Retraining comes AFTER the cause is known'",
     "Chapter": "11, 40, 41"},
])
QuestionWeak answerStrong answerChapter
What do you do with missing values?Fill them with the median'First I check WHY they're missing: at random or systematically. If the gap itself is a signal, I add a flag column. The imputer goes INSIDE the Pipeline, otherwise it's leakage'5
How do you encode a categorical column?With LabelEncoder'OneHot if there are few categories. Target encoding if there are many, but it MUST be computed on train only. LabelEncoder invents an order, which is wrong for unordered categories'5, 23
How do you handle imbalanced classes?With SMOTE'First I fix the metric (F1, PR-AUC). Then class_weight. SMOTE only on train, inside the Pipeline, and with the result measured: it doesn't always help'9
A model got worse in production. Why?It needs retraining'First I find the cause: did the input distribution change (drift), is the preprocessing chain identical, was the offline estimate free of leakage. Retraining comes AFTER the cause is known'11, 40, 41

Deployment and engineering

python18 lines
questions([
    {"Question": "How do you save a model?",
     "Weak answer": "joblib.dump(model)",
     "Strong answer": "'I save the whole Pipeline, not just the model. Next to it I put a manifest: versions, expected columns, metrics. And I run a save-and-load round-trip test'",
     "Chapter": "40"},
    {"Question": "Is pickle safe?",
     "Weak answer": "Yes, it's the standard library",
     "Strong answer": "'No. Loading a pickle EXECUTES CODE. I never load a file a user sent. In an uncontrolled environment I use ONNX: it's data, not code'",
     "Chapter": "40"},
    {"Question": "Where do you load the model in an API?",
     "Weak answer": "Inside the view",
     "Strong answer": "'At module level, once. Loading inside the view reads from disk on every request and multiplies memory. This book measured the difference'",
     "Chapter": "41"},
    {"Question": "Where do you validate the input?",
     "Weak answer": "The model will raise an error itself",
     "Strong answer": "'At the boundary, before the model sees it. Type, range, allowed values. Because the model may not raise an error on bad input at all: it SILENTLY gives a wrong answer'",
     "Chapter": "38, 40, 41"},
])
QuestionWeak answerStrong answerChapter
How do you save a model?joblib.dump(model)'I save the whole Pipeline, not just the model. Next to it I put a manifest: versions, expected columns, metrics. And I run a save-and-load round-trip test'40
Is pickle safe?Yes, it's the standard library'No. Loading a pickle EXECUTES CODE. I never load a file a user sent. In an uncontrolled environment I use ONNX: it's data, not code'40
Where do you load the model in an API?Inside the view'At module level, once. Loading inside the view reads from disk on every request and multiplies memory. This book measured the difference'41
Where do you validate the input?The model will raise an error itself'At the boundary, before the model sees it. Type, range, allowed values. Because the model may not raise an error on bad input at all: it SILENTLY gives a wrong answer'38, 40, 41

Trick questions

python18 lines
questions([
    {"Question": "My model got 99% accuracy. What do you say?",
     "Weak answer": "A very good result",
     "Strong answer": "'First two questions: what's the class distribution, and what does the baseline get? Then a third: is there leakage? 99% is more often a sign of a mistake than of success'",
     "Chapter": "6, 7, 8"},
    {"Question": "Feature importance says a column matters. So it's the cause?",
     "Weak answer": "Yes",
     "Strong answer": "'No. Importance says the model used that column, not that it's a cause. A cause needs an experiment'",
     "Chapter": "26, 46"},
    {"Question": "Cross-validation gave 0.85. Will test be the same?",
     "Weak answer": "Yes",
     "Strong answer": "'Roughly, if the CV was built correctly. But if I used CV many times to choose the model, the CV result will be optimistic'",
     "Chapter": "10, 24"},
    {"Question": "Which algorithm is the best?",
     "Weak answer": "XGBoost / a neural network",
     "Strong answer": "'It depends on the data; you can't say in advance. That's why I set a baseline and measure several models under the same conditions'",
     "Chapter": "7, 20, 25"},
])
QuestionWeak answerStrong answerChapter
My model got 99% accuracy. What do you say?A very good result'First two questions: what's the class distribution, and what does the baseline get? Then a third: is there leakage? 99% is more often a sign of a mistake than of success'6, 7, 8
Feature importance says a column matters. So it's the cause?Yes'No. Importance says the model used that column, not that it's a cause. A cause needs an experiment'26, 46
Cross-validation gave 0.85. Will test be the same?Yes'Roughly, if the CV was built correctly. But if I used CV many times to choose the model, the CV result will be optimistic'10, 24
Which algorithm is the best?XGBoost / a neural network'It depends on the data; you can't say in advance. That's why I set a baseline and measure several models under the same conditions'7, 20, 25

The question they ask you to ask

python9 lines
questions([
    {"Question": "What questions do you have for us?",
     "Weak answer": "None, everything's clear",
     "Strong answer": "'How do you evaluate your models, and how do you monitor them in production?': this question shows your experience",
     "Chapter": "-"},
])
print("In an interview, the strongest answer is to say 'I MEASURED this'")
print("and explain how you measured it. That's why every chapter in this book")
print("ends with a measurement: so you have something to say.")
Output
In an interview, the strongest answer is to say 'I MEASURED this'
and explain how you measured it. That's why every chapter in this book
ends with a measurement: so you have something to say.
QuestionWeak answerStrong answerChapter
What questions do you have for us?None, everything's clear'How do you evaluate your models, and how do you monitor them in production?': this question shows your experience-

Questions about this chapter?

The course, homework review and the other five books are in Uzbek. Questions in English are welcome on Telegram.

Write on Telegram Contents