Comprehensive coverage of how to measure, validate, debug, and monitor machine learning model performance across problem types and throughout the development lifecycle. Candidates should be able to select and justify appropriate evaluation metrics for classification, regression, object detection, and natural language tasks, including accuracy, precision, recall, F one score, receiver operating characteristic area under the curve, mean squared error, mean absolute error, root mean squared error, R squared, intersection over union, and mean average precision, and to describe language task metrics such as token overlap and perplexity. They should be able to interpret confusion matrices and calibration, perform threshold selection and cost sensitive decision analysis, and explain the business implications of false positives and false negatives. Validation and testing strategies include train test split, holdout test sets, k fold cross validation, stratified sampling, and temporal splits for time series, as well as baseline comparisons, champion challenger evaluation, offline versus online evaluation, and online randomized experiments. Candidates should demonstrate techniques to detect and mitigate overfitting and underfitting including learning curves, validation curves, regularization, early stopping, data augmentation, and class imbalance handling, and should be able to debug failing models by investigating data quality, label noise, feature engineering, model training dynamics, and evaluation leakage. The topic also covers model interpretability and limitations, robustness and adversarial considerations, fairness and bias assessment, continuous validation and monitoring in production for concept drift and data drift, practical testing approaches including unit tests for preprocessing and integration tests for pipelines, monitoring and alerting, and producing clear metric reporting tied to business objectives.
EasyTechnical
84 practiced
Explain Intersection over Union (IoU) and mean Average Precision (mAP) used for object detection. Describe step-by-step how IoU determines a true positive, how to treat multiple detections for the same ground-truth object, and the high-level computation of mAP across precision-recall curves (single IoU threshold and COCO-style multiple thresholds).
Sample Answer
Intersection over Union (IoU)IoU measures overlap between a predicted bounding box and a ground-truth box: IoU = area(intersection) / area(union). Values range 0–1; higher is better.How IoU determines a True Positive (step-by-step)1. For each predicted box (with class and confidence), compute IoU against all ground-truth boxes of the same class in the image.2. Find the ground-truth box with the highest IoU.3. If that IoU ≥ threshold (e.g., 0.5), and that ground-truth box has not yet been assigned to a higher-scoring detection, mark the prediction as a True Positive (TP) and mark the ground-truth as matched.4. If IoU < threshold or the matched ground-truth is already assigned, mark the prediction as a False Positive (FP).Treating multiple detections for the same ground-truthApply greedy matching by descending prediction confidence: assign the highest-confidence detection that exceeds IoU threshold to the ground-truth (TP); subsequent detections that also overlap that same ground-truth become FPs. This avoids double-counting.High-level mAP computation1. Collect all detections across dataset per class, sort by confidence descending.2. Produce precision-recall (PR) curve by sweeping confidence: at each point compute cumulative TP/(TP+FP) = precision and TP/(TP+FN) = recall.3. Compute Average Precision (AP) as area under the PR curve (interpolated or using discrete sums). For a single IoU threshold, mAP is mean of APs across classes (common: IoU=0.5 → mAP@0.5).4. COCO-style mAP averages AP over multiple IoU thresholds (from 0.50 to 0.95 step 0.05) and across classes: mAP = mean_{classes,IoUs} AP. This rewards both localization precision and robust detection across thresholds.
MediumTechnical
75 practiced
Implement in Python a pair of functions that take arrays of ground-truth labels and predicted scores or class labels and compute: confusion matrix, accuracy, precision, recall, and F1. Your implementation should handle edge cases including no positive predictions and empty input. Provide a brief description of how you would unit test these functions.
Sample Answer
To compute binary classification metrics robustly, convert predicted scores to labels (threshold 0.5) if needed, compute confusion matrix (TP, FP, FN, TN), then derive accuracy, precision, recall, and F1 with safe handling for divisions by zero and empty inputs.
python
from typing import Iterable, Tuple
def to_labels(y_pred):
# Accept iterable of floats or ints/bools; threshold at 0.5 for scores
return [1 if (isinstance(x, float) or isinstance(x, int)) and x >= 0.5 else int(bool(x)) for x in y_pred]
def confusion_matrix(y_true: Iterable[int], y_pred: Iterable, threshold=0.5) -> Tuple[int,int,int,int]:
y_true = list(y_true)
y_pred = list(y_pred)
if len(y_true) != len(y_pred):
raise ValueError("Lengths of y_true and y_pred must match")
if len(y_true) == 0:
return 0,0,0,0
# convert predictions (scores or labels)
preds = []
for p in y_pred:
if isinstance(p, float):
preds.append(1 if p >= threshold else 0)
else:
preds.append(int(bool(p)))
TP = FP = FN = TN = 0
for t, p in zip(y_true, preds):
if t == 1 and p == 1: TP += 1
elif t == 0 and p == 1: FP += 1
elif t == 1 and p == 0: FN += 1
elif t == 0 and p == 0: TN += 1
return TP, FP, FN, TN
def metrics(y_true: Iterable[int], y_pred: Iterable, threshold=0.5) -> dict:
TP, FP, FN, TN = confusion_matrix(y_true, y_pred, threshold)
total = TP+FP+FN+TN
accuracy = (TP+TN)/total if total else 0.0
precision = TP/(TP+FP) if (TP+FP) else 0.0
recall = TP/(TP+FN) if (TP+FN) else 0.0
f1 = (2*precision*recall)/(precision+recall) if (precision+recall) else 0.0
return {"TP":TP,"FP":FP,"FN":FN,"TN":TN,"accuracy":accuracy,"precision":precision,"recall":recall,"f1":f1}
Key points:- Handles scores and labels, empty inputs, and no positive predictions (returns precision=0).- Time O(n), Space O(n) for input copies.Unit tests to write:- Empty inputs -> zeros.- Perfect prediction (all metrics 1).- All negatives in true labels with some positive predictions -> precision defined, recall 0.- No positive predictions -> precision 0, recall 0.- Mixed examples verifying computed TP/FP/FN/TN against manual counts.- Mismatched lengths -> ValueError.
MediumSystem Design
92 practiced
Design an offline and online evaluation plan for a recommendations system. Specify offline proxy metrics (NDCG, recall@k, propensity-weighted metrics), how to design an online randomized experiment (bucket sizes, guardrail metrics, attribution windows), and how to detect and handle metric mismatch between offline and online results.
Sample Answer
Requirements & goals: maximize long-term engagement / conversions while keeping latency and business KPIs stable. Evaluate both relevance and downstream impact.Offline evaluation- Proxy metrics: NDCG@10 (ranked relevance), recall@50 (coverage), MAP for per-user quality. Use propensity-weighted estimators (IPS / SNIPS) to correct for logging bias when training from logged bandit data: IPS = Σ r_i * (π_new(a_i)/π_log(a_i)); SNIPS to reduce variance. Also track diversity / novelty and calibration (predicted score vs observed).- Offline protocol: create time-based train/validation/test splits, preserve session/user splits, evaluate per-cohort and position slices. Simulate candidate pool and business rules to reflect online constraints.Online randomized experiment- Randomization: user-level stable assignment to avoid cross-contamination. Ensure random seed, stratify by key covariates (region, device).- Buckets & ramp: staged rollout — diagnostic (0.1%–1%) → pilot (5%) → ramp (25%) → full (100%). Use independent control and treatment groups; consider multiple arms for variants.- Sample size & power: compute required N using expected lift, baseline variance, desired power (80–90%) and alpha (0.05); monitor early stopping rules conservatively.- Guardrail metrics: daily active users, CTR, conversion rate, revenue per user, latency/error rates, infra SLOs. Define alert thresholds (statistical and business).- Attribution windows: choose based on product funnel — short actions (click/purchase) 7-day window; longer lifecycle metrics (retention, LTV) 28–90 days. Use both short-term (immediate engagement) and leading indicators for longer-term effects.- Logging & instrumentation: log model inputs, candidate IDs, positions, policies, exposure probabilities, and downstream events. Ensure deterministic hashing for assignment and robust event deduplication.Detecting & handling offline-online metric mismatch- Diagnosis checklist: 1. Instrumentation mismatch: verify logged probabilities, sampling, and join keys. 2. Distribution shift: compare feature distributions and candidate pools between offline test and live traffic (covariate shift, new items). 3. Position / exposure bias: offline metrics often ignore UI effects—run interleaving or bandit-style exposure to measure position bias. 4. Objective mismatch: offline relevance (NDCG) may not capture business metrics (LTR vs conversions). Check correlation per slice. 5. Temporal effects: novelty or priming causing delayed effects—inspect longer attribution windows and cohort trends.- Remedies: - Reweight offline eval with propensity scores from live logging or perform counterfactual evaluation (IPS/SNIPS). - Run focused online diagnostics: small-scale A/B with additional logging, interleaving experiments, or canary that logs both systems exposures for paired comparison. - Slice analysis and causal trees to find segments where model underperforms; retrain with upweighted examples or add features. - Ensemble or hybrid deploy: conservative blending with control (e.g., 90% control, 10% new) while fixing issues. - Update offline proxies: incorporate position models, simulate UI, or optimize for proxy more correlated with online KPI (e.g., learn-to-rank objective using downstream labels).- Final step: iterate — fix instrumentation, retest offline using corrected propensities, rerun small online experiment, and only ramp when guardrails stable.This plan ensures rigorous offline proxies, safe and powered online testing, and systematic handling of mismatches so the recommender improves real business outcomes.
HardTechnical
78 practiced
Design monitoring metrics and alerting for ML models used in high-stakes domains such as healthcare diagnostics. Include requirements for human-in-the-loop fail-safes, audit logs (data provenance and decision trace), fail-over behavior when model confidence is low, alerting thresholds, and how to instrument the system to meet regulatory audit and compliance requirements.
Sample Answer
Situation: Deploying diagnostic ML models in clinical settings requires monitoring, human oversight, and auditable trails to protect patients and comply with regulators.Design summary — goals:- Detect performance drift, safety incidents, and data issues in near real-time- Ensure human-in-the-loop (HITL) escalation and clear fail-over when confidence is low- Produce immutable audit logs for provenance, decision trace, and regulatory review- Provide configurable alerting with explained thresholds and remediation playbooksMonitoring metrics (examples and intent):- Model performance: rolling AUC/ROC, sensitivity, specificity, positive predictive value measured on recent labeled cases (windowed: 7/30/90 days)- Input-data health: feature distribution shifts (KL divergence, population stability index), missingness rate, out-of-range values- Calibration & uncertainty: expected calibration error, Brier score, predicted probability histograms, fraction of low-confidence predictions (< threshold)- Operational: latency, inference error rate, throughput, model version usage- Safety signals: unexpected high false negatives on critical classes, subgroup performance (age, sex, device type)Alerting thresholds & behavior:- Multi-tier alerts: - Informational: non-actionable drift below soft threshold (e.g., PSI > 0.1) - Warning: near-threshold (PSI 0.1–0.2, calibration shift ECE > 0.05) — notify ML ops and clinical lead - Critical: exceed hard thresholds (PSI > 0.2, sensitivity drop > 5% absolute, > X false negatives/day) — trigger immediate HITL hold and paging to on-call clinician and compliance- Thresholds set from baseline performance, clinical risk tolerance, and validated in retrospective simulations; tuned with clinicians and QA.Human-in-the-loop fail-safes & fail-over:- Confidence gating: if model confidence < configured threshold or model signals ambiguity (ensemble disagreement, high epistemic uncertainty), route case to human reviewer or secondary rule-based system- Pre-approved fallback logic: deterministic clinical decision support rules for immediate safe action (e.g., flag as “requires urgent review”); automated queuing and priority routing to clinicians- Automatic model hold: critical alerts auto-disable model in production for affected cohort; traffic routed to manual workflow and a shadow model remains running for diagnostics- Escalation playbook integrated with alert: who to call, steps to triage, rollback instructions, regulatory notification requirementsAudit logs, data provenance, and decision trace:- Immutable append-only logs (WORM or blockchain-backed ledger optional) capturing: request timestamp, model version, input hashes, pre-processing code version, model weights checksum, predicted label + probability, calibration/uncertainty metrics, downstream action taken (human override, final disposition), user identity for overrides, and retention policy- Structured provenance metadata linking raw data source IDs, ingestion pipeline versions, training dataset snapshot IDs, and model training run IDs- Store logs in encrypted, access-controlled storage with write-once-read-many and tamper-evident checksums; retain per regulatory retention windows (e.g., 7–10 years for medical devices)- Provide tools to reconstruct decision lineage (trace a prediction back to training data snapshot, feature transformations, and model artifact)Instrumentation & implementation:- Telemetry pipeline: collect metrics at inference time (via sidecar or centralized logging agent) and send to monitoring stack (Prometheus/Grafana for ops + specialized ML monitors like Evidently/WhyLabs)- Use feature stores to record input feature vectors and versions; pair with sample buffers for human review- Implement automated labeling hooks: when clinician overrides decision, capture label and context for fast feedback and continuous evaluation- CI/CD gated by monitoring: model rollout staged (shadow → canary → full) with automated checks against acceptance criteria; automate rollback when critical alerts fire- Access & auditability: centralized IAM, SSO, RBAC for approvals; signed deployment manifests; change logs for model and pipeline updatesRegulatory & compliance considerations:- Map metrics and logs to regulatory requirements (FDA SaMD, HIPAA, GDPR): ensure PHI protection, data minimization, consent where required- Documentation: maintain validation reports, performance benchmarks, risk assessments, and traceability matrices; include monitoring plan and escalation procedures in technical file- Periodic independent audits and red-team safety tests; retention and destruction policies aligned to law- Provide explainability artifacts for clinicians and auditors: per-case explanation (SHAP/local surrogates), population-level fairness reports, bias mitigation logsWhy this approach:- Combines statistical monitoring with operational safety nets and human oversight to minimize patient risk- Immutable provenance and structured decision traces satisfy auditability and enable root-cause analysis- Tiered alerting and automated fail-over reduce time-to-detection and enforce safe defaults while keeping clinicians in the loopExample short workflow:1) Inference recorded with features → telemetry agent emits input hash + model output + uncertainty2) Monitoring pipeline computes drift/calibration; if alert tier reached, notify ML ops + clinician via pager with context and link to audit trace3) If critical, system flips routing: new cases for affected cohort go to HITL queue; model flagged for investigation; deployment rollback initiated if needed4) All actions and overrides appended to audit log for compliance and retraining dataset creation.
MediumTechnical
75 practiced
Explain focal loss and how it modifies cross-entropy to focus training on hard examples for highly imbalanced classification problems. Provide the focal loss formula, describe the role of gamma and alpha hyperparameters, and compare focal loss to class-weighting or oversampling in terms of gradient dynamics and overfitting risk.
Sample Answer
Focal loss is a modification of standard cross-entropy designed to down-weight easy/examples and focus learning on hard, misclassified examples — useful for highly imbalanced classification (e.g., dense object detection). For binary classification, focal loss is:FL(p_t) = -α_t (1 - p_t)^γ log(p_t)where p_t is the model’s predicted probability for the ground-truth class (p for positive label, 1−p for negative), α_t ∈ [0,1] balances class importance, and γ ≥ 0 is the focusing parameter.Role of hyperparameters:- γ (gamma): controls how strongly easy examples are down-weighted. When γ = 0, FL reduces to weighted cross-entropy. Larger γ increases attenuation of well-classified examples (p_t → 1), so gradients concentrate on hard examples (small p_t).- α (alpha): addresses class imbalance by scaling loss per class (like class weights). Typical choice: α for positives, 1−α for negatives.Gradient dynamics vs class-weighting / oversampling:- Class-weighting multiplies the loss (and gradients) uniformly per class, so rare-class gradients scale up for all examples — both easy and hard — which can still let easy negatives dominate optimization early.- Oversampling increases frequency of minority examples, raising their cumulative gradient contribution but may cause repeated easy examples and lead to overfitting minority patterns.- Focal loss dynamically scales gradient per example: g ∝ (1−p_t)^γ [term] so as p_t increases the gradient decays, preventing easy majority examples from dominating. This leads to more targeted updates on hard samples without inflating gradients of already-correct minority examples.Overfitting risk:- Oversampling and heavy class-weighting can increase overfitting to minority noise because they amplify all minority examples. Focal loss reduces that risk by focusing on misclassified minority examples rather than all minority examples equally. However, an overly large γ can over-focus on noisy/hard outliers and harm generalization, so tune γ and α and consider combining with regularization or smart sampling.
Unlock Full Question Bank
Get access to hundreds of Model Evaluation and Validation interview questions and detailed answers.