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.
Your offline evaluation shows Model A clearly beating Model B, but the online A/B test shows no meaningful difference. Propose an investigation plan to identify the cause, and recommend concrete changes to your offline evaluation process to improve alignment going forward.
Sample Answer
Investigation plan: goal: find why offline lift (A > B) didn't surface online. I'll run targeted checks in parallel (triage → hypothesis testing → remediation).
- Reproduce the discrepancy
- Verify offline evaluation uses the exact same model artifacts, preprocessing, feature generation, and decision logic as deployed.
- Run a small shadow test in production (log model A and B decisions on live traffic without affecting users) to compare predictions and inputs.
- Metric alignment
- Compare offline objective vs online KPI. Map model output → business action → online metric. If offline uses log-loss or AUC but online cares about click-through or conversion, retrain/evaluate using the online metric (or a proxy such as calibrated probabilities leading to same decision thresholds).
- Compute calibration and decision-threshold swept offline performance for the exact online metric.
- Sampling bias / population shift
- Compare train/validation distribution to online traffic (user segments, time-of-day, geos, devices). Use population statistics (feature marginals), covariate shift tests (KL, PSI), and model performance broken down by segment.
- If bias found, reweight offline test set to match production distribution or evaluate on stratified holdouts.
- Instrumentation and logging
- Audit feature generation and any upstream transformations in production for missing/lagged features, default fill values, or unit mismatches. Check feature freshness and latency.
- Validate online event instrumentation for the outcome label (deduplication, attribution windows, causal assignment correctness).
- Feature leakage and training-serving skew
- Scan for features that leak future info in offline pipeline (timestamps, labels-derived features). Run a “time-forward” evaluation where features only use information available at decision time.
- Compare feature statistics between training snapshots and serving logs to catch serving-time approximations.
- Exposure and feedback loops
- Check whether model A changes downstream user behavior differently (novelty, long-term effects) not captured offline. Run short-term vs long-term metric analysis and look for delayed signals.
- Ensure randomization in A/B test is correct and no spillover between buckets.
Concrete changes to offline evaluation
- Use holdout sets that mimic production sampling (stratified by geo/device/time) and apply importance weighting when distributions differ.
- Evaluate using the online business metric (or a well-validated proxy); perform threshold-based and counterfactual simulations to map offline scores to online decisions.
- Implement production shadowing as standard: daily sample of live traffic scored with both models and logged for retrospective analysis.
- Add automatic checks: calibration, PSI per feature, training-serving skew alerts, and unit tests validating feature parity.
- Remove or flag any features that rely on future signals; enforce “information cutoff” during dataset assembly.
- Periodically run randomized offline experiments (simulate assignment logic) and maintain an issues playbook linking offline failure modes to remediation steps.
Expected outcome: faster root-cause identification, higher offline/online correlation, fewer failed launches.
Design a comprehensive evaluation framework for a large-scale search or recommendation product serving tens of millions of users monthly. Cover offline metrics (NDCG@k, recall@k, MAP), how you would correct for position and exposure bias, the online metrics you would track (CTR, revenue, retention), the logging schema needed for counterfactual evaluation, and how offline evaluation, online A/B tests, and champion-challenger deployment fit together.
Sample Answer
Requirements & goals:
- Evaluate ranking quality (relevance), business outcomes (CTR, revenue, retention), and long-term user satisfaction at 100M DAU with low risk.
Offline metrics & protocol:
- Relevance: NDCG@k, Recall@k, MAP@k computed on holdout sessions; use session-level aggregation and per-user temporal splits (train on t, test on t+delta).
- Calibration & confidence: compute confidence intervals via bootstrapping by user.
- Diversity & novelty: catalog-based measures (intra-list diversity, coverage).
Correcting position/exposure bias:
- Propensity scoring via logged exposure probabilities (from serving logs): use IPS (inverse propensity scoring) and SNIPS to unbiasedly estimate CTR and NDCG.
- Train position-bias models (e.g., an examination model / PBM) to estimate propensities when they aren't logged directly.
- Use doubly robust estimators combining IPS with outcome models to reduce variance.
Online metrics:
- Immediate: raw CTR, conversion rate, revenue per thousand impressions.
- Short-term engagement: session length, day-over-day retention.
- Long-term value: 7/30/90-day retention, LTV, churn rate, downstream purchases.
Logging schema (must be complete & immutable):
- Event id, user_id (hashed), timestamp, session_id, request_id, placement_id, rank_list (item_ids + positions), served_probabilities (model score, softmax prob), exposure_flag per item, click/engagement events with timestamps, item metadata (owner, category), context (device, region), policy_version, experiment_id, traffic_bucket, reward signals (purchase, watch_time), prior user-state feature snapshot. Ensure deterministic replay keys and a sampling indicator for subsampling.
Counterfactual eval & offline simulator:
- Offline simulator: replay logged requests, simulate alternative policies using logged propensity or importance weights. Include synthetic user-response models learned from logs for stress tests (e.g., adversarial content).
- Use IPS/SNIPS/doubly robust for policy evaluation. Validate simulators by backtesting on historical A/B tests.
A/B testing & system components:
- Experiment platform: traffic allocation, randomization (user-level), exposure logging, kill switch.
- Metrics pipeline: near-real-time aggregator for guardrail metrics, weekly cohort analyses for long-term metrics.
- Policy rollout: staged (canary to ramp), automatic risk checks (statistical significance and business bounds).
- Analysis tools: automated uplift estimation, sequential testing with alpha-spending, and variance reduction via stratification/ANCOVA.
How offline evaluation, online A/B tests, and champion-challenger deployment fit together:
- Offline metrics are the fast, cheap FILTER: any new policy must beat the current champion on the offline holdout and the offline simulator (using IPS/SNIPS/DR) before it is allowed anywhere near real traffic. This is the stage that screens out most bad candidates for near-zero cost.
- A/B testing is the causal VALIDATION step: a policy that clears the offline bar gets a randomized, low-traffic online test against the current champion, because even debiased offline estimators can miss position-bias or feedback-loop effects that only appear with real exposure.
- Champion-challenger is the ONGOING PRODUCTION pattern once a challenger has won its A/B test: instead of a full one-shot replacement, the challenger is promoted to serve a small, sustained slice of live traffic (e.g. 5-10%) permanently alongside the incumbent champion, with the same online metrics tracked continuously rather than for a fixed test window. This catches slow drift, seasonality, and small regressions a short A/B window would miss, and gives an instant, no-redeploy rollback (shift traffic back to the champion) if the challenger degrades later. Only after a sustained period of the challenger matching or beating the champion does it get promoted to be the new champion, at which point a fresh challenger can be tested against it.
- Together the three form a funnel of increasing cost and decreasing risk: offline (cheap, many candidates screened, imperfect signal) -> A/B (moderate cost, causal, time-boxed) -> champion-challenger (small ongoing cost, the steady-state safety net that a time-boxed test can't provide).
Long-term impact tracking:
- Cohort-based LTV and retention dashboards, causal impact analyses (difference-in-differences, synthetic controls), monitor content-provider effects and feedback loops (popularity bias).
- Periodic offline retraining with debiased labels and causal features to prevent feedback loops.
Trade-offs & operational notes:
- Logging volume: sample some heavy fields but keep deterministic keys for replay.
- Bias-variance: IPS is unbiased but high variance; prefer doubly robust estimators in production.
- Privacy: hash/anonymize PII; consider differential privacy for aggregate dashboards.
This framework provides unbiased offline evaluation, safe online experimentation, and a champion-challenger steady state to iterate recommendation policies at scale with continuous, low-cost safety monitoring.
Compare ROC-AUC and PR-AUC (precision-recall AUC) for a binary classifier. For a task where positives are rare (for instance 0.5% prevalence), which curve is more informative and why? Sketch or describe a scenario where ROC-AUC looks strong but PR-AUC reveals the model is actually poor, and explain the intuition for why that happens.
Sample Answer
ROC-AUC (receiver operating characteristic AUC) measures a model’s ability to rank positives above negatives by plotting TPR vs FPR across thresholds. PR-AUC (precision–recall AUC) plots precision vs recall and emphasizes the model’s positive predictive value. When positives are rare, PR-AUC is more informative because precision directly incorporates class prevalence (it penalizes false positives heavily), while FPR can look small simply because there are many negatives.
Synthetic example:
- Dataset: 1,000 samples, 10 positives (1%), 990 negatives.
- At a chosen threshold the model returns: TP = 9, FN = 1, FP = 99, TN = 891.
- Recall (TPR) = 9/10 = 0.90
- FPR = 99/990 = 0.10
- Precision = 9 / (9 + 99) ≈ 0.083
Interpretation:
- The ROC point (TPR=0.90, FPR=0.10) looks strong → contributes to a high ROC-AUC.
- Precision is very low (8.3%), so PR-AUC is poor: most predicted positives are false alarms.
- In imbalanced settings, small FPRs correspond to many false positives in absolute terms, which ROC hides but PR highlights.
Practical guidance:
- For rare-event detection, prioritize PR-AUC or Average Precision, and report precision at operational recall (e.g., precision at 80% recall).
- Also consider calibration, cost-sensitive metrics (F1, precision@k), and business impact when selecting thresholds.
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.
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) with schema (event_id, user_id, exp_id, variant, timestamp, payload).
- Ingestion & enrichment: stream processors (Flink/Spark Streaming) 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) 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/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.
- Sequential testing corrections: use alpha-spending methods (O’Brien–Fleming or Pocock) or group-sequential designs to allow interim looks without inflating Type I error. Alternatively, use always-valid p-values 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) 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.
Analytics & final analysis:
- Use batch processing on analytics store to compute per-user aggregated metrics, use regression adjustment (covariate adjustment, CUPED) 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 hundreds of Model Evaluation and Validation interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.