Advanced ML Techniques & Research Application Questions
Advanced machine learning techniques, architectures, training methods, evaluation strategies, and the application of research insights to production ML systems. Covers bridging research findings to practical deployment, scalable training and serving, model governance, experiment design, and responsible AI practices.
HardTechnical
38 practiced
For a recommendation ranking model exhibiting feedback-loop bias (popular items get more exposure and thus more clicks), propose practical mitigation strategies including exploration policies (epsilon-greedy, Thompson sampling), counterfactual evaluation with inverse propensity scoring, offline debiasing techniques, and improved logging strategies to enable unbiased evaluation. Discuss trade-offs and deployment concerns.
Sample Answer
Problem summary: feedback-loop bias arises because popular items receive more exposure → more clicks → model learns popularity rather than true relevance. Mitigation requires a mix of exploration, unbiased evaluation, offline debiasing, and better logging.1) Exploration policies (practical use)- Epsilon‑greedy: with prob. ε randomly replace top item(s). Simple, low-cost, predictable regret. Good for short experiments.- Thompson sampling / Bayesian bandits: sample from posterior over item/context rewards to balance exploration-exploitation adaptively. Lower regret vs. ε-greedy, handles uncertainty, but needs per-item/posterior maintenance and careful priors.- Contextual bandits (LinUCB, neural TS): scale to high-dim contexts to explore relevant regions rather than purely random items.Trade-offs: exploration causes short-term UX/regret and business metric hit; must tune exploration budget, use soft-exploration (expose less-critical slots) or time-decay schedules.2) Counterfactual evaluation (IPS & extensions)- Log propensity p(a|x) for each shown action. Use IPS estimator to compute unbiased offline estimates of new policies: E[f] ≈ Σ r(a)/p(a). Use self-normalized IPS to reduce variance.- Use Doubly Robust (DR) estimators to combine IPS with a reward model — lower variance and more stable.- Beware of large variance when propensities are tiny — clip or regularize propensities.3) Offline debiasing techniques- Reweight training examples by 1/p(show) to correct exposure bias.- Causal embedding / instrumental variable approaches when propensities missing.- Synthetic counterfactual data via controlled randomized buckets to augment tails.- Train models to predict propensity as an auxiliary target; use balancing regularizers to reduce correlation between item popularity and predicted score.4) Improved logging to enable unbiased evaluation- Log full candidate list, ranks, exposures, rewards (clicks, dwell time), context, timestamp, unique request id, and probability/propensity and RNG seed. Log whether item was eligible.- Maintain separate randomized-exposure buckets (small % traffic) with full randomization for ground-truth estimation and offline training.- Ensure deterministic replay: store model version and feature snapshot to reproduce decisions.Deployment concerns & trade-offs- UX/regret vs. long-term model quality: keep exploration small and targeted (lower slots, new-user cohorts).- Engineering: storing full candidate sets and propensities increases telemetry volume and costs; design streaming pipelines and compressed schemas.- Latency: bandit algorithms and propensity computations must be efficient; precompute priors or use approximate posteriors.- Cold-start & scale: Thompson sampling needs priors; use hierarchical/embedding-sharing to handle long-tail items.- Privacy & compliance: avoid logging PII; aggregate where possible.- Monitoring: track online/offline estimator drift, IPS variance, and counterfactual estimator diagnostics (effective sample size).Recommended rollout:- Start with small randomized buckets + ε-greedy in low-impact slots to collect reliable propensities.- Implement IPS/DR for offline evaluation; validate with randomized bucket outcomes.- Gradually move to contextual TS for better regret, ensuring robust logging and monitoring.
HardTechnical
49 practiced
As a principal ML engineer, outline how you would set cross-team technical standards for model quality, reproducibility, and review processes. Include measurable criteria for unit tests, dataset versioning, experiment tracking, model cards, pre-deployment gates, CI/CD checks, and periodic audits. Describe enforcement mechanisms and how to balance speed with rigor.
Sample Answer
Situation/Goal: As principal ML engineer I need cross-team standards so models are high-quality, reproducible, auditable, and deployable without blocking velocity.Approach (policy + measurable rules):- Unit tests & CI: - Minimum: 80% function-level coverage for data-processing and model-serving modules; critical components 95%. - Tests include data-schema contracts, determinism (seeded RNG), and operator-level numeric regression (tolerance eps). - Enforced via pre-commit hooks + CI that fails build on coverage or flaky-test regressions.- Dataset versioning & lineage: - All training/validation/test sets must be immutable, stored with content-addressable IDs (e.g., DVC/DeltaLake) and registered in a metadata catalog. - Measurable: every experiment references dataset_id, schema_hash, and upstream query; data-drift alarms trigger when feature distribution JS divergence > 0.1 or KS-stat p < 0.01.- Experiment tracking: - Record code git-sha, dataset_id, hyperparams, environment/container image, and random-seed. - Track metrics with an experiment DB (e.g., MLFlow) and require at least one reproducibility run that replays train with same artifacts producing metric delta within 2%.- Model cards & documentation: - Mandatory model card fields: purpose, intended population, training data summary, evaluation metrics (overall + slice), fairness metrics, risks/limitations, approved owners, and lineage links. - Must be auto-generated from experiment metadata and reviewed before any production promotion.- Pre-deployment gates / CI checks: - Automatic checks: unit/integ tests, reproducibility replay, performance thresholds (e.g., accuracy >= baseline ± delta), latency and memory budgets, fairness thresholds (e.g., disparate impact < 1.25), and adversarial/basic robustness tests. - Human review: security, privacy, and product-signoff for high-risk models. - Deployment strategy: automated canary/percentage rollouts with automated rollback on metric regressions (SLO breaches, user-impact metrics).- Periodic audits: - Quarterly audits for high-impact models: data lineage review, retraining cadence, drift analysis, and a compliance checklist. Maintain audit logs and signed attestations.Enforcement mechanisms:- Gatekeeper CI pipelines that reject merges or promotions lacking artifacts (dataset_id, model card, reproducibility badge).- Codeowners and approval rules in repo; required reviewers from data-science, infra, and compliance for Tier-1 models.- Automated policy-as-code (e.g., OPA) integrated into CI and deployment.- KPI scorecards per team: % models with complete model-cards, % reproducible runs, mean time to rollback. Tie to performance reviews and resource allocation.Balancing speed vs rigor:- Risk-based tiers: Fast lane for low-risk/experimental models with lighter gates; strict lane for production/high-impact models.- Provide golden paths and tooling: templates, SDKs that auto-populate metadata, local reproducibility runners, and cheap mocks for quick iteration.- Shift-left: fast feedback via local linting, synthetic-data unit tests, and small-scope experiments; require heavier full-pipeline checks only at promotion.- Invest in automation to keep rigor low-friction so compliance is a byproduct, not a bottleneck.Result: Clear, measurable standards, automated enforcement, and risk-based flexibility let teams move fast where safe and apply rigorous controls where required.
MediumSystem Design
38 practiced
Design a real-time recommendation serving architecture capable of handling 100k QPS with p95 latency under 50ms. Include components for online feature retrieval (feature store), model serving (stateless vs stateful), caching strategies for hot features, batching where appropriate, autoscaling, A/B testing hooks, and monitoring. Discuss trade-offs for expensive features and strategies to handle cold-start users.
Sample Answer
Requirements & constraints:- 100k QPS, p95 < 50ms, real-time feature retrieval, A/B testing, autoscale, handle expensive features and cold-start.High-level architecture:- API Gateway / Frontend → Load Balancer → Model Serving Cluster(s) + Feature Store Cache Layer → Feature Store (online) + Expensive Feature Service (async) → Distributed Cache (Redis/Memcached / Tornado) → Logging / Metrics / Experiment Manager.Core components:1. Online Feature Store:- Low-latency key-value store (e.g., RocksDB-backed Cassandra or Redis Streams) for user/item keys. Expose gRPC API with multi-get.- Partition by user-id hashed to reduce hotspotting; replica reads for availability.2. Model Serving:- Stateless microservices for inference (containerized pods). Keep model weights in-memory; use optimized inference libs (ONNX Runtime, TensorRT).- For stateful needs (session-level RNNs), keep minimal state in an external fast store (Redis) to keep pods horizontally scalable.3. Caching:- Two-tier cache: edge LRU cache close to serving pods (local in-process) + global Redis cluster for hot keys.- Hot-keys pre-warming for top X% users/items; use popularity-based TTLs.- Cache keys: concatenated user-id + feature-version to support rollbacks.4. Batching:- Micro-batching at the serving layer (e.g., group requests for the same model within 1–5ms) to exploit vectorized inference while respecting p95 latency. Use adaptive batching with max batch size and max wait constraint.5. Expensive features:- Decompose into cheap (real-time) vs expensive (heavy compute/ML pipeline). Compute expensive offline and materialize; if still dynamic, compute asynchronously and use best-effort stale-safe values with confidence scores. Optionally fallback to model that omits expensive features.6. Autoscaling:- Horizontal autoscaling on request rate and CPU/GPU utilization. Use predictive autoscaling (look-ahead) because cold starts hurt p95. Keep warm pool of instances and use fast container runtime (gVisor or Firecracker) for rapid start.7. A/B testing / Experimentation:- Experiment manager assigns user treatment and stamps requests with experiment-id. Serving layer reads experiment routing to select model variant. Use traffic split at gateway and deterministic hashing to ensure consistency. Log exposures and outcomes to event stream for offline analysis.8. Monitoring & SLOs:- Per-endpoint latency histograms (p50/p95/p99), error rates, feature-store tail latencies, cache hit rates, batch sizes, model correctness metrics (CTR, loss drift). Alert on p95 > 50ms and cache miss spikes. Use distributed tracing for root-cause.Cold-start strategies:- For new users: use population averages, content-only models, or collaborative-filtering warm-start using device/session signals. Use exploration policy (epsilon-greedy or Thompson sampling) to collect signal while minimizing UX impact.Trade-offs:- Strong consistency vs latency: prefer eventual consistency for features to reduce latency.- Caching increases staleness risk — mitigate with versioned keys and TTLs.- Batching increases throughput but can add latency; adaptive batching balances both.- Keeping everything stateless simplifies scaling; externalize session/state but pay network cost.This design balances low latency and high throughput while supporting experiments, autoscaling, and practical handling of expensive features and cold-starts.
MediumTechnical
35 practiced
Describe how you would implement knowledge distillation in a production ML pipeline. Cover teacher model selection, generating soft targets (logits), temperature scaling, dataset selection for distillation (in-domain vs synthetic), student architecture design, training schedule, and validation criteria to ensure the distilled model preserves critical behaviors and thresholds.
Sample Answer
Requirements & goals: reduce latency/memory while preserving critical business metrics and thresholded behaviors (e.g., fraud recall at X%, latency < Y ms). Implementation plan:1. Teacher selection- Choose a high-performing, well-calibrated model (or ensemble) used in prod or a checkpoint with best validation on target metrics. Prioritize model whose failure modes are acceptable and whose logits are informative (not overly confident).- If ensemble used, average logits to provide richer soft targets.2. Generating soft targets (logits) & temperature- Export teacher logits (pre-softmax) for distillation. Store logits alongside inputs to enable offline training.- Apply temperature T>1 in softmax: soft_target = softmax(logits / T). Higher T smooths probabilities and transfers dark knowledge.- Distillation loss: L = α * CE(student_soft, soft_target) + (1-α) * CE(student_hard, y_true). Typical α in [0.3,0.7]; tune.3. Dataset for distillation- In-domain data is best (real distribution). Use logged production inputs (privacy-safe/sampled) and augment with teacher-labeled unlabeled data.- Synthetic / augmented data valuable to expose edge cases or amplify rare classes; generate via augmentation, simulation, or generative models but validate distribution shift.- Mix: prioritize in-domain, add curated synthetic batches to cover tail behaviors.4. Student architecture design- Choose architecture matching deployment constraints (MobileNet, DistilBERT, smaller transformer, quantized/residual-lite). Consider structured pruning, knowledge distillation + quantization-aware training.- Keep capacity to model critical thresholds; sometimes wider but shallower works better than extreme compression.5. Training schedule & practical recipe- Pretrain student on hard labels / supervised task to get stable gradients.- Distillation phase: train with combined loss; use lower LR, longer tail schedule. Example: AdamW, base LR 1e-4 → cosine decay; batch size tuned for stability.- Curriculum: start with higher weight on hard labels, gradually increase α toward soft loss to transfer nuanced behavior.- Regularization: label smoothing, data augmentation, EMA of weights. If logits stored, sample diverse batches to avoid overfitting to teacher quirks.6. Validation & acceptance criteria- Primary business metrics: ensure model meets or exceeds thresholds (recall/precision at operating point, ROC AUC, FPR at target recall).- Behavior preservation tests: - Thresholded decision parity: run A/B on held-out production slice to confirm identical action rates at decision thresholds. - Calibration: reliability diagrams, expected calibration error; if teacher calibrated, student should match. - Edge-case/unit tests: synthetic scenarios, adversarial or rare-class inputs verify outputs match teacher within tolerances. - OOD detection: monitor confidence distribution; ensure student doesn't become overconfident on OOD.- Performance metrics: latency, memory, energy; ensure deployment constraints met.- Statistical tests: paired significance tests on key metrics vs teacher; set guarded rollback thresholds.- Continuous monitoring: deploy with shadow/ canary mode, collect mismatches and drift metrics, trigger retraining if divergence grows.7. Additional production considerations- Store teacher logits and random seeds for reproducibility.- Consider iterative distillation (distill to intermediate model then further compress), or teacher assistant technique.- Combine distillation with quantization/pruning-aware fine-tuning.- Maintain explainability and fairness checks; monitor for amplified biases.This approach balances fidelity to the teacher with practical deployment constraints, using mixed datasets, temperature tuning, loss weighting, and rigorous validation to ensure critical behaviors and thresholds are preserved.
EasyTechnical
35 practiced
Implement an early stopping mechanism suitable for integration into a PyTorch training loop in Python. Your design should include a simple EarlyStopping class or function that accepts patience, min_delta, an optional metric name to monitor (e.g., 'val_loss' or 'val_auc'), and a checkpoint path. Explain how it interacts with the optimizer and scheduler, how it restores the best weights, and how to handle parallel/distributed training checkpointing.
Sample Answer
Approach: implement a small EarlyStopping class that monitors a specified metric, keeps the best checkpoint (model + optimizer + scheduler states), and signals when training should stop after patience epochs without improvement greater than min_delta. It supports resuming by restoring best weights and handles distributed training by letting only rank 0 write checkpoints and saving model.module for DDP.Key points / usage:- Call early_stopping.step(metric, epoch, model, optimizer, scheduler, is_distributed, rank) at end of validation.- If step returns True, break training loop (early stop).- After stopping, call early_stopping.restore(...) to load best weights (and optimizer/scheduler states) before final evaluation or saving a production artifact.- For distributed training: ensure only one process (rank 0) writes checkpoints; use torch.distributed.get_rank() or environment vars to determine rank. When loading in DDP, load into model.module or load state into base model before wrapping in DDP.- Interaction with optimizer/scheduler: saving/restoring state_dict preserves optimizer momentum/learning-rate scheduler progress so resumed training behaves consistently. If you prefer to keep scheduler progress separate, omit saving scheduler state.- Edge cases: missing ckpt file, metrics with NaN, when model is wrapped/unwrapped, when scheduler has non-serializable state (catch exceptions).- Complexity: negligible CPU/I/O overhead proportional to model size when saving.This design is lightweight, production-friendly, and easily integrated into a PyTorch training loop.
python
import os
import torch
class EarlyStopping:
def __init__(self, patience=5, min_delta=0.0, monitor='val_loss', mode='min', ckpt_path='best.pt', verbose=True):
self.patience = patience
self.min_delta = min_delta
self.monitor = monitor
self.mode = mode
self.ckpt_path = ckpt_path
self.verbose = verbose
if mode == 'min':
self.best = float('inf')
self._is_better = lambda a, b: a < b - self.min_delta
else:
self.best = -float('inf')
self._is_better = lambda a, b: a > b + self.min_delta
self.num_bad_epochs = 0
self.best_epoch = None
def step(self, metric_value, epoch, model, optimizer=None, scheduler=None, is_distributed=False, rank=0):
improved = self._is_better(metric_value, self.best)
if improved:
self.best = metric_value
self.num_bad_epochs = 0
self.best_epoch = epoch
if not is_distributed or rank == 0:
self._save_checkpoint(model, optimizer, scheduler)
if self.verbose:
print(f"Saved new best checkpoint (epoch {epoch}) {self.monitor}={metric_value:.6f}")
else:
self.num_bad_epochs += 1
return self.num_bad_epochs > self.patience
def _save_checkpoint(self, model, optimizer, scheduler):
# unwrap DistributedDataParallel
net = model.module if hasattr(model, "module") else model
state = {'model_state': net.state_dict(), 'epoch': self.best_epoch}
if optimizer is not None:
state['optimizer_state'] = optimizer.state_dict()
if scheduler is not None:
try:
state['scheduler_state'] = scheduler.state_dict()
except Exception:
pass
torch.save(state, self.ckpt_path)
def restore(self, model, optimizer=None, scheduler=None, map_location=None):
if not os.path.exists(self.ckpt_path):
raise FileNotFoundError(f"Checkpoint not found: {self.ckpt_path}")
ckpt = torch.load(self.ckpt_path, map_location=map_location)
net = model.module if hasattr(model, "module") else model
net.load_state_dict(ckpt['model_state'])
if optimizer is not None and 'optimizer_state' in ckpt:
optimizer.load_state_dict(ckpt['optimizer_state'])
if scheduler is not None and 'scheduler_state' in ckpt:
try:
scheduler.load_state_dict(ckpt['scheduler_state'])
except Exception:
pass
return ckpt.get('epoch', None)Unlock Full Question Bank
Get access to hundreds of Advanced ML Techniques & Research Application interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.