Skip to content
Tutorials Center
My learning

Topics

Lesson 72 of 76 ยท Practice and Review

Data Science Interview Questions

40 data science interview questions with model answers: statistics, machine learning, SQL and pandas, case-style problems and behavioural questions.

A data science interview usually checks four things: whether your statistics is sound, whether you can build and judge a model, whether you can get the data out of tables, and whether you can think through an open-ended business problem and talk about it clearly. This page collects 40 questions that come up again and again, grouped in those areas, each with a model answer you can adapt. Three small code puzzles let you check your reading of SQL, pandas and model behaviour.

Try to answer each question out loud before opening it. A good answer is short and has a fixed shape: the direct answer, the reason, a tiny example, and a caveat or trade-off. If you cannot explain an idea in a minute to someone who has never heard of it, go back to the linked lesson.

flowchart LR
    A["Direct answer<br/>(one sentence)"] --> B["Why<br/>(the mechanism)"]
    B --> C["Example<br/>(numbers or code)"]
    C --> D["Trade-off or caveat"]
    D --> E["Ask if they want<br/>more depth"]

A reliable shape for answering a technical question in an interview.

Tip: if a question is vague ("how would you predict demand?"), ask a clarifying question first: what decision will the number support, what data exists, and how will success be measured? Interviewers often judge the questions you ask as much as the answers you give.

Statistics and probability

1. What is the central limit theorem, and why does it matter?

If you take many samples of size n from a population with finite variance, the distribution of the sample means approaches a normal distribution as n grows, whatever the shape of the population. It matters because it lets us build confidence intervals and tests for means using the normal or t distribution even for skewed data. Caveat: the more skewed or heavy-tailed the data, the larger n must be, and the theorem says nothing about the distribution of the raw values. See Probability and Distributions You Will Meet.

2. What does a p-value mean?

It is the probability of seeing data at least as extreme as ours if the null hypothesis were true. It is not the probability that the null is true, and not the probability that the result will replicate. A small p-value says the data would be surprising under "no effect"; whether the effect is large enough to matter is a separate question for the effect size. See Hypothesis Testing Done Right.

3. How do you interpret a 95 percent confidence interval?

It comes from a procedure which, repeated over many samples, would capture the true parameter in 95 percent of cases. For one computed interval, we do not say "there is a 95 percent chance the truth is inside" in the frequentist view; we say the method is reliable 95 percent of the time. In practice, read it as the range of effect sizes compatible with the data, and judge whether the whole range is practically important. See Estimation and Confidence Intervals.

4. Explain type I and type II errors and power.

A type I error is a false positive (rejecting a true null; its rate is alpha, often 0.05). A type II error is a false negative (missing a real effect; its rate is beta). Power is 1 - beta: the chance of detecting a real effect of a given size. Power grows with sample size, effect size and alpha, and falls with noise. Plan power before an experiment, not after. See A/B Testing and Experiments.

5. You ran 20 tests and one gave p = 0.03. Is it a finding?

Probably not by itself. With 20 independent tests of true nulls, you expect about one p-value below 0.05 by chance (the chance of at least one is around 64 percent). Correct for multiple comparisons (Bonferroni, Holm, or control the false discovery rate), or treat the result as a hypothesis to confirm on new data. Better still, decide the primary test before looking.

6. What is the peeking problem in A/B tests?

If you check the results repeatedly and stop as soon as p < 0.05, the false positive rate is much higher than 5 percent, because you gave chance many opportunities to cross the line. Fix the sample size in advance and look once, or use a sequential method designed for repeated looks, with corrected thresholds.

7. Correlation is not causation: how do you get closer to causation?

A correlation can come from a confounder (a third variable driving both), reverse causation, or chance. A randomized experiment breaks confounding by design. When you cannot randomize, you adjust for known confounders (stratification, matching, regression), use natural experiments or difference-in-differences, and state clearly what remains uncertain. See Causal Thinking and Confounding.

8. What is Simpson's paradox?

A trend that appears in several groups can reverse when the groups are combined, because group sizes differ. Example below in puzzle 2: women are admitted at a higher rate in each department, yet at a lower rate overall, because they applied mostly to the department with the lower admission rate. The lesson: always look at the data by the relevant subgroup before drawing a conclusion from an overall number.

9. When is the median better than the mean?

When the data is skewed or has outliers (incomes, house prices, response times), the median describes the typical case and is not dragged by extreme values. The mean is still right when you care about totals (revenue per customer times customers) or when the data is roughly symmetric. Often report both, plus a percentile or two. See Descriptive Statistics in Practice.

10. What is the bootstrap and when would you use it?

You resample your data with replacement many times, compute your statistic on each resample, and use the spread of those values as an estimate of its uncertainty. It gives intervals for statistics with no easy formula (median, ratio, a model metric) and makes few distributional assumptions. It cannot fix a biased or tiny sample, and it is wrong for some statistics such as the maximum.

11. A test is 95 percent accurate for a disease affecting 1 percent of people. A person tests positive. What is the chance they are sick?

It depends on the base rate. Taking "95 percent accurate" as 95 percent sensitivity and specificity, among 10,000 people about 95 sick people test positive and about 495 healthy people also test positive, so a positive result is right only about 16 percent of the time. Explain it with natural frequencies, as in Common Data Science Mistakes.

Machine learning

12. Explain the bias-variance trade-off.

A model's error has a part from being too simple to capture the pattern (bias, underfitting) and a part from being too sensitive to the particular training sample (variance, overfitting), plus irreducible noise. More flexible models lower bias and raise variance. You look for the flexibility where validation error is lowest, using regularization, more data, simpler models or ensembles. Puzzle 3 below shows it with polynomials.

13. How do you detect and reduce overfitting?

Detect it by a large gap between training and validation scores, or validation error that rises while training error falls. Reduce it with more data, simpler models, regularization, early stopping, fewer or better features, ensembles (bagging), and by tuning with cross-validation. Make sure the validation setup itself is not leaking.

14. What is data leakage? Give examples.

Leakage is when training uses information that will not be available at prediction time. Examples: a feature recorded after the outcome (a cancellation survey in a churn model), fitting a scaler or selecting features on all the data before splitting, random splits of time series that let the model see the future, and duplicate rows across train and test. Symptoms: a suspiciously high score. Prevention: pipelines, time-based splits and the question "would I know this at prediction time?". See Train/Test Splits and Cross-Validation.

15. Why use cross-validation instead of one train/test split?

A single split gives one noisy estimate that depends on which rows landed in the test set. K-fold cross-validation averages K estimates and shows their spread, using all the data for both training and validation. Use stratified folds for imbalanced classes and time-ordered folds for time series. Keep a final untouched test set for the last check.

16. Define precision, recall and F1. When does each matter?

Precision = true positives / predicted positives (how many alarms are real). Recall = true positives / actual positives (how many real cases we catch). F1 is their harmonic mean. Favor recall when missing a case is costly (disease screening, fraud), precision when false alarms are costly (blocking legitimate customers, sending a human to investigate). See Evaluating Models with the Right Metrics.

17. What does ROC AUC mean? When is the PR curve better?

AUC is the probability that a randomly chosen positive example gets a higher score than a randomly chosen negative one (0.5 is chance, 1.0 is perfect). It does not depend on a threshold. With heavy class imbalance, ROC curves can look good even when precision is poor, because the false-positive rate is computed over a huge number of negatives; the precision-recall curve is more informative there.

18. How do you handle class imbalance?

First, pick metrics that fit (precision, recall, PR AUC, cost), not accuracy. Then options: class weights, resampling (undersampling the majority or oversampling the minority, only inside training folds), better features, and threshold tuning based on the cost of errors. Collecting more minority examples is often the most effective step. Avoid evaluating on resampled data.

19. L1 vs L2 regularization?

Both add a penalty on the size of the coefficients to the loss. L2 (ridge) shrinks all coefficients smoothly and helps with correlated features. L1 (lasso) can shrink some coefficients to exactly zero, so it performs feature selection. Features usually need to be on the same scale for the penalty to be fair.

20. Random forest vs gradient boosting?

A random forest builds many deep trees independently on bootstrap samples with random feature subsets and averages them, which mainly reduces variance; it is robust and needs little tuning. Gradient boosting builds shallow trees one after another, each correcting the errors of the previous ones, which reduces bias and often achieves higher accuracy, but it is more sensitive to hyperparameters and can overfit noisy data. See Decision Trees, Random Forests and Boosting.

21. Which models need feature scaling?

Models that use distances or gradient steps or penalties: k-nearest neighbors, k-means, SVMs, PCA, neural networks, and regularized linear models. Tree-based models do not, because splits depend only on the order of values. Always fit the scaler on training data only, inside the pipeline.

22. How would you choose the evaluation metric for a project?

Start from the decision and its costs. What does a false positive cost compared with a false negative? Is the target a number (MAE if all errors matter equally, RMSE if large errors are worse, percentage errors if scale varies) or a class? Is a ranking enough (AUC) or do we need calibrated probabilities (log loss, calibration curve)? Always compare with a simple baseline and report in business terms where you can.

23. How do you explain a model's prediction to a stakeholder?

Use the simplest tool that is faithful: coefficients for linear models, permutation importance and partial dependence for global behavior, examples of individual predictions, and error analysis by segment. State the limits: importance does not mean causation, and correlated features share credit. See Explaining Model Behaviour.

24. How would you handle missing values?

First understand why they are missing (completely at random, depending on other variables, or depending on the missing value itself). Then choose: drop rows if few and random, impute with a median or group statistic, add a "was missing" indicator, or use a model that handles missing values natively. Fit imputers on training data only. Never fill silently without recording how many values were filled. See Handling Missing Values.

SQL and pandas

25. What is the difference between WHERE and HAVING?

WHERE filters rows before grouping; HAVING filters groups after aggregation, so it can use aggregate functions such as COUNT(*) > 10. Filtering early with WHERE is cheaper because less data is grouped.

26. Explain inner, left, right and full joins, and the "fan-out" trap.

An inner join keeps matching rows only. A left join keeps all rows of the left table and fills non-matches with NULL (right is the mirror, full keeps both sides). The fan-out trap: joining on a key that is not unique on the other side multiplies rows, so sums and counts are inflated. Always check row counts before and after a join, and aggregate to the right grain first.

27. How do you get the top N rows per group?

Use a window function: rank rows with ROW_NUMBER(), RANK() or DENSE_RANK() over PARTITION BY group ORDER BY value DESC, then filter on the rank. The choice decides what happens with ties. In pandas, groupby(...).rank(...) or groupby(...).head(n) after sorting. Puzzle 1 shows both.

28. What is the difference between COUNT(*), COUNT(column) and COUNT(DISTINCT column)?

COUNT(*) counts rows; COUNT(column) counts rows where the column is not NULL; COUNT(DISTINCT column) counts distinct non-NULL values. NULL handling is a favorite source of wrong answers.

29. In pandas, what is the difference between agg, transform and apply after a groupby?

agg returns one row per group; transform returns a result with the same length as the input (for example a group mean attached to each row); apply runs arbitrary code per group and is the most flexible but slowest. Ask first what shape of output you need. See Pandas Interview Questions for more.

30. How would you find and handle duplicates?

Decide what a duplicate means (a whole identical row, or the same business key such as order id). Use duplicated(subset=...) to inspect them before dropping. Decide which copy to keep (first, latest, the most complete) and record how many rows were removed. Sometimes duplicates are legitimate (two real identical purchases).

31. A CSV is too large for memory. What do you do?

Options in order: read only the needed columns and set smaller dtypes; read in chunks and aggregate each chunk; load into a database such as SQLite or DuckDB and let SQL do the work; use a columnar format such as Parquet; and finally move to a distributed engine if the data truly requires it. See Loading Data from CSV, JSON and SQL.

Case-style questions

32. Daily orders dropped 10 percent yesterday. How do you investigate?

Work from cheap checks to deep ones. (1) Is the data right? Check for pipeline delays, missing partitions or a logging change. (2) Is it real? Compare with the same weekday in previous weeks and think about holidays and seasonality. (3) Where is it? Split by platform, country, channel, product and new vs returning customers: a drop concentrated in one slice points to a cause. (4) What changed? Releases, price changes, outages, campaigns, competitors. (5) Report what is known, the likely cause and the next step. The key is a structured search, not guessing.

33. Design an A/B test for a new checkout page.

Define the primary metric (checkout completion rate) and guardrail metrics (refunds, page speed, revenue per visitor). Choose the unit of randomization (user, not session). Estimate the baseline rate and the smallest effect worth detecting, then compute the sample size for 80 percent power. Run for whole weeks to cover weekly patterns, check that the groups are balanced, do not peek, and analyze with a test and an interval. Decide in advance what result leads to which action. See A/B Testing and Experiments.

34. How would you start a project to predict customer churn?

Clarify the decision (who gets a retention offer, and what does it cost?), define churn precisely (no purchase in 90 days? subscription cancelled?) and the prediction horizon. Build a table of customers as of a past date with features computed only from data before that date and a label from the period after it, which prevents leakage. Start with a baseline and a logistic regression, evaluate with precision and recall at the threshold the business can afford, then explain the drivers and propose a test of the retention action. See Project: Customer Churn Analysis.

35. How would you measure whether a new feature is successful?

Tie it to the goal the feature was meant to serve: pick one primary metric (adoption is not enough; look at retention or task success), a few guardrails, and a comparison group (a randomized holdout if possible; otherwise a careful before-and-after or difference-in-differences). Separate novelty effects from lasting ones by watching over time, and look at segments to see who benefits.

36. How many pizzas are sold in a city per day? (an estimation question)

The goal is clear structure, not the exact number. Break it down: population (say 1 million), share who eat pizza in a given week (say 40 percent), servings per person who do (1 per week), so 400,000 pizzas per week or about 57,000 per day; then sanity-check against per-restaurant numbers (500 pizzerias at about 100 pizzas a day gives 50,000). State assumptions aloud, compute with round numbers, and cross-check with a second route.

Behavioural questions

37. Tell me about an analysis you got wrong.

Pick a real, modest example and tell it in four parts: the situation, what went wrong (be specific and take ownership), how you found out, and what you changed in your process (a validation check, a peer review, a test). Interviewers want honesty and learning, not a disguised success story.

38. How do you explain a technical result to a non-technical audience?

Start with the decision and the finding in one sentence, then one supporting chart, then the uncertainty in plain words ("we are fairly sure the real lift is between 2 and 6 percent"), then the recommendation and the risks. Avoid jargon and keep the technical details in an appendix. See Communicating Results to Non-Experts.

39. A stakeholder wants an answer that the data does not support. What do you do?

Listen to the real need behind the request, show what the data does and does not support, offer the strongest honest statement ("this suggests X, but we cannot rule out Y"), and propose how to get a better answer (more data, an experiment). Do not bend the analysis, and do not simply say no; offer an alternative.

40. You have too many requests and limited time. How do you prioritize?

Estimate impact (value of the decision it supports) and effort, check what blocks others, ask the requester about deadlines and what they would do with the answer, and make the queue visible. Quick wins with real decisions behind them come first; "interesting questions" with no decision attached come last.

Three code-reading puzzles

Decide what each program prints before you run it.

Puzzle 1: top two earners per department

Sales has a tie at the top. Which rows does rnk <= 2 keep, and does DENSE_RANK or ROW_NUMBER matter? (SQL and pandas agree.)

Python Puzzle 1: top N per group in SQL and pandas

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Eli and Fay tie for first place in Sales, both have rank 1, and Dara is rank 2, so three Sales rows are returned. With ROW_NUMBER() the tie would be broken arbitrarily and only two Sales rows would appear; RANK() would give Dara rank 3 and drop her. Which behaviour is right depends on the business question, and saying so is the answer interviewers look for.

Puzzle 2: who is admitted more often?

Python Puzzle 2: Simpson's paradox

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Overall, men are admitted at 50 percent and women at 20 percent. Yet in department A women are admitted at 70 percent against 60, and in B at 14.4 percent against 10. Most women applied to department B, which admits few people from everyone. A comparison of the two overall rates answers a different question from "is there bias in each department's decisions".

Puzzle 3: more flexible, better?

Python Puzzle 3: train and test error vs model complexity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The training error keeps falling as the polynomial degree grows (0.420, 0.249, 0.174, 0.142), but the test error is lowest around degree 3 (0.306) and explodes for degree 9 (19.0) and 15 (974.7), where the curve passes through the 25 training points and swings wildly between them. A model that looks better on the data it has seen can be far worse on new data. That is overfitting, and the gap between the two columns is how you spot it.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is a good general shape for answering a technical interview question?

    A short direct answer followed by mechanism, example and caveat shows understanding and keeps you in control of the depth.
  2. 2. A join on a non-unique key doubles your revenue total. What happened?

    Joining to a table with several rows per key multiplies the left rows. Aggregate to the right grain before joining and compare row counts.
  3. 3. Training error 0.14, test error 975. What does this suggest?

    A tiny training error with a huge test error means the model memorized the training data and generalizes terribly.
  4. 4. You peek at an A/B test daily and stop when p < 0.05. What is the problem?

    Repeated looks with a fixed threshold inflate the type I error. Fix the sample size in advance or use sequential methods.
  5. 5. Which metric view is usually more informative under heavy class imbalance?

    Accuracy can be high for a useless model, and ROC curves can look good because negatives dominate. Precision and recall focus on the rare class.

Summary

  • Answer in a fixed shape: direct answer, reason, example, caveat; clarify vague questions first.
  • Statistics: know p-values, intervals, power, multiple comparisons, peeking, confounding and Simpson's paradox.
  • Machine learning: know bias-variance, leakage, cross-validation, metrics for imbalance, regularization and ensembles.
  • SQL and pandas: WHERE vs HAVING, joins and fan-out, window functions for top N, NULL behaviour, groupby shapes.
  • Case questions are about structure: define the metric, split the problem, check data quality first, say what you would do next.
  • Behavioural answers need a specific story, ownership and what you changed afterwards.

More practice: databases, Python and statistics questions, machine learning and deep learning questions and coding interview questions.

Next: Data Science Cheat Sheet, a one-page reference for the workflow, metrics, tests and code snippets.