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

ANOVA (Analysis of Variance)

Compare means across multiple groups and follow up with post-hoc contrasts carefully.

AdvancedF-statistic · Groups3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

ANOVA checks whether three or more groups have different averages by comparing how different the groups are from each other with how different people are within each group.

Think of it like…

Judging whether three classes' exam scores really differ: if the class averages are far apart relative to how much scores vary inside each class, the classes differ; if each class's own spread is as large as the gaps between class averages, the gaps could just be noise.

Where you’ve seen it

Three checkout page designs are tested on 30 users each (N = 90) with time-to-purchase as the outcome. The one-way ANOVA gives F(2, 87) = 5.1, p ≈ 0.008: not all means are equal. Effect size η² = SS_between / SS_total = (2 × 5.1) / (2 × 5.1 + 87) ≈ 0.105, so about 10% of the variance in time is associated with design. Tukey's HSD then shows that design C differs from A (adjusted p = 0.006) while B is not distinguishable from either, which tells the team to prefer C over A but not that C beats B.

Words you’ll hear

F statistic
Ratio of between-group to within-group mean squares; large F suggests real differences.
Degrees of freedom
k − 1 between groups and N − k within, for k groups and N observations.
Post-hoc test
Pairwise comparisons after a significant ANOVA with multiplicity control (Tukey HSD).
η² (eta squared)
Share of total variance explained by group membership.

Visual diagram

Loading interactive diagram

Theory & deep dive

How one-way ANOVA works

Total variation SS_total splits into SS_between (how far group means lie from the grand mean, weighted by group size) and SS_within (how far observations lie from their own group mean). Dividing by degrees of freedom gives mean squares: MS_between = SS_between / (k − 1) and MS_within = SS_within / (N − k). If all group means are equal, both estimate the same noise variance and F ≈ 1; if means differ, MS_between grows and F becomes large. The p-value comes from the F distribution with (k − 1, N − k) degrees of freedom. With two groups, ANOVA's F equals the square of the pooled t statistic.

In Python: stats.f_oneway(a, b, c) for the one-way test, or statsmodels (ols("time ~ C(design)", data=df).fit() followed by anova_lm) for tables and extensions such as two-way ANOVA with interactions. Report F with both degrees of freedom, the p-value and an effect size (η² or partial η²).

A one-way ANOVA in code

F, p = stats.f_oneway(times_a, times_b, times_c) returns F ≈ 5.1 and p ≈ 0.008 for the page-design data; pg.anova or statsmodels also give η².

Key takeaway: F compares between-group to within-group variation; report degrees of freedom and effect size.

Assumptions, post-hoc tests and alternatives

ANOVA assumes independent observations, approximately normal residuals within groups (fairly robust with balanced groups and moderate n) and equal variances (check with Levene's test or look at group standard deviations). With unequal variances use Welch's ANOVA; with heavy skew, outliers or ordinal outcomes use Kruskal-Wallis (stats.kruskal). Repeated measures on the same users need repeated-measures ANOVA or a mixed model, and several factors (design × device) need two-way ANOVA, where interactions (the design effect differs by device) are often the interesting part.

A significant F only says that not all means are equal. To learn which pairs differ, run a post-hoc procedure that controls the family-wise error rate: Tukey's HSD (statsmodels.stats.multicomp.pairwise_tukeyhsd) for all pairwise comparisons, Dunnett's test when comparing every group with a control, or Bonferroni/Holm-adjusted t-tests. If the overall F is not significant, don't go hunting for pairwise differences. Plan comparisons in advance when possible (planned contrasts are more powerful), and interpret in terms of effect sizes and confidence intervals for differences, not just p-values.

Tukey HSD follow-up

print(pairwise_tukeyhsd(df.time, df.design)) lists mean differences, adjusted p-values and CIs for A–B, A–C and B–C, flagging A–C as the only significant pair at α = 0.05.

Key takeaway: Check assumptions, follow a significant F with adjusted post-hoc tests, and use Welch or Kruskal-Wallis when needed.

How it works, step by step

Step 1 of 4
Design

1. Frame the comparison

Confirm you have one numeric outcome and three or more independent groups; list planned comparisons.

time-to-purchase by design A/B/C
Key numbers at this step
Paired?
use repeated measures

When to use it & trade-offs

What it is

ANOVA (analysis of variance) tests whether the means of three or more groups are all equal. It compares the variation between group means with the variation within groups: F = MS_between / MS_within. A large F (small p-value) says at least one mean differs, but not which ones; post-hoc tests (Tukey's HSD, Bonferroni-adjusted t-tests) then identify the differing pairs while controlling the false-positive rate. Assumptions: independent observations, roughly normal residuals and similar variances across groups (check with Levene's test); alternatives include Welch's ANOVA and the rank-based Kruskal-Wallis test.

Why it matters: Comparing three variants of a page, four regions, or five customer segments with repeated pairwise t-tests inflates false positives: three comparisons at α = 0.05 give a 1 − 0.95³ ≈ 14% chance of a spurious finding. ANOVA answers the overall question once, at the right error rate, and its framework extends to regression with categorical predictors and to experiments with several factors. Interpreting it correctly (including effect size and post-hoc follow-up) is a standard analytics skill.

When to reach for it

  • Comparing several page or pricing variants
  • Comparing metrics across regions or segments
  • Quality control across machines or batches

When to avoid or be careful

  • Many unadjusted pairwise t-tests
  • Post-hoc comparisons after a non-significant F
  • Ignoring unequal variances and group sizes

Comparing several groups

One-way ANOVA
Best for: Means across three or more groups with similar variances

Strengths: One test at the right error rate; extends to regression.

Watch out: Says only that some means differ.

Welch's ANOVA / Kruskal-Wallis
Best for: Unequal variances / skewed or ordinal data

Strengths: Robust to assumption violations.

Watch out: Less power if ANOVA assumptions actually hold.

Repeated pairwise t-tests (unadjusted)
Best for: Nothing: avoid

Strengths: Simple.

Watch out: Inflates false positives (14% with 3 comparisons).

Interview signals & numbers to know

When this topic comes up

  • “Compare three or more groups”
  • “F-test”
  • “Post-hoc tests”
  • Kruskal-Wallis
  • Interaction effects

Benchmarks & rules of thumb

k − 1 and N − k -
ANOVA degrees of freedom (2 and 87)
≈ 14% -
false-positive chance with 3 unadjusted comparisons
≈ 0.10 -
η² in the example

Common mistakes & follow-ups

Common interview mistakes

  • Running three separate t-tests and reporting each at α = 0.05.
  • Concluding "all designs differ" from a significant F.
  • Using Student's ANOVA with wildly different group variances and sizes.

Likely interviewer follow-ups

  • Why not just use several t-tests?
  • What does a significant ANOVA tell you, and what doesn't it?
  • What would you do if the variances are unequal?
Interview tip: Mention the 14% inflation for three t-tests. It's the quick, convincing reason ANOVA exists.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply ANOVA (Analysis of Variance) with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Z-test, T-test & Chi-Square Test
Next topic
A/B Testing & Experiment Design