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

Sampling Techniques & Sampling Bias

Design samples with random, stratified, and cluster methods while spotting selection bias.

IntermediateRandom Sample · Stratified3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

A good sample gives everyone a fair chance of being picked; a biased sample leaves out or over-counts some kinds of people.

Think of it like…

Tasting soup: stir the pot and take a spoonful (random sample) and you learn about the whole pot. Taste only the top where the salt floats, or only from the bowl your friend served first (convenience), and you'll misjudge it however carefully you taste.

Where you’ve seen it

A company emails a satisfaction survey to 10,000 customers and gets 800 replies (8%). The average rating is 4.5, but responders are mostly delighted or furious customers; silent customers may be neutral. Nothing about n = 800 fixes this non-response bias. A better design draws a stratified sample, 70% small-business and 30% enterprise customers to match the base (350 and 150 for a 500-person target), follows up non-responders through several reminders and weights results back to the customer mix. The famous Literary Digest poll of 1936 collected over two million replies yet predicted the wrong winner because its sample frame (car and telephone owners, magazine subscribers) over-represented wealthier voters.

Words you’ll hear

Sampling frame
The list from which the sample is drawn; gaps cause undercoverage.
Stratified sampling
Dividing the population into groups and sampling within each, to guarantee representation and reduce variance.
Selection bias
Systematic difference between the sample and the population due to how units were chosen or retained.
Non-response bias
Bias because those who respond differ from those who don't.

Visual diagram

Loading interactive diagram

Theory & deep dive

Sampling methods

Simple random sampling selects units so every subset of size n is equally likely (df.sample(n, random_state=42)); it needs a complete frame. Systematic sampling picks every k-th record after a random start (easy, but biased if the list has a periodic pattern). Stratified sampling splits the population into homogeneous strata and samples within each, either proportionally or with oversampling of small groups, then reweights; it reduces variance and ensures rare segments are represented (df.groupby("segment").sample(frac=0.05)). Cluster sampling randomly picks groups (stores, schools, villages) and samples all or some units inside; it's cheaper logistically but has larger variance than simple random sampling of the same size because units within a cluster resemble each other. Multi-stage designs combine these.

Non-probability samples (convenience, quota, snowball, voluntary response) are useful for exploration, qualitative work and pilots, but you can't compute valid sampling errors from them, and inference to the population depends on untestable assumptions. In machine learning, the same logic applies: training data are a sample, and a model trained on one population is only reliable where the deployment population resembles it.

Stratified sampling in pandas

sample = df.groupby("segment", group_keys=False).apply(lambda g: g.sample(n=min(len(g), 150 if g.name == "enterprise" else 350), random_state=1)) guarantees 150 enterprise and 350 small-business customers, then attach design weights (population share ÷ sample share).

Key takeaway: Random methods allow valid inference; stratify to protect small groups; cluster to save cost.

Types of bias and how to reduce them

Undercoverage: the frame misses part of the population (customers without email, users on old app versions). Selection bias: the process of getting into the data favours some units (only active users are logged; only approved loans have repayment outcomes). Non-response bias: responders differ from non-responders. Survivorship bias: only survivors are observed (analysing successful startups, retained customers or planes that returned from missions). Voluntary response: people with strong opinions self-select. Measurement bias: the instrument or question wording distorts answers, and time bias arises when data from one period are applied to another.

Defences: define the target population explicitly; choose or build a frame that covers it; randomise selection; follow up non-responders and compare early and late responders; compare sample demographics with known population margins and post-stratify or weight to fix imbalances; record who was excluded and why; and for models, check training data against the deployment population and monitor drift. When bias can't be removed, state it, test sensitivity ("how large would the bias have to be to change the conclusion?") and avoid over-claiming.

A representativeness check

Sample: 52% mobile, 48% desktop. Site traffic: 68% mobile, 32% desktop. Weight mobile responses by 0.68 / 0.52 ≈ 1.31 and desktop by 0.32 / 0.48 ≈ 0.67 so the weighted sample matches traffic, then compare the weighted and unweighted results to see how much the imbalance mattered.

Key takeaway: Name the biases, compare with population margins, weight, and state what remains.

How it works, step by step

Step 1 of 4
Design

1. Define the population and frame

State exactly who or what you want to learn about and whether your list covers them.

active customers in India, last 12 months
Key numbers at this step
Coverage
gaps?

When to use it & trade-offs

What it is

A sample is the subset you observe; the population is what you want to learn about. Probability sampling gives each unit a known chance of selection: simple random (everyone equally likely), systematic (every k-th unit), stratified (random samples within groups such as customer segments) and cluster (randomly pick whole groups like stores, then sample inside). Non-probability methods (convenience, quota, voluntary response) are cheap but can't support unbiased inference. Sampling bias, including selection, non-response and survivorship bias, systematically distorts results, and more data doesn't fix it.

Why it matters: Every statistic is only as good as the sample behind it. Surveys with 8% response rates, models trained on customers who stayed, and A/B tests that exclude mobile users all produce confident, precise, wrong answers. Understanding how a sample was drawn, and how it differs from the population you care about, matters more than sample size or sophisticated methods.

When to reach for it

  • Customer surveys and market research
  • Auditing and quality checks
  • Designing train/test data that mirror deployment

When to avoid or be careful

  • Treating a large convenience sample as representative
  • Ignoring non-response
  • Analysing only survivors

Sampling methods

Simple random
Best for: Homogeneous populations with a complete frame

Strengths: Unbiased and easy to analyse.

Watch out: May miss small groups; needs a full list.

Stratified
Best for: Populations with distinct segments

Strengths: Guarantees representation; lowers variance.

Watch out: Needs strata information; weights if disproportionate.

Cluster
Best for: Geographically spread populations, costly fieldwork

Strengths: Cheaper and practical.

Watch out: Higher variance per observation.

Convenience / voluntary
Best for: Pilots and exploration

Strengths: Fast and cheap.

Watch out: No valid inference; heavy bias risk.

Interview signals & numbers to know

When this topic comes up

  • “Only 8% responded to the survey”
  • “Is this sample representative?”
  • “Stratified vs cluster sampling”
  • Survivorship bias
  • Training data vs production population

Benchmarks & rules of thumb

350 + 150 -
strata sample sizes in the example
1.31 / 0.67 -
post-stratification weights in the example
2 million -
replies to the 1936 Literary Digest poll, yet biased

Common mistakes & follow-ups

Common interview mistakes

  • Assuming 800 responses make the survey accurate despite an 8% response rate.
  • Evaluating a credit model only on approved applicants and extrapolating to all applicants.
  • Sampling "first 1,000 rows" of a table sorted by date and calling it random.

Likely interviewer follow-ups

  • What's the difference between stratified and cluster sampling?
  • How would you deal with non-response bias?
  • What is survivorship bias? Give an example from data science.
Interview tip: Lead with "who is missing from this sample?". That one question surfaces most sampling problems.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Sampling Techniques & Sampling Bias with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Central Limit Theorem (CLT)
Next topic
Standard Error & Confidence Intervals