Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Science topics
Lesson 54/ 160
03. Mathematics & Statistics

Z-test, T-test & Chi-Square Test

Apply classic tests for means and categorical associations in analytics workflows.

IntermediateZ-test · T-test3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Use a t-test to compare averages, a z-test to compare percentages, and a chi-square test to see whether two categories are linked.

Think of it like…

Three tools in a toolbox: the t-test is the wrench for comparing averages, the z-test for proportions is the same wrench with a larger head when you have lots of data, and chi-square is the gauge for checking whether category counts match what independence would predict.

Where you’ve seen it

Chi-square: of 400 mobile visitors 120 bought, of 600 desktop visitors 90 bought. Under independence the expected purchases are 84 (mobile) and 126 (desktop), and non-purchases 316 and 474. χ² = Σ (O − E)² / E = 15.43 + 4.10 + 10.29 + 2.73 ≈ 32.6 with 1 degree of freedom, so p < 0.001: device and purchase are associated. Welch t-test: checkout time for design A is 38.2 s (s = 9.0, n = 120) and for design B 35.9 s (s = 8.0, n = 100); SE = √(81/120 + 64/100) ≈ 1.147, t ≈ 2.0, p ≈ 0.046: a 2.3-second difference with a 95% CI of roughly (0.04, 4.6) seconds.

Words you’ll hear

One-sample / two-sample / paired t-test
Compare a mean with a target / two independent group means / differences within matched pairs.
Welch's t-test
Two-sample t-test that doesn't assume equal variances; the safe default.
Chi-square test of independence
Compares observed category counts with those expected if variables were independent.
Expected count rule
Chi-square needs expected counts of about 5 or more in most cells.

Visual diagram

Loading interactive diagram

Theory & deep dive

z-tests and t-tests

A z-statistic is (estimate − null) / SE using the normal distribution. For proportions, statsmodels.stats.proportion.proportions_ztest(counts, nobs) compares two rates with a pooled standard error under H₀. t-tests replace the unknown σ by the sample s, giving heavier-tailed reference distributions that converge to the normal as n grows. One-sample: t = (x̄ − μ₀) / (s/√n) with n − 1 df. Independent two-sample (Welch): t = (x̄₁ − x̄₂) / √(s₁²/n₁ + s₂²/n₂) with Welch-Satterthwaite degrees of freedom; stats.ttest_ind(a, b, equal_var=False). Paired: compute each unit's difference and run a one-sample t-test on the differences (stats.ttest_rel(before, after)), which removes between-unit variation and usually gains a lot of power.

Assumptions: independent observations (or properly paired), and approximately normal sampling distributions of the means: fine for large samples by the CLT, but for small samples with strong skew or outliers use a rank-based test (Mann-Whitney U, Wilcoxon signed-rank) or a permutation test. Report the effect size: the mean difference with its confidence interval, or Cohen's d = difference / pooled std (0.2 small, 0.5 medium, 0.8 large as rough guides).

A Welch t-test call

t, p = stats.ttest_ind(checkout_a, checkout_b, equal_var=False) returns t ≈ 2.0 and p ≈ 0.046 for the checkout-time data; add the interval stats.ttest_ind(...).confidence_interval() in recent SciPy versions.

Key takeaway: t-tests for means (Welch by default, paired when matched); z-tests for proportions.

Chi-square tests and how to run them

The chi-square statistic is χ² = Σ (Observed − Expected)² / Expected over all cells. For a test of independence in a contingency table, Expected = (row total × column total) / grand total, with (rows − 1) × (columns − 1) degrees of freedom. For goodness of fit, expected counts come from hypothesised proportions (are the 7 weekdays equally busy?). Use chi2, p, dof, expected = stats.chi2_contingency(table); for 2×2 tables SciPy applies Yates' continuity correction by default (correction=False disables it). Conditions: counts (not percentages), independent observations, and expected counts of at least about 5 per cell; with small expected counts use Fisher's exact test (stats.fisher_exact).

Interpretation: a significant chi-square says the variables are associated, not how strongly or in which direction; inspect the standardised residuals or the table, and compute an effect size such as Cramér's V or the difference in proportions (30% vs 15% purchase rate). For comparing two conversion rates the z-test for proportions and a 2×2 chi-square without continuity correction are equivalent (χ² = z²). With very large samples, associations of trivial size become significant, so report the effect with its interval.

Chi-square on the device-by-purchase table

table = [[120, 280], [90, 510]]; chi2, p, dof, exp = stats.chi2_contingency(table, correction=False) gives chi2 ≈ 32.6, dof = 1, p < 0.001; expected counts [[84, 316], [126, 474]] are all above 5, so the test is valid.

Key takeaway: Chi-square on counts with adequate expected values; report effect size, not just p.

How it works, step by step

Step 1 of 4
Frame

1. Identify the outcome type and design

Is the outcome numeric, binary or categorical, and are the groups independent or paired?

checkout time, two designs, independent
Key numbers at this step
Pairs
change the test

When to use it & trade-offs

What it is

Three classic tests cover most everyday comparisons. The z-test compares means (or proportions) using the normal distribution when σ is known or n is large; the t-test compares means when σ is estimated from the sample, in three forms: one-sample (mean vs a target), independent two-sample (use Welch's version, which doesn't assume equal variances) and paired (before/after on the same units). The chi-square test examines categorical data: independence between two variables (is purchase related to device?) or goodness of fit to expected proportions. Each produces a test statistic, a p-value and an effect size you should report alongside it.

Why it matters: Choosing the correct test for the data and design is half of applied statistics: a paired design analysed as two independent groups wastes power, a proportion analysed with a t-test is awkward, and a chi-square on tiny expected counts is invalid. Knowing the three tests, their assumptions and their scipy calls lets you answer most "is this difference real?" questions correctly.

When to reach for it

  • Comparing groups in experiments and observational data
  • Checking whether two categorical variables are associated
  • Validating claims against targets

When to avoid or be careful

  • Treating paired data as independent
  • Chi-square on percentages or tiny expected counts
  • Reporting only p-values without effect sizes

Test selection

Welch's t-test
Best for: Two independent groups, numeric outcome

Strengths: Robust to unequal variances and sizes.

Watch out: Assumes roughly normal means.

Paired t-test
Best for: Before/after or matched measurements

Strengths: Removes between-unit noise; more power.

Watch out: Needs a true pairing.

Chi-square / z for proportions
Best for: Categorical and binary outcomes

Strengths: Direct and standard.

Watch out: Needs counts and adequate expected values.

Interview signals & numbers to know

When this topic comes up

  • “Which statistical test should I use?”
  • “Compare conversion between two groups”
  • “Are device and purchase related?”
  • Welch vs Student t-test
  • Fisher's exact test

Benchmarks & rules of thumb

32.6 -
χ² for the device-by-purchase table (df = 1)
t ≈ 2.0 · p ≈ 0.046 -
Welch test on checkout time
≥ 5 -
rule-of-thumb expected count per cell

Common mistakes & follow-ups

Common interview mistakes

  • Running an independent-samples t-test on the same users measured before and after.
  • Feeding percentages into chi2_contingency instead of counts.
  • Using Student's t-test (equal variances) when group sizes and spreads differ.

Likely interviewer follow-ups

  • When would you use a paired t-test?
  • Why is Welch's t-test usually preferred?
  • What are the assumptions of a chi-square test?
Interview tip: Start your answer with the design (independent or paired) and the outcome type. Those two facts decide the test.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Z-test, T-test & Chi-Square Test with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Type I & Type II Errors & Statistical Power
Next topic
ANOVA (Analysis of Variance)