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

Variance, Standard Deviation & IQR

Quantify spread with variance, std, range, and interquartile range for robust summaries.

BeginnerVariance · Std Dev3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Spread says how much the numbers differ from each other: small spread means they're close together, large means they're all over the place.

Think of it like…

Two archers with the same average landing point: one clusters arrows tightly around the target (small spread), the other scatters widely. Spread measures how tight the cluster is; the IQR ignores the few arrows that flew far off.

Where you’ve seen it

Delivery times (minutes) for 8 orders: 2, 4, 4, 4, 5, 5, 7, 9 (a toy set). The mean is 5. Squared deviations sum to 32, so the population variance is 32 / 8 = 4 (std 2) and the sample variance is 32 / 7 ≈ 4.57 (std ≈ 2.14). The quartiles are Q1 = 4 and Q3 = 5.5, so IQR = 1.5 and the outlier fences are 4 − 2.25 = 1.75 and 5.5 + 2.25 = 7.75: the 9-minute delivery is flagged as an outlier by the IQR rule.

Words you’ll hear

Variance
Mean of squared deviations from the mean (÷ n for a population, ÷ n − 1 for a sample).
Standard deviation
Square root of the variance, in the original units.
IQR
Q3 − Q1: the range of the middle half of the data.
Coefficient of variation
std ÷ mean, a unit-free spread measure for positive data.

Visual diagram

Loading interactive diagram

Theory & deep dive

Variance, standard deviation and why n − 1

Variance squares deviations so positive and negative ones don't cancel and large deviations weigh more. Its units are squared (minutes²), so the standard deviation is reported instead. For a sample, the deviations are measured from the sample mean, which sits closer to the data than the true mean does, so dividing by n underestimates the population variance; dividing by n − 1 (Bessel's correction) fixes that bias. With large n the difference is negligible. The convention trap: pandas.Series.std() and var() default to ddof=1 (sample), while numpy.std() and numpy.var() default to ddof=0 (population); np.std(x, ddof=1) matches pandas.

Interpretation: for roughly bell-shaped data about 68 percent of values lie within one std of the mean and 95 percent within two, but for skewed or heavy-tailed data these rules fail, and a few outliers can inflate the std dramatically. The coefficient of variation (std / mean) compares spread across variables with different scales.

Same mean, different consistency

Ratings [4, 4, 4, 4, 4] and [1, 5, 5, 4, 5] both average 4, but their sample standard deviations are 0 and about 1.7. Reporting the std alongside the mean exposes the difference.

Key takeaway: Std is in original units; use n − 1 for samples and mind the pandas/NumPy ddof difference.

IQR, percentiles and outlier fences

Percentiles split sorted data: Q1 (25th), median (50th), Q3 (75th). Different libraries interpolate quartiles slightly differently; NumPy and pandas use linear interpolation by default (np.percentile(x, [25, 75])). The IQR is robust because it ignores the outer quarter on each side; the 1.5 × IQR rule flags values below Q1 − 1.5·IQR or above Q3 + 1.5·IQR as potential outliers (box-plot whiskers use the same rule). The MAD (median absolute deviation) is another robust spread measure, used for robust z-scores.

Choose by purpose: std for symmetric data and for formulas that need it (confidence intervals, z-scores, many tests), IQR or percentiles for skewed data and outlier-prone metrics (latency: report p50, p95, p99). Also check spread within groups: very different variances across groups affect t-tests, ANOVA and regression assumptions. And remember a flagged outlier is a prompt to investigate, not an automatic deletion.

Latency summary

np.percentile(latency_ms, [50, 95, 99]) returns the typical request and the tail experience; a mean of 120 ms with p99 of 2,400 ms signals a heavy tail that the mean and std hide.

Key takeaway: Use IQR and percentiles for skewed data; treat flagged outliers as questions.

How it works, step by step

Step 1 of 4
Prepare

1. Compute the centre

Find the mean (and median) as the reference point for deviations.

mean = 5
Key numbers at this step
Then
deviations

When to use it & trade-offs

What it is

Spread tells you how far values typically lie from the centre. Variance is the average squared deviation from the mean; the standard deviation (std) is its square root, in the data's own units. The range is max − min. The interquartile range (IQR) is Q3 − Q1, the spread of the middle 50 percent, and it's robust to outliers. For samples, divide the sum of squared deviations by n − 1 (not n) to estimate the population variance without bias. Std suits roughly symmetric data; IQR and percentiles suit skewed data.

Why it matters: Two products with the same average rating of 4.0 can be very different: one is consistently 4, another is a mix of 1s and 5s. Spread quantifies risk, consistency and noise, and it's the denominator of z-scores, confidence intervals, effect sizes and many model diagnostics. Mixing up population vs sample formulas (and pandas vs NumPy defaults) is a classic source of small but real discrepancies.

When to reach for it

  • Describing consistency of ratings, times and measurements
  • z-scores, confidence intervals and tests
  • Latency and income reporting with percentiles

When to avoid or be careful

  • Reporting std for heavily skewed data without percentiles
  • Mixing population and sample formulas
  • Deleting flagged outliers without investigation

Measures of spread

Standard deviation / variance
Best for: Roughly symmetric data and inferential formulas

Strengths: Uses every value; basis of z-scores and CIs.

Watch out: Inflated by outliers.

IQR / percentiles
Best for: Skewed data and robust reporting

Strengths: Resistant to outliers; easy to interpret.

Watch out: Ignores tails; less algebra-friendly.

Range / MAD
Best for: Quick checks (range) and robust z-scores (MAD)

Strengths: Simple; MAD is robust.

Watch out: Range depends on two points only.

Interview signals & numbers to know

When this topic comes up

  • “Why divide by n − 1?”
  • “pandas and NumPy give different std”
  • “What is the IQR rule?”
  • Box plots
  • Percentile latency reporting

Benchmarks & rules of thumb

32 / 8 = 4 -
population variance in the example
32 / 7 ≈ 4.57 -
sample variance in the example
1.5 × IQR -
the standard outlier fence

Common mistakes & follow-ups

Common interview mistakes

  • Calling np.std(x) for a sample and getting a std slightly too small compared with pandas.
  • Using std to describe highly skewed latency data.
  • Dropping every point beyond the fences without checking whether it's a real event.

Likely interviewer follow-ups

  • Why do we divide by n − 1 for a sample?
  • When would you use IQR instead of standard deviation?
  • How do you detect outliers with the IQR rule?
Interview tip: Mention the ddof default difference between pandas and NumPy. It's a small detail that marks real hands-on experience.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Variance, Standard Deviation & IQR with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Descriptive Statistics (Mean, Median, Mode)
Next topic
Skewness & Kurtosis