Skip to content
Tutorials Center
My learning

Topics

Lesson 73 of 76 ยท Practice and Review

Interview Questions: Databases, Python and Statistics

More data science interview questions with runnable answers: lifecycle, Python data structures, SQL and databases, probability axioms, skewness, p-values, ANOVA.

The main interview page covers the questions that come up in almost every interview. This page goes one level wider: the follow-up questions about Python data structures, databases, visualization tools and the statistics behind the tests. Interviewers like these because a short answer quickly shows whether you only used a tool or understood it.

Each answer is short on purpose. Open a question only after you have tried to answer it aloud, and run the examples to see the claims for yourself.

The data science lifecycle

1. What are the stages of the data science lifecycle?

A common version has six loops rather than a straight line: frame the question (what decision will change?), collect data, clean and explore it, model, evaluate and communicate, and deploy and monitor. The key point is that you move backwards often: a surprising chart sends you back to the data, a weak model sends you back to feature creation, and monitoring in production may send you back to the question itself. Most of the calendar time goes to data work, not to modelling. See The Data Science Workflow and Framing the Question.

Python for data work

2. What is the difference between a list and a tuple?

A list is mutable (you can append, remove and assign), a tuple is immutable. Because a tuple cannot change, it is hashable when its items are, so it can be a dictionary key or a set member; a list cannot. Use tuples for fixed records such as (latitude, longitude) and lists for collections that grow. Tuples are also a little smaller and faster to create. Caveat: a tuple that contains a list is not hashable, and the inner list can still be changed.

3. Why is a NumPy array preferred over a list for numeric work?

A list stores pointers to separate Python objects; an array stores raw numbers of one dtype in one contiguous block. That gives three advantages: much lower memory, vectorized operations that run in compiled loops instead of Python bytecode, and arithmetic that means what you expect (array * 2 doubles each element, while list * 2 repeats the list). The price is that all elements share a type (mix 1 and "a" and everything becomes text) and the size is fixed after creation.

Python Lists, tuples and arrays side by side

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 array stores a million 8-byte integers in exactly 8,000,000 bytes. The list needs more than half of that for its pointer table alone, and every integer object lives elsewhere on top. Read more in Linear Algebra for Data Science and the NumPy tutorial.

4. How would you choose between Matplotlib, Seaborn, Plotly and other visualization tools?

Matplotlib is the low-level foundation: total control, verbose code, static images. Seaborn sits on top of it and gives statistical charts (distributions, regressions, category comparisons) in one line with good defaults. Plotly makes interactive charts with hover and zoom for dashboards and web pages. pandas .plot is the fast way to a quick look. Outside Python, Tableau and Power BI suit business users and drag-and-drop dashboards. Choose by audience: exploring alone, use pandas and Seaborn; publishing for a manager, an interactive or polished chart; a paper, Matplotlib for pixel control. Principles come first: Visualization Principles.

Databases and SQL

5. What is the difference between SQL and NoSQL databases?

SQL (relational) databases store data in tables with a fixed schema, relate them with keys and offer joins, transactions and a standard query language. NoSQL is a family: document stores (JSON-like records), key-value, column-family and graph databases. They trade rigid structure and joins for flexible schemas and easier horizontal scaling. Pick SQL when the data is structured, relationships matter and correctness is critical (money, inventory); pick a NoSQL type when the shape varies a lot, the volume is huge or the access pattern is simple lookups. Many systems use both. See SQL for Data Science.

6. What is the difference between a primary key and a foreign key?

A primary key uniquely identifies each row of its own table: unique, never NULL, one per table (it may span several columns). A foreign key is a column whose values must exist as a primary key (or unique key) in another table, so it links rows and enforces referential integrity: you cannot insert an order for a customer that does not exist. The database rejects a violating insert, as the first example below shows.

7. What does ACID stand for?

The guarantees of a transaction: Atomicity (all of its statements happen or none do), Consistency (it moves the database from one valid state to another, so constraints hold), Isolation (concurrent transactions do not see each other's half-finished work) and Durability (once committed, the data survives a crash). The classic example is a money transfer: debit and credit are one unit.

8. What is the logical execution order of a SQL query?

You write SELECT first, but the engine conceptually evaluates FROM (and joins), WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, then LIMIT. That explains why WHERE cannot use a column alias created in SELECT or an aggregate (use HAVING), and why ORDER BY can use an alias.

9. When do you use a subquery and when a CTE?

A subquery is a query nested inside another; it is fine for a one-off value such as "salary above the average". A common table expression (WITH name AS (...)) names the intermediate result first. Advantages: readability (read top to bottom, like steps), reusability (refer to the same result several times in one statement) and recursion (WITH RECURSIVE walks hierarchies such as an org chart, which a plain subquery cannot do). Performance is usually the same; engines may or may not materialise a CTE.

10. What is the difference between normalization and denormalization?

Normalization splits data into tables so each fact is stored once (no repeated customer name in every order row). It prevents update, insert and delete anomalies and saves space, at the cost of joins. Denormalization deliberately repeats data or pre-joins it to make reads fast, which is typical for analytics tables and warehouses; the cost is redundancy that must be kept consistent. OLTP systems lean normalized, reporting layers lean denormalized.

Python Keys, execution order, subquery vs CTE, recursion

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.

Python Atomicity and a first normalization

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.

In the second transfer the credit to account A is applied first, then the debit from B breaks the CHECK. The with con: block rolls back both statements, so the balances stay at 70 and 50. That is atomicity.

Probability and statistics

11. State the axioms of probability.

For any event A in a sample space S: non-negativity, P(A) >= 0; normalization, P(S) = 1 (something must happen); additivity, for mutually exclusive events P(A or B) = P(A) + P(B) (and for countably many disjoint events, the sum of their probabilities). Everything else follows: P(not A) = 1 - P(A), P(A) <= 1, and for overlapping events P(A or B) = P(A) + P(B) - P(A and B). See Probability and Distributions You Will Meet.

12. What are skewness and kurtosis?

Skewness measures asymmetry. Positive skew means a long right tail (income, waiting times: mean above median); negative skew a long left tail; about zero is symmetric. Kurtosis measures tail weight relative to a normal curve. Software usually reports excess kurtosis (kurtosis minus 3): zero for a normal curve (mesokurtic), positive for heavy tails and more outliers (leptokurtic), negative for light tails (platykurtic, such as the uniform distribution). Kurtosis is about extreme values, not about how "pointy" the peak looks.

13. Describe the Bernoulli and chi-squared distributions.

A Bernoulli variable is one yes/no trial: 1 with probability p, 0 with 1 - p. Mean p, variance p(1 - p). A sum of n independent Bernoulli trials is binomial. A chi-squared variable with k degrees of freedom is the sum of k squared independent standard normal variables. It is non-negative, right-skewed, has mean k and variance 2k, and approaches a normal shape as k grows. It underlies the chi-squared tests for independence and goodness of fit and the variance of a normal sample.

14. What is a p-value, and what does it not tell you?

The p-value is the probability, assuming the null hypothesis is true, of a result at least as extreme as the one observed (the short definition is in question 2 of the main page). A useful way to show it is simulation: flip a fair coin 100 times in a computer thousands of times and count how often you see a gap of 10 or more from 50 heads. That fraction is the p-value. It does not give the probability that the null is true, and it says nothing about size: with a huge sample a trivial difference becomes "significant". Always report the effect size and a confidence interval next to it. See Hypothesis Testing Done Right.

Python Skewness, kurtosis, Bernoulli, chi-squared and a simulated p-value

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 exponential sample is strongly right-skewed with heavy tails, the uniform has negative excess kurtosis (-1.2), and the t distribution with 5 degrees of freedom has the heavy tails you see in financial returns. The 95th percentile of chi-squared with 3 degrees of freedom is 7.815, and about 5 percent of the simulated sums of three squared normals exceed it.

15. What is two-way ANOVA?

One-way ANOVA compares the means of groups defined by one factor. Two-way ANOVA uses two factors at once (say, ad design and device type) and tests three things: the main effect of each factor and their interaction (does the effect of the design depend on the device?). It splits the total variation into parts for factor A, factor B, the interaction and the leftover error, then compares each part to the error with an F ratio (variance explained divided by variance unexplained, per degree of freedom). Assumptions: independent observations, roughly normal residuals and equal variances. See Inferential Statistics with Parametric Tests.

16. What is homoscedasticity and why does it matter?

It means the spread of the errors (residuals) is the same at every level of the predictors. The opposite, heteroscedasticity, shows as a funnel: errors grow with the fitted value (typical for money and counts). Ordinary least squares still gives unbiased coefficients, but the standard errors, p-values and confidence intervals become unreliable. Check it with a residual plot or a test such as Breusch-Pagan; remedies are transforming the target (log), weighted least squares, or robust standard errors.

Preparing data

17. How can you impute missing values with a model?

Treat the column with gaps as the target: train a model on the rows where it is known, using the other columns as features, then predict it for the missing rows. This uses relationships in the data and is usually closer than a mean fill. Variants are k-nearest-neighbour imputation and iterative (chained-equation) imputation. Cautions: fit the imputer on the training set only to avoid leakage, add a "was missing" flag column, and remember that predicted values are too tidy, so they understate uncertainty (multiple imputation addresses that). Strategy overview: Missing Values Strategy.

18. What is feature creation and what are common examples?

Building new columns from existing ones so a model sees the signal more directly: ratios (spend per order), date parts (month, weekend flag, days since signup), aggregates (customer's average basket), interactions (price times quantity), flags (has ever purchased), bins and text features (length, keyword counts). Good features encode domain knowledge; always build them from information available at prediction time. See Feature Engineering.

Python Imputing by prediction, a residual spread check and a few new features

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 first block shows the funnel: the residual spread is about the same in both halves when the errors are constant, and more than doubles when the noise grows with x. The model fill beats the mean fill by a wide margin because price depends strongly on area.

Two-way ANOVA by hand

The one calculation worth practising is the ANOVA table itself. This example builds data with real effects for both factors and no interaction, then computes each sum of squares directly.

Python Two-way ANOVA from sums of squares

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.

Both main effects are significant and the interaction is not (p around 0.55), exactly as the data were built. In a real project you would use a library routine, but being able to explain where each F comes from is what an interviewer is listening for.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why can a tuple, but not a list, be used as a dictionary key?

    Dictionary keys must be hashable, which needs a value that cannot change. Lists are mutable and therefore unhashable.
  2. 2. A query uses an alias from SELECT inside its WHERE clause and fails. Why?

    Logically the order is FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY. ORDER BY can use the alias because it runs after SELECT.
  3. 3. Which feature of a common table expression has no equivalent in a plain subquery?

    A recursive CTE can refer to itself, so it can walk hierarchies of unknown depth, such as an org chart.
  4. 4. A distribution has excess kurtosis of -1.2. What does that tell you?

    Negative excess kurtosis (platykurtic) means fewer extreme values than a normal distribution. The uniform distribution has exactly -1.2.
  5. 5. In two-way ANOVA, what does the interaction term test?

    An interaction means the factors do not simply add up: the effect of A changes with B.

Summary

  • The lifecycle is a loop: question, data, cleaning and exploration, model, communication, deployment and monitoring.
  • Lists are mutable, tuples hashable; NumPy arrays are typed, compact and vectorized.
  • Keys link tables, ACID makes transactions safe, normalization stores each fact once, and a CTE is a readable, reusable (and recursive) subquery.
  • Remember the logical SQL order: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT.
  • Probability rests on three axioms; skewness is asymmetry and excess kurtosis is tail weight.
  • A p-value is a probability under the null, never the chance the null is true; two-way ANOVA adds an interaction test; heteroscedasticity breaks standard errors, not the coefficients.
  • Imputing with a model and creating features both use other columns, so keep them inside the training split.

Next: Interview Questions: Machine Learning and Deep Learning, the model-focused follow-up.