Beginner

Stats Coding in Interviews

Quant firms like Jane Street, Citadel, Two Sigma, and DE Shaw test whether you can implement statistical methods from scratch. Data science roles at Google, Meta, and Netflix test the same skill. This lesson explains what to expect, why "from scratch" matters, and the patterns that appear in every interview.

Why "From Scratch" Matters

Calling scipy.stats.ttest_ind(a, b) proves nothing about your understanding. Interviewers want to see that you know the formula behind the function. At quant firms, this is non-negotiable: if you cannot derive the t-statistic from its definition, you will not pass the coding round.

Quant Firms

Jane Street, Citadel, Two Sigma, DE Shaw, and Jump Trading expect you to implement statistical tests, probability distributions, and simulation methods without any library calls. They test mathematical fluency through code.

Data Science Roles

Google, Meta, Netflix, and Airbnb ask you to implement A/B test analysis, confidence intervals, and hypothesis tests. They want to see you understand the math, not just the API.

ML Engineering

ML roles at top companies ask you to implement sampling methods, bootstrap procedures, and Bayesian inference. These are the building blocks of uncertainty quantification in production ML.

Research Roles

Research scientist positions at DeepMind, OpenAI, and FAIR test Monte Carlo methods, MCMC samplers, and Bayesian optimization. From-scratch implementation is the baseline expectation.

scipy.stats vs From Scratch

Know both. Use the library version to verify your from-scratch implementation. In interviews, always implement from scratch unless told otherwise.

import math

# ---- THE LIBRARY WAY (not what interviews want) ----
from scipy import stats
t_stat, p_value = stats.ttest_ind(group_a, group_b)
# One line. Zero understanding demonstrated.

# ---- THE FROM-SCRATCH WAY (what interviews want) ----
def ttest_ind_from_scratch(a, b):
    """Two-sample t-test, assuming unequal variances (Welch's t-test)."""
    n_a, n_b = len(a), len(b)
    mean_a = sum(a) / n_a
    mean_b = sum(b) / n_b

    # Sample variances (Bessel's correction: divide by n-1)
    var_a = sum((x - mean_a) ** 2 for x in a) / (n_a - 1)
    var_b = sum((x - mean_b) ** 2 for x in b) / (n_b - 1)

    # Standard error of the difference in means
    se = math.sqrt(var_a / n_a + var_b / n_b)

    # t-statistic
    t_stat = (mean_a - mean_b) / se

    # Welch-Satterthwaite degrees of freedom
    numerator = (var_a / n_a + var_b / n_b) ** 2
    denominator = (var_a / n_a) ** 2 / (n_a - 1) + (var_b / n_b) ** 2 / (n_b - 1)
    df = numerator / denominator

    return t_stat, df

# This shows you understand every component:
# - Why we divide by n-1 (Bessel's correction for sample variance)
# - What the standard error of a difference in means is
# - Why Welch's t-test uses adjusted degrees of freedom
# - The formula for Welch-Satterthwaite approximation
💡
Interview strategy: When asked to implement a statistical method, start by writing the mathematical formula in a comment, then translate each component into code. This shows the interviewer you are working from understanding, not from memory of an API.

Common Interview Patterns

These patterns appear repeatedly across quant, data science, and ML coding interviews. Recognize them and you will solve problems faster.

PatternExample ProblemsKey Skill
Accumulator statisticsRunning mean, online varianceWelford's algorithm, numerical stability
From-scratch distributionsNormal PDF, Poisson PMFMath-to-code translation
Sampling & simulationMonte Carlo pi, option pricingRandom number generation, convergence
Hypothesis testingt-test, chi-squared, permutationTest statistic + p-value computation
Resampling methodsBootstrap CI, permutation testSampling with/without replacement
Bayesian updatingBeta-Binomial, posterior computationPrior * likelihood = posterior
Numerical precisionLog-probabilities, Kahan summationAvoiding overflow/underflow

What Interviewers Evaluate

# Interviewer evaluation rubric for statistics coding:

evaluation_criteria = {
    "mathematical_correctness": {
        "weight": "CRITICAL",
        "description": "Does the implementation match the mathematical definition?",
        "red_flag": "Using n instead of n-1 for sample variance",
        "green_flag": "Correctly applying Bessel's correction and explaining why"
    },
    "from_scratch_ability": {
        "weight": "HIGH",
        "description": "Can the candidate implement without library calls?",
        "red_flag": "Reaching for scipy.stats immediately",
        "green_flag": "Writing the formula first, then coding each component"
    },
    "numerical_stability": {
        "weight": "HIGH",
        "description": "Does the candidate handle edge cases?",
        "red_flag": "Computing variance as E[X^2] - (E[X])^2 (catastrophic cancellation)",
        "green_flag": "Using Welford's algorithm or two-pass approach"
    },
    "edge_case_awareness": {
        "weight": "MEDIUM",
        "description": "Does the candidate consider degenerate inputs?",
        "red_flag": "Division by zero when n=1, empty lists",
        "green_flag": "Checking sample size, handling edge cases gracefully"
    },
    "complexity_analysis": {
        "weight": "MEDIUM",
        "description": "Can the candidate analyze time/space complexity?",
        "red_flag": "O(n^2) when O(n) is possible",
        "green_flag": "Streaming/online algorithms where applicable"
    }
}

Essential Math You Must Know

Before diving into the coding problems, make sure these formulas are second nature. You should be able to write them from memory and translate them directly into code.

# ---- FORMULAS YOU MUST KNOW ----

# Mean: sum of values divided by count
# mu = (1/n) * sum(x_i)

# Variance (population): average squared deviation from mean
# sigma^2 = (1/n) * sum((x_i - mu)^2)

# Variance (sample): Bessel's correction
# s^2 = (1/(n-1)) * sum((x_i - x_bar)^2)

# Standard deviation: square root of variance
# sigma = sqrt(variance)

# Covariance: how two variables move together
# cov(X,Y) = (1/(n-1)) * sum((x_i - x_bar)(y_i - y_bar))

# Correlation (Pearson): normalized covariance
# r = cov(X,Y) / (std(X) * std(Y))

# Normal PDF: f(x) = (1 / sqrt(2*pi*sigma^2)) * exp(-(x-mu)^2 / (2*sigma^2))

# Bayes' theorem: P(A|B) = P(B|A) * P(A) / P(B)

# t-statistic: t = (x_bar - mu_0) / (s / sqrt(n))

# Chi-squared statistic: chi2 = sum((observed - expected)^2 / expected)

Course Roadmap

This course is structured from fundamentals to advanced methods. Each lesson contains real interview problems with complete from-scratch solutions.

Course Structure:

Lesson 2: Descriptive Statistics (6 problems)
  - Mean, median, mode from scratch
  - Variance, standard deviation
  - Percentiles, histogram binning
  - Correlation coefficient
  - Running/streaming statistics

Lesson 3: Implement Distributions (5 problems)
  - Normal distribution (PDF, CDF, sampling)
  - Binomial PMF and CDF
  - Poisson distribution
  - Exponential distribution
  - Sampling: inverse CDF, Box-Muller transform

Lesson 4: Hypothesis Testing in Code (5 problems)
  - Two-sample t-test (Welch's)
  - Chi-squared test of independence
  - A/B test significance analysis
  - Permutation test
  - Bootstrap confidence intervals

Lesson 5: Monte Carlo Simulation (5 problems)
  - Estimate pi via random sampling
  - Option pricing (Black-Scholes Monte Carlo)
  - Probability puzzles via simulation
  - Markov chain Monte Carlo (Metropolis-Hastings)

Lesson 6: Bayesian Methods in Code (5 problems)
  - Bayesian inference engine
  - Beta-Binomial conjugate model
  - Naive Bayes classifier from scratch
  - Bayesian A/B testing
  - Thompson sampling for bandits

Lesson 7: Patterns & Tips
  - Statistical coding patterns cheat sheet
  - Numerical precision guide
  - Interview FAQ

Key Takeaways

💡
  • Quant firms and data science roles test from-scratch implementation of statistical methods
  • Always write the mathematical formula first, then translate to code
  • Numerical stability (Welford's, log-probabilities) separates strong from weak candidates
  • Know both the from-scratch version and the scipy equivalent for verification
  • Edge cases matter: empty inputs, n=1, division by zero, overflow