Model Evaluation and Validation Questions
Measuring whether a model is good enough to trust and ship. Covers metric selection for classification, regression, and ranking (precision/recall, ROC-AUC, calibration, RMSE), offline validation design, evaluation-metric-to-business-objective alignment, and production safety guardrails. Emphasizes choosing metrics that reflect real objectives and avoiding misleading evaluations.
The base rate (class prevalence) for your model's target changes over time in production. Describe concrete strategies to keep probability outputs well calibrated as that happens, what monitoring signal would trigger a recalibration, and how you would validate the recalibration without setting off a cascade of unnecessary retraining.
Sample Answer
Situation: In production, your model's class prevalence can shift (seasonality, market changes, a new acquisition channel), which breaks probability calibration even when ranking (AUC) stays stable, because P(y=1|x) responds to the class-prior term even when the class-conditional feature distributions P(x|y) do not change.
Recalibration approaches (from cheapest to most flexible)
- Bayesian prior-probability adjustment: if P(x|y) is unchanged and only the class prior P(y) has shifted, correct each old posterior p_old(x) analytically:
p_new(x) = [p_old(x) * (pi_new/pi_old)] / [p_old(x) * (pi_new/pi_old) + (1 - p_old(x)) * ((1-pi_new)/(1-pi_old))]
where pi_old is the training-time base rate and pi_new is the current base rate. Both ratio terms must appear (the numerator ratio AND the complementary 1-prior ratio); dropping the second is a common shortcut that silently under-corrects. Worked check: pi_old=0.10, pi_new=0.20, p_old(x)=0.30 gives p_new(x)=0.491, not the 0.462 you get if you drop the (1-pi_new)/(1-pi_old) term. - Platt (logistic) scaling: refit a single sigmoid on recent labeled data; simple, robust when the miscalibration is monotonic.
- Isotonic regression: non-parametric monotonic mapping for more flexible shape; needs more labeled data to avoid overfitting the tails.
- Bin-based (histogram) calibration table: compute the observed frequency per predicted-probability bin on recent data and use it as a lookup; easy to maintain and explain, cheap to update on a rolling window.
- Online exponential smoothing of the calibration mapping for steady, gradual drift rather than a one-shot bin recompute.
Monitoring signals and trigger rules
- Calibration metrics over time: Brier score (the mean squared error between each predicted probability and the actual 0/1 outcome; lower means better-calibrated, 0 is perfect) and Expected Calibration Error (ECE: buckets predictions by predicted probability, e.g. 0-10%, 10-20%, etc., and averages the gap between each bucket's mean predicted probability and its observed positive rate; 0 means the model's stated confidence always matches reality) computed on a rolling window (e.g. weekly), by segment. Trigger recalibration when ECE exceeds an absolute floor or rises materially relative to its trailing baseline.
- Base-rate shift: track the observed positive rate directly (a simple, model-free signal) alongside a Population Stability Index (PSI) on the score distribution; a base-rate move of a few percentage points is the most direct trigger for the prior-adjustment specifically.
- Predicted-vs-observed mismatch by decile (reliability-diagram deltas), or a KS/chi-square test between predicted-probability buckets and observed outcome rates.
- Volume and sample-size guardrails: only trigger a data-driven recalibration (isotonic, histogram) when there are enough recent labeled examples per bin to fit reliably; below that floor, prefer the closed-form prior-adjustment, which needs only the aggregate new base rate, not per-bin labels.
Validating the recalibration without cascading retrains
- Decouple calibration from model weights: treat calibration as a separate, versioned post-model layer, so a recalibration event never touches the underlying model artifact or triggers its retrain pipeline.
- Rolling holdout: fit the calibration map on one recent labeled window and validate on a subsequent, non-overlapping window (a temporal backtest), never on the slice used to fit it.
- Shadow/canary rollout: apply the recalibrated probabilities in parallel (shadow) and compare downstream decision metrics against the live mapping before switching consumers over.
- Require both a statistical check (bootstrap CI on the ECE/Brier improvement excludes zero) and a minimum practical delta before promoting a recalibration; a statistically detectable but practically tiny improvement is not worth a production change.
- Versioned, timestamped calibration maps with instant rollback: if downstream metrics degrade post-switch, revert the calibration layer without touching the base model.
Why this avoids retrain cascades: recalibration only needs the closed-form prior-shift correction or a lightweight refit of a low-parameter map (Platt/histogram), validated on a holdout window and shipped as an independently versioned artifact. None of that requires retraining the underlying classifier, so a base-rate move does not automatically trigger the expensive full retrain pipeline, unless the calibration-only fix fails to hold (P(x|y) itself has also shifted), which the reliability-diagram diagnostics above will surface as continued miscalibration even after a prior-only correction.
Explain Goodhart's Law and how metric gaming happens when a team optimizes a proxy metric instead of the true business objective (for example optimizing for clicks and getting clickbait). Give concrete examples, and propose an evaluation regime to detect and mitigate it: adversarial holdout sets, randomized audits, tracking long-term business metrics, and continuous monitoring for improvements that don't correlate with real outcomes.
Sample Answer
When teams optimize proxies (e.g., click-through rate, predicted relevance score) instead of the true objective (e.g., lifetime value, user retention), Goodhart’s law kicks in: the proxy ceases to be informative because models and people learn to exploit quirks of the proxy rather than driving real value. In AI systems this appears as reward-hacking (models generating superficially high-scoring outputs), dataset-specialization, or surface-level UI changes that boost metrics but harm retention.
Evaluation regime to detect & mitigate gaming:
- Define hierarchical objectives
- Primary business KPIs: retention, LTV, revenue per user, safety incidents.
- Secondary proxies: CTR, predicted relevance: explicitly tagged as proxies.
- Adversarial holdout sets
- Maintain holdout sets crafted adversarially (edge cases, manipulated inputs, distribution shifts).
- Periodically evaluate new models on these sets to reveal brittle optimizations.
- Randomized audits
- Run randomized, blind human audits across outputs (A/B tests with hidden evaluation teams).
- Include synthetic adversarial prompts designed to reveal overfitting to proxies.
- Long-term business metrics & cohort analysis
- Measure downstream outcomes (30/90-day retention, churn, refund rates) per experiment cohort.
- Use uplift modeling to attribute long-term impact vs short-term proxy gains.
- Cross-team guardrails & incentives
- Align incentives: tie engineering/product goals partly to long-term KPIs.
- Require evaluation checklists and sign-offs from analytics, trust/safety, and business stakeholders.
- Continuous monitoring and anomaly detection
- Instrument metric correlations: monitor cases where proxy improves but business KPIs stagnate or decline.
- Build automated detectors for suspicious patterns (sudden localized improvements, distributional changes, surge in edge-case failures) and trigger rollback/hold.
- Use feature-importance drift, calibration checks, and distributional tests (KS, population stability index).
- Investigations & remediation workflow
- On anomaly: run forensic tests (batch vs online, example-level inspection, adversarial inputs), freeze rollout if confirmed, and deploy fixes (regularization, adversarial training, reward reshaping, or revised proxy).
Example: A reranker increases CTR by 15% but cohort retention drops. Investigation reveals click-bait snippets; mitigation included adding a downstream retention objective to training, adversarial holdout of click-bait examples, and human audits: restoring aligned improvements.
This regime blends offline adversarial tests, randomized human checks, long-horizon measurement, organizational incentives, and continuous automated monitoring to detect and reduce metric gaming.
Explain cluster-randomized experiments, where you randomize at the level of a user, household, or region rather than an individual event, and why clustering is necessary when there is spillover or correlated behavior within a cluster. Define the intra-cluster correlation coefficient and describe how it affects the required sample size and variance estimation.
Sample Answer
Cluster-randomized experiments randomize treatment at the group level (users, households, schools, regions) rather than individuals. You use them whenever interference or correlated behavior makes individual randomization invalid: e.g., within-household spillover, network effects, or shared environments where one person’s treatment affects others’ outcomes. Randomizing clusters preserves the causal contrast and avoids contamination.
Intra-cluster correlation coefficient (ICC, ρ) measures the similarity of outcomes within clusters: ρ = σ_b² / (σ_b² + σ_w²), where σ_b² is between-cluster variance and σ_w² is within-cluster variance. ICC ranges 0–1; higher ρ means outcomes within the same cluster are more alike.
Impact on sample size and variance:
- Design effect (DE) = 1 + (m − 1)·ρ, with m = average cluster size. DE inflates variance relative to independent individuals.
- Effective sample size Neff ≈ N / DE (N = total individuals). So required total N must be multiplied by DE to retain power.
- Variance of treatment effect estimates must account for clustering: Var_clust = DE · Var_ind. Ignoring ICC underestimates standard errors, inflating Type I error.
Analysis recommendations:
- Power calculations should use estimated ICC and cluster sizes; increasing number of clusters is more effective than increasing cluster size when ρ>0.
- Use cluster-robust standard errors, mixed-effects models (random intercepts), or GEE with exchangeable correlation to correctly estimate SEs.
- Ensure sufficient degrees of freedom (enough clusters) because inference depends on number of clusters, not individuals.
Example: m=20, ρ=0.05 → DE = 1 + 19·0.05 = 1.95, so nearly double the sample needed compared with individual randomization.
Propose a comprehensive adversarial-testing strategy for an NLP text classifier: black-box and white-box tests, character-level perturbations, synonym substitution and paraphrase attacks, and prompt-injection or data-poisoning scenarios. Explain what metrics you would use to report robustness, and how you would integrate continuous adversarial testing into CI/CD.
Sample Answer
Threat surface for the text classifier
Treat this as four related but distinct test categories: (1) character-level attacks, (2) word/synonym and paraphrase attacks, (3) prompt-injection and data-poisoning scenarios, and (4) the metrics and CI/CD wiring that turn all three into a repeatable regression suite rather than a one-off audit.
White-box attacks (full model and gradient access)
- Gradient-guided character flips (HotFlip): use the gradient of the loss with respect to the one-hot character/token embedding to pick the single character substitution that most increases loss, applied iteratively under a small edit-distance budget.
- Gradient-guided word substitution: rank candidate synonym replacements by their projected effect on the loss using the embedding gradient, a white-box analogue of TextFooler, useful when you want a worst-case bound rather than a realistic-attacker simulation.
Black-box attacks (query access only, no gradients)
- TextFooler: rank words by importance (leave-one-out score drop), then substitute the top-ranked words with counter-fitted-embedding synonyms that are POS-tag consistent and semantically similar (a USE cosine-similarity threshold), searching for the fewest substitutions that flip the label.
- PWWS (Probability Weighted Word Saliency): combines word saliency with synonym-substitution likelihood to prioritize which word to perturb first.
- BAE (BERT-based Adversarial Examples): uses a masked language model to generate contextual word replacements or insertions, producing more fluent, paraphrase-like adversarial text than fixed synonym lists.
- Character-level black-box (DeepWordBug / TextBugger): swap, insert, delete, or substitute characters, including homoglyphs (e.g. a Cyrillic look-alike for a Latin letter) and keyboard-adjacent typos, at the highest-saliency character positions; this category specifically targets tokenizer brittleness rather than semantic understanding.
- Genetic-algorithm / population-based attacks: evolve a population of candidate perturbations under a fitness function balancing attack success and semantic similarity; more expensive but finds successful attacks the greedy methods above miss.
Triage: in practice, start with TextFooler for word-level robustness and DeepWordBug for character-level robustness; these two catch most real-world robustness gaps cheaply and are the right default battery for a new test suite. PWWS, BAE, and the genetic-algorithm attacks are worth adding once the basics are covered, since they are more expensive to run and mostly find the same class of failures with somewhat higher success rates. A concrete before/after example of what a successful attack looks like: the input The movie was great, would recommend (predicted: positive, confidence 0.94) becomes The movie was gr3at, would recommend after a single DeepWordBug character substitution, flipping the model's prediction to negative (confidence 0.61) even though a human reader still reads it as clearly positive.
Prompt-injection and data-poisoning scenarios
- Prompt injection (relevant when the classifier is prompted, e.g. a zero-shot or instruction-tuned LLM used as a classifier, rather than a fine-tuned discriminative model): embed adversarial instructions inside the input text itself (for example, text instructing the model to ignore its classification instructions) and test whether the model's decision follows the injected instruction instead of the actual content.
- Data poisoning / backdoor attacks: during training-data construction, insert a rare trigger token or phrase correlated with a target label in a small fraction of training examples; test whether the trained model has learned to flip its prediction whenever the trigger appears, independent of the rest of the input. Detect via trigger-search techniques (scanning for input tokens whose presence alone flips a disproportionate share of predictions) run against a held-out clean set before the model ships.
Robustness metrics to report
- Attack success rate at a fixed perturbation budget (max percent of words/characters changed, or max query count for black-box attacks), reported per attack method, not as a single aggregate number, since character-level and synonym-level attacks fail differently.
- Perturbation rate on successful attacks (median percent of tokens/characters changed): a lower rate means the model is easier to fool with a smaller, less detectable edit.
- Semantic-similarity-constrained success rate: success rate restricted to adversarial examples that pass a similarity floor against the original (e.g. USE cosine similarity above 0.8), so you do not credit 'attacks' that actually changed the meaning.
- Robust accuracy under budget: accuracy on the full test set when every example is attacked up to the fixed budget above, directly comparable to clean accuracy.
- Poisoning/trigger metrics: attack success rate of the trigger phrase specifically (fraction of trigger-inserted inputs that flip label) versus the false-trigger rate on clean inputs that happen to contain similar tokens.
- Certified robustness where feasible: randomized smoothing over word substitutions can certify that no synonym substitution within a bounded set flips the prediction, a provable rather than empirically-observed guarantee for a subset of inputs.
Integrating continuous adversarial testing into CI/CD
- Maintain a fixed, versioned adversarial regression set: a few hundred previously-successful attacks (character, synonym, and poisoning-trigger examples) captured once and replayed on every candidate model, so a regression against a KNOWN attack is caught without regenerating attacks each time.
- Fast PR-time check: run a cheap subset (a fixed-seed TextFooler/DeepWordBug batch, a few hundred examples, seconds to minutes) as a required CI gate; fail the build if attack success rate exceeds the current production model's rate by more than a small tolerance, or robust accuracy drops below a floor.
- Slower nightly/pre-release check: run the full battery, including the expensive genetic-algorithm attacks and a fresh generation pass (not just the frozen regression set), against the release-candidate model, and require human sign-off on any regression before promotion.
- Data-pipeline gate: run the trigger-search poisoning check as part of the training-data validation stage, before training even starts, not only post-training, since the cheapest fix for a poisoning attempt is catching the contaminated data before it is trained on.
- Track all of the above metrics on a dashboard across model versions, so you can see gradual robustness erosion, not just a pass/fail at each individual gate.
Design an online A/B test to compare a new model (for example a ranking or recommendation model) against the current production model. Specify your primary metric and guardrail metrics (revenue, latency, error rate), the bucketing strategy and unit of randomization, how you would compute the required sample size to detect a given relative lift with adequate power, and how you would handle sequential monitoring, early stopping, and novelty effects during the rollout.
Sample Answer
Requirements & constraints:
- Primary metric (business objective) + multiple secondary metrics.
- Automated guardrails: revenue, latency, error-rate.
- Support streaming and daily batch aggregation, safe gradual rollout, and statistical rigor for sequential looks.
- Low-latency monitoring for guardrails; accurate aggregation for final analysis.
High-level architecture:
- Traffic layer: deterministic bucketing (user_id hash + experiment salt) implemented in the app / serving proxies.
- Event capture: client/server emits immutable event logs to a streaming pipeline (Kafka/Kinesis: durable, ordered message queues that buffer high-volume event streams between producers and downstream consumers) with schema (event_id, user_id, exp_id, variant, timestamp, payload).
- Ingestion & enrichment: stream processors (Flink/Spark Streaming: distributed engines that transform and aggregate the incoming event stream in near real time) dedupe, join identity, enrich with user metadata, compute per-event revenue/latency/error flags, write to two sinks: real-time metrics store (Prometheus/ClickHouse/InfluxDB: time-series-oriented stores built for fast metric queries over time) for monitoring and analytics store (partitioned Parquet on S3 or BigQuery) for statistical analysis.
- Feature store / model registry integrates experiment assignments to reproduce offline evaluation.
Randomization & data quality:
- Deterministic assignment ensures consistent unit-level treatment. Log assignment events and periodic audits comparing assigned variant vs delivered treatment.
- Include client and server-side SDKs to fallback on server-side assignment when needed.
- Add sequence numbers and idempotency tokens to avoid double-counting.
Monitoring & dashboards:
- Real-time dashboards for guardrails (latency p95, error-rate, revenue per MAU) with alerting via thresholds and rate-of-change anomalies (PagerDuty: an on-call alerting/paging tool that routes a triggered alert to a human/Slack).
- Experiment dashboard shows primary & secondary metrics, per-cohort breakdown, balance checks, sample size, exposed vs assigned, and confidence intervals.
- Include cohort drift monitors (assignment skew over time) and instrumentation failure detectors.
Statistical approach:
- Primary analysis: predefine metric, unit of analysis, minimum detectable effect (MDE), alpha, power, and max sample size. Sample size worked example using the standard two-proportion formula n = (z_{α/2} + z_β)^2 × [p1(1−p1) + p2(1−p2)] / (p2−p1)^2: baseline conversion p1 = 5%, target lift giving p2 = 6% (a 1 percentage-point absolute MDE, a 20% relative lift), two-sided α = 0.05 (z_{α/2} = 1.96), power = 80% (z_β = 0.84). n = (1.96 + 0.84)^2 × (0.05×0.95 + 0.06×0.94) / (0.01)^2 = 7.85 × 0.1039 / 0.0001 ≈ 8,150 per arm (≈16,300 total). Recompute this per experiment with the actual baseline rate and MDE the team commits to.
- Sequential testing corrections: use alpha-spending methods (O'Brien–Fleming: an approach that spends very little of the alpha budget on early interim looks and most of it near the planned end, so an early significant result must clear a much higher bar than a late one, or Pocock: an approach that spends the alpha budget evenly across all planned interim looks, giving a lower, constant bar at every look but a stricter one than O'Brien-Fleming at the final look) or group-sequential designs to allow interim looks without inflating Type I error. Alternatively, use always-valid p-values (p-values computed with a method, such as mSPRT, that stays statistically valid no matter how many times or how often you peek at them, unlike an ordinary p-value which becomes unreliable under repeated peeking) or Bayesian credible intervals if team prefers Bayesian approach.
- Multiple secondary metrics & guardrails: treat guardrails as hard stop rules (not corrected): monitor them with tight thresholds; for hypothesis testing across many secondary metrics, apply FDR control (Benjamini–Hochberg: a correction that controls the expected proportion of false discoveries among all metrics flagged significant, less conservative than requiring every single metric to individually clear a Bonferroni-adjusted bar) for interpretation, but avoid masking guardrail alerts.
Early-stopping & automated rules:
- Two classes:
- Safety guardrail stops: automated immediate rollback if guardrail breach crosses predefined absolute or relative thresholds (e.g., error-rate increase > X% with minimum N events and p < 0.01 using sequentially-corrected test). Implement conservative thresholds and cool-down windows to avoid noisy rollbacks.
- Efficacy early stop: if primary metric shows strong benefit/loss at interim looks according to pre-specified alpha spending boundaries, then stop early. Record decision provenance in the experiment metadata store.
- Use monitor windows (e.g., min exposure time and min sample size) before allowing any stop decision.
Novelty effects:
- A new model or ranking can look better simply because it is new: users notice the change and interact differently for a short period regardless of whether the underlying improvement is real, or even present at all. Detect this by comparing early-window lift (e.g., days 1-3 of a user's exposure) to late-window lift (e.g., days 10-14) for the same cohort; a lift that decays toward zero as novelty wears off signals the win is partly or wholly novelty rather than a durable effect.
- Mitigate by excluding the first N days of each user's exposure from the primary analysis (e.g., the first week), or by running the experiment long enough that any pure novelty component has time to decay before the final read; report both the early-window and late-window lift explicitly so a decaying effect is visible rather than averaged away.
Analytics & final analysis:
- Use batch processing on analytics store to compute per-user aggregated metrics, use regression adjustment (covariate adjustment, CUPED: Controlled-experiment Using Pre-Experiment Data, which uses each user's own pre-experiment metric value to strip out noise unrelated to the treatment, letting you detect the same effect size with fewer users) to reduce variance and improve power.
- Report intention-to-treat (assigned) and treatment-on-the-treated (exposed) analyses, with stratified analyses for key segments.
- Provide reproducible notebooks and queries tied to experiment version and code for auditability.
Safe rollout practices:
- Canary → ramp: start with small %, monitor guardrails for short windows, then stepwise increase (1%, 5%, 25%, 50%, 100%) with automated gate checks at each step.
- Use feature flags + kill switch for immediate rollback.
- Bake in blast-radius limits (per-region, per-user-segment caps).
- Post-rollout monitoring for regression and long-term metrics (30/90-day cohorts).
Operational considerations:
- Store experiment metadata (start/stop, hypotheses, thresholds, alpha spending schedule) in a central experiments DB; log all decisions and alerts.
- Testing infra includes synthetic traffic tests, chaos tests, and canary validation of metrics pipeline.
- Governance: require pre-registered experiments for production runs and postmortems for any automatic rollback.
This design balances low-latency safety monitoring for guardrails with rigorous sequential-corrected statistical inference for primary metrics, reproducibility, and safe, auditable rollouts.
Unlock Full Question Bank
Get access to all Model Evaluation and Validation interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.