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 51/ 160
03. Mathematics & Statistics

Hypothesis Testing & Null vs Alternative

Frame H0/H1, choose tests, and decide with evidence rather than vibes.

IntermediateH0/H1 · Test Statistic3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

You start by assuming nothing changed, then check whether your data would be very surprising if that were true; if it would, you doubt the assumption.

Think of it like…

A courtroom: the defendant is presumed innocent (H₀) until the evidence is strong enough to convict (reject H₀). Not guilty doesn't mean innocent; it means the evidence wasn't strong enough beyond reasonable doubt (α).

Where you’ve seen it

A delivery company claims an average of 30 minutes. A sample of n = 40 deliveries has mean 32.5 minutes and standard deviation 8. H₀: μ = 30, H₁: μ ≠ 30. The standard error is 8 / √40 ≈ 1.265, so t = (32.5 − 30) / 1.265 ≈ 1.98 with 39 degrees of freedom, giving a two-sided p ≈ 0.055. At α = 0.05 the team fails to reject H₀: the data are suggestive but not conclusive. The matching 95% CI, 32.5 ± 2.56 = (29.9, 35.1), includes 30 and shows plausible averages up to 35 minutes, so they collect more data instead of declaring the claim true.

Words you’ll hear

Null hypothesis H₀
The default claim of no effect or no difference.
Alternative H₁
The claim that there is an effect: two-sided (≠) or one-sided (> or <).
Test statistic
A standardised measure of how far the data are from H₀.
Significance level α
The false-positive rate you accept; chosen before looking at the data.

Visual diagram

Loading interactive diagram

Theory & deep dive

Steps and choosing a test

The workflow: (1) state H₀ and H₁ in terms of parameters; (2) pick α and the sample size before collecting data; (3) choose a test that matches the data type and design; (4) compute the test statistic and p-value; (5) decide; (6) report the effect size and interval, not just the verdict. The test statistic has the form (estimate − null value) / standard error, so it counts how many standard errors the estimate lies from H₀.

Choose the test by the question and data: one mean against a target (one-sample t), two independent means (Welch t), before-and-after on the same units (paired t), two proportions (z-test for proportions), category association (chi-square), three or more means (ANOVA), and non-parametric alternatives (Mann-Whitney, Wilcoxon) when assumptions fail. Decide one- vs two-sided before looking: a two-sided test detects increases and decreases, and switching to one-sided after seeing the direction halves the p-value dishonestly.

The test in scipy

stats.ttest_1samp(delivery_minutes, popmean=30) returns the t statistic and two-sided p-value; use alternative="greater" only if the one-sided hypothesis was fixed in advance.

Key takeaway: Hypotheses and α first, test matched to the design, effect size and CI with every verdict.

Interpreting outcomes and common mistakes

If p ≤ α you reject H₀: the data would be unusual if H₀ were true. If p > α you fail to reject: the data are compatible with H₀, but also with many other values (the confidence interval shows which). "Not significant" can mean there's no effect or that the sample was too small to detect one (low power). Statistical significance is not practical significance: with huge samples trivial differences become significant, and with small ones large effects may not.

Frequent mistakes: p-hacking (trying many tests or subsets and reporting the significant one), choosing hypotheses after seeing the data, ignoring multiple comparisons, using the wrong test for dependent data (treating before/after measurements as independent groups), stopping an experiment when significance appears, and treating α = 0.05 as a law of nature rather than a convention to adapt to the cost of errors. Document the plan, report all tests run, and prefer intervals and effect sizes alongside p-values.

Same data, richer report

"Mean delivery time 32.5 min (95% CI 29.9–35.1, n = 40) vs the 30-min target; t(39) = 1.98, p = 0.055. We can't rule out the target but can't rule out up to 5 minutes over it; recommend collecting 150 more deliveries."

Key takeaway: Fail to reject isn't acceptance; report effects, intervals and the full testing plan.

How it works, step by step

Step 1 of 4
Plan

1. State hypotheses and α

Write H₀ and H₁ in terms of parameters, choose one- or two-sided, and fix α before looking.

H₀: μ = 30 · H₁: μ ≠ 30 · α = 0.05
Key numbers at this step
Before
data

When to use it & trade-offs

What it is

Hypothesis testing asks whether data are consistent with a default claim. The null hypothesis H₀ is the "nothing special" statement (no difference, no effect, the claim is true); the alternative H₁ is what you're looking for (a difference, an effect). You compute a test statistic (such as a t or z value) that measures how far the data are from H₀ in standard-error units, find the probability of a result at least that extreme if H₀ were true (the p-value), and compare with a pre-chosen significance level α (often 0.05) to decide: reject H₀ or fail to reject it. Failing to reject is not proof that H₀ is true.

Why it matters: Teams constantly ask "is this difference real or noise?": did the redesign change conversion, is delivery time really above target, do two groups differ? Hypothesis tests give a disciplined, repeatable answer and expose how much evidence you have. Their misuse (choosing the test after seeing data, treating "not significant" as "no effect") is also the source of many false findings, so the framing matters.

When to reach for it

  • Checking performance claims against targets
  • Comparing groups in experiments and observational data
  • Deciding whether more data is needed

When to avoid or be careful

  • Choosing hypotheses or tails after seeing the data
  • Reading "not significant" as "no effect"
  • Running many tests and reporting only the significant one

Choosing a test

t-tests (one-sample, Welch, paired)
Best for: Comparing means with unknown variance

Strengths: Robust for moderate n; standard.

Watch out: Assumes roughly normal means or large n.

z-test for proportions / chi-square
Best for: Conversion rates and categorical associations

Strengths: Direct for binary and count data.

Watch out: Needs adequate expected counts.

Non-parametric (Mann-Whitney, permutation)
Best for: Skewed data, small samples, ordinal outcomes

Strengths: Few assumptions.

Watch out: Less powerful if parametric assumptions hold.

Interview signals & numbers to know

When this topic comes up

  • “Is the difference real or noise?”
  • “What's the null hypothesis?”
  • “One-sided or two-sided test?”
  • p-hacking
  • Test selection

Benchmarks & rules of thumb

0.05 -
the conventional α
t ≈ 1.98 · p ≈ 0.055 -
the delivery-time example
two-sided -
default tail choice

Common mistakes & follow-ups

Common interview mistakes

  • Switching to a one-sided test after seeing which direction the data went.
  • Reporting "no difference" when the sample was too small to detect one.
  • Treating before-and-after measures of the same users as independent samples.

Likely interviewer follow-ups

  • What do "reject" and "fail to reject" mean?
  • How do you choose between a one-sided and a two-sided test?
  • Why isn't p > 0.05 evidence that there's no effect?
Interview tip: Always finish with the effect size and CI. A verdict with an interval is far more useful than a bare "significant".

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Hypothesis Testing & Null vs Alternative with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Standard Error & Confidence Intervals
Next topic
P-values & Statistical Significance