188 Data Scientist Interview Questions & Answers

92 top • 12 Amazon • 15 Apple • 13 Google • 12 Meta • 15 Microsoft • 15 Netflix • 14 NVIDIA

Data Scientist icon

Questions with Detailed ExplanationsWith Detailed Explanations

(Last Updated: September 8, 2026)

41. Design an API for text embeddings and compatible classifications.Machine Learning System DesignEasy

Question Details

The service receives raw text, owns deterministic preprocessing, and supports synchronous single-item requests plus larger asynchronous batches. Define versioned request and response contracts, item-level validation and partial failures, embedding and classifier training data and evaluation labels, an immutable compatibility registry, batching and accelerator scheduling, caching, tenant isolation, retries and checkpoints, online and batch serving, quality and drift monitoring, feedback, rollout and rollback, retraining triggers, privacy and retention, security boundaries, availability, and cost.

Short Interview Answer (30-60 seconds)

I would expose versioned embed and classify endpoints for synchronous single-item requests, plus an asynchronous batch API. Every path shares deterministic preprocessing and an immutable embedding-classifier compatibility registry. Batch workers use queues, accelerator-aware batching, retries, and checkpoints, while caching, tenant isolation, monitoring, feedback, safe rollout, privacy, and cost controls apply across the system.

Detailed Explanation

The service accepts raw text and owns deterministic preprocessing, so training and serving must use the same versioned text rules. I would expose synchronous embedding and classification endpoints for single items and an asynchronous batch endpoint for larger jobs. Every request is tenant-aware, validated, versioned, and resolved through an immutable compatibility registry before inference. Batch jobs preserve successful items when others fail and resume through checkpoints after retryable failures. Online and batch serving share model artifacts, preprocessing, compatibility rules, response contracts, monitoring, privacy boundaries, feedback lineage, and rollout controls while using different scheduling behavior.

Useful Questions to Ask the Interviewer
  1. What latency and request-size expectations should the synchronous endpoints support?
  2. How large can asynchronous batches become, and what completion-time expectations apply to them?
  3. Can a classification request run multiple classifier models against one embedding, or normally only one classifier?
  4. Can a classifier be approved for several embedding versions, or is each classifier tied to exactly one embedding version?
  5. What tenant isolation, retention, deletion, and regional requirements apply to raw text, embeddings, predictions, metadata, and logs?
  6. What availability and cost constraints should guide accelerator capacity, caching, batching, and graceful degradation?
Design an API for text embeddings and compatible classifications. diagram
How to Explain It in an Interview
1. Define the versioned API contracts

The synchronous unit of work is one raw text item. The service, rather than the client, owns preprocessing. I would expose versioned endpoints such as POST /v1/embed and POST /v1/classify. The API gateway performs authentication and authorization, tenant isolation and quotas, rate limiting, request validation, routing, and API-version selection.

An embed request carries raw text, the requested model or version information, and supported options. Its response contains the embedding and metadata such as model version, preprocessing version, embedding dimension, and request metadata. A classify request similarly returns labels or scores plus version metadata. A response formatter keeps these contracts consistent across serving paths.

For larger workloads, I would expose POST /v1/batches. A valid batch submission returns an accepted job identifier instead of keeping the HTTP connection open until all items finish. Clients can query job status and retrieve completed results.

2. Validate at both request and item level

A malformed request envelope can fail the whole request. A multi-item job is different: one invalid text item should not normally discard valid work from all other items.

Each item therefore has its own success or error state. The final batch can be marked partial, with counts of succeeded and failed items, successful outputs, and explicit errors for failed indexes. This matches the diagram's partial-failure response model and avoids reprocessing expensive successful items.

Idempotency is also important. Repeating the same accepted batch request with the same idempotency key should not create duplicate logical jobs or duplicate committed results.

3. Use deterministic, versioned preprocessing

The preprocessing configuration store contains versioned rules used by both online inference and the offline training pipeline. Relevant transformations can include normalization, language detection, PII handling, tokenization, truncation, or other model-required text preparation.

The important rule is consistency. Whatever transformations a deployed model requires must be represented by a stable preprocessing version and applied with the same semantics during training and serving. That prevents training-serving skew.

4. Make model compatibility an immutable contract

An embedding model and classifier should not be combined merely because their dimensions appear compatible. A classifier was trained and evaluated against a particular embedding representation and configuration.

I would keep an immutable compatibility registry containing approved embedding-to-classifier relationships. An entry records the embedding version, classifier version, dimensional constraints when relevant, evaluation or threshold metadata, and the validity of that combination. Existing approved entries are not silently rewritten; a new combination creates a new versioned entry.

The online inference service and batch workers both consult this same compatibility information before running classification. If a requested combination is not approved, the request fails explicitly rather than silently substituting another model.

5. Keep the model registry and lineage immutable

The model registry stores versioned model artifacts together with code and configuration versions, training-data snapshots, evaluation results, schema information, and other artifact metadata shown in the design. A published model therefore has traceable lineage back to the data, preprocessing, code, configuration, and evaluation that produced it.

The compatibility registry answers a different question from the model registry. The model registry says which artifacts exist. The compatibility registry says which embedding and classifier versions are approved to work together.

6. Separate synchronous serving from asynchronous batch serving

The synchronous path is optimized for interactive requests. The gateway sends a valid request to the online inference service, which applies deterministic preprocessing, resolves compatible model versions, performs embedding and optional classification, uses safe caches where applicable, and sends results through the response formatter.

Large jobs use the asynchronous path. A batch orchestrator maintains the job queue, chunks work into useful batches, schedules accelerator capacity, applies retry policy, records checkpoints, and sends work to batch inference workers. Workers preprocess items, run embedding and classification, checkpoint progress, and write results.

Keeping these paths separate prevents large backfills from consuming all resources required for interactive traffic.

7. Batch efficiently on accelerators

Accelerators are usually most efficient when several compatible items are processed together. The scheduler can group queued work by compatible model versions and other execution constraints, then form batches that improve utilization.

The tradeoff is latency. Waiting longer to form a larger accelerator batch may reduce cost per item but increases queueing delay. Interactive traffic should therefore use tighter batching limits, while asynchronous jobs can tolerate larger batches and longer waits.

Tenant quotas and scheduler fairness must prevent one tenant or one large batch from monopolizing shared accelerator capacity.

8. Retry with checkpoints instead of restarting whole jobs

A worker can fail after processing only part of a large job. Restarting from the beginning wastes accelerator time and risks duplicate writes.

The orchestrator records progress at stable chunk or item boundaries. After a retryable worker failure, processing resumes from the last committed checkpoint. Result writes and job-state transitions are idempotent so replay does not create duplicate logical results.

Permanent item failures, such as invalid input, remain item errors and are not retried forever. Retryable infrastructure failures are treated separately.

9. Cache only when semantic reuse is safe

The diagram contains separate embedding and classification caches. An embedding cache key should represent the effective normalized input and the relevant model and preprocessing versions. Classification caching must additionally capture the classifier version and any options that change classification semantics.

Caches must also obey tenant-isolation rules. A cache optimization must never expose another tenant's raw text, embeddings, classifications, or metadata.

Caching reduces latency and accelerator cost when requests repeat, but it uses storage and provides little benefit when almost every text is unique.

10. Use stores for the right responsibilities

The metadata store holds information such as tenants, quotas, API scopes, model aliases, endpoint limits, and related service metadata. The optional feature or vector store can hold versioned embeddings or vector indexes when downstream retrieval use cases require them, while keeping tenant partitions explicit.

These stores are not substitutes for the immutable model and compatibility registries. Each has a different responsibility: service metadata, optional vector data, model artifacts, and model compatibility should remain separately governed.

11. Train embeddings and classifiers offline

Offline training is separate from serving. The pipeline can ingest approved sources such as internal logs, user content, third-party data, or public corpora when permitted. Ingestion and labeling perform deduplication, PII handling, quality checks, and creation of the labels required for classifier training and evaluation.

The same deterministic preprocessing semantics used in serving are applied before training. Embedding-model training uses its appropriate representation-learning objective. Classifier training uses labeled examples for the classification task. If the classifier consumes embeddings, those training embeddings must be generated by the embedding version recorded in the compatibility relationship.

Training-data snapshots, preprocessing configuration, code, model configuration, artifacts, and evaluation results are versioned so the result can be reproduced and audited.

12. Evaluate before publishing

A training metric alone is not enough to deploy a model. The offline evaluation stage should measure metrics appropriate to the embedding or classification task, inspect errors, evaluate classifier calibration when its scores need probability-like interpretation, and examine meaningful slices when relevant.

The exact metric and acceptance threshold should come from product requirements rather than being invented. Evaluation results are stored with the artifact. Only models that pass the defined validation gate are published to the model registry and made eligible through compatibility entries.

13. Keep labels and feedback joined to the correct prediction

Some labels or outcomes can arrive after inference. Each prediction therefore needs a stable identifier together with the model and preprocessing versions that produced it. User feedback, corrections, or later system outcomes can be joined back to the correct prediction at the correct entity and time.

After quality and privacy checks, this feedback can be analyzed, prioritized, and used as future training data. The join must not accidentally associate feedback with a different request or model version.

14. Monitor several dimensions separately

Observability should distinguish several kinds of signals. Service-health monitoring covers end-to-end latency, model execution time, error rate, saturation, queue health, accelerator use, and other resources. End-to-end latency includes preprocessing, queueing, networking, inference, and formatting, so it is different from model execution time alone.

Data-quality monitoring checks schema validity, missing or empty input, language behavior, nulls, ranges, and other expected properties. Drift monitoring compares current inputs, embeddings, or predictions with suitable references. Model-quality monitoring uses labels or outcomes when available. Business outcomes are monitored separately.

Drift alone is not proof that model quality has declined. It is a signal to investigate together with quality measurements and delayed outcomes.

15. Roll out new versions safely and keep rollback ready

After offline validation, a new approved version should be introduced through controlled rollout rather than immediately receiving all traffic. The design supports canary release, shadow evaluation when appropriate, and feature flags.

Service-health and model-quality guardrails determine whether traffic expands. If a defined SLO or quality guardrail is breached, routing can return to the previous approved version. Because preprocessing, model artifacts, and compatibility entries are all versioned, rollback restores a complete known-compatible combination rather than swapping one model independently.

16. Use retraining triggers as signals, not automatic deployment

Potential retraining triggers include meaningful data drift, demonstrated quality degradation, important business impact, scheduled maintenance, or an authorized manual trigger.

A trigger starts the training and evaluation workflow. It does not bypass evaluation, compatibility checks, rollout controls, or rollback preparation. This prevents a noisy drift signal from automatically replacing a healthy production model.

17. Protect privacy and security boundaries

Raw text can contain sensitive data. The service therefore needs PII detection or masking where required, encryption in transit and at rest, tenant isolation, scoped API authentication and authorization, and restricted access to caches, metadata stores, model registries, vector stores, training data, and accelerator workers.

Retention policies apply to raw text, embeddings, classifications, metadata, logs, and training copies according to policy. Where required, deletion or export workflows should cover derived data as well as original text. Audit logs should record sensitive administrative operations without unnecessarily copying raw text into logs.

18. Design availability and cost together

Interactive serving can run across multiple failure domains or regions and autoscale to protect availability. Asynchronous queues absorb bursts and let batch work wait for accelerator capacity. Rate limits and tenant quotas protect shared resources.

Graceful degradation must remain semantically safe. A valid cache hit can avoid model execution, and an asynchronous batch can wait for capacity. A missing, unavailable, or incompatible model should return an explicit failure rather than silently use an unapproved model.

Major costs include accelerator inference, training, retained model copies, embeddings or vector storage, caches, metadata storage, and redundant online capacity. Dynamic batching, caching, asynchronous scheduling, autoscaling, tenant quotas, retention policies, and cost monitoring control those costs. More batching improves utilization but can increase latency, while more spare online capacity improves resilience but costs more.

Technical Approach
  1. Define versioned synchronous /embed and /classify contracts and an asynchronous /batches contract.
  2. Put authentication, authorization, tenant quotas, rate limits, request validation, idempotency, routing, and API-version handling at the gateway.
  3. Apply one versioned deterministic preprocessing contract in offline training, synchronous serving, and batch serving.
  4. Store immutable model artifacts and lineage in the model registry and approved embedding-classifier combinations in the compatibility registry.
  5. Route single-item traffic to online inference and large jobs to a batch orchestrator with queues, chunking, accelerator-aware scheduling, retries, and checkpoints.
  6. Use version-aware, tenant-safe embedding and classification caches where reuse is semantically valid.
  7. Use the metadata store for tenant, quota, scope, alias, and endpoint metadata, and use the optional feature or vector store only when versioned embeddings or indexes are required.
  8. Train embedding and classifier artifacts offline from versioned, quality-checked data and labels.
  9. Evaluate before publishing and never deploy from a training metric alone.
  10. Roll out approved versions with canary, shadow, or feature-flag controls and retain a complete rollback path.
  11. Monitor service health, data quality, drift, model quality, and business outcomes separately.
  12. Join feedback and delayed outcomes to prediction identifiers and versions, then use validated signals as retraining triggers.
  13. Apply PII handling, encryption, retention, deletion, tenant isolation, and scoped security boundaries.
  14. Balance synchronous availability against accelerator, batching, caching, storage, training, and redundant-capacity cost.
Practical Complexity & Trade-offs

The most expensive computation is usually embedding and classification inference on accelerators. Dynamic batching improves accelerator utilization because several compatible items can share one execution batch, but waiting to form a batch can increase latency. Caching repeated computations can reduce both latency and accelerator cost, but caches consume storage and provide little value for unique text. Asynchronous jobs improve throughput and absorb bursts, but they require queue state, retries, checkpoints, result storage, and cleanup. Keeping older approved versions available makes rollback safer but consumes memory and capacity. Strong tenant isolation, encryption, retention controls, monitoring, lineage, and optional vector storage add operational cost. The practical design therefore protects synchronous capacity while allowing large workloads to use queued and efficiently batched accelerator capacity.

Where it is used

This design is useful when one service must provide reusable text embeddings and compatible classifiers to web applications, backend services, data-science notebooks, and large offline jobs. Examples include applications that need embeddings for semantic retrieval, systems that classify text into supported labels, and backfills that process many text items asynchronously. It is especially useful when several classifier versions depend on specific embedding versions and the platform must prevent untested combinations from reaching production.

Why Interviewers Ask This

This question tests whether a candidate can design the complete machine-learning service around text embeddings and classifiers, not just describe model inference. A strong answer connects API contracts, deterministic preprocessing, compatible model versions, synchronous and asynchronous serving, item-level failures, accelerator scheduling, caching, tenant isolation, training and evaluation, monitoring, feedback, safe rollout, privacy, availability, and cost. It also tests whether compatibility, reproducibility, and failure behavior are explicit system contracts rather than assumptions hidden inside application code.

Common interview mistakes

Common mistakes include letting clients implement preprocessing independently; using mutable model names without immutable versions; assuming classifiers are compatible with any embedding of the same dimension; failing an entire batch because one item is invalid; retrying whole batches instead of resuming from checkpoints; omitting idempotency for retried jobs; using cache keys that omit model, preprocessing, classifier, or tenant semantics; letting large batch jobs starve synchronous traffic; training a classifier on embeddings from one version and serving it with another; mixing the responsibilities of the model registry, compatibility registry, metadata store, and vector store; deploying from training metrics without offline evaluation; treating drift as proof of quality loss; joining delayed feedback without prediction identifiers and version metadata; silently falling back to an incompatible model; retaining sensitive text indefinitely; and measuring model execution time while ignoring queueing, preprocessing, networking, and total end-to-end latency.

Interview tip

Draw the synchronous and asynchronous paths first. Then explain the contracts shared by both paths: deterministic preprocessing, immutable model compatibility, versioned artifacts, item-level failure handling, and tenant boundaries. Finish with offline training and evaluation, rollout and rollback, monitoring and feedback, retraining triggers, privacy, availability, and cost.

Interviewer may ask next
What happens if a batch contains valid and invalid items, and a worker crashes after some valid items have already completed?

I would keep validation and execution status at item level. Invalid items become permanent item failures without blocking valid items. Valid work is processed in chunks, and completed chunks are committed with checkpoints. If a worker crashes, a retry resumes from the last committed checkpoint instead of restarting the entire batch. Result writes and job-state transitions are idempotent so replay cannot create duplicate logical results. The final batch can have a partial status, with successful outputs preserved and failed indexes carrying explicit error codes. Retryable infrastructure failures remain separate from permanent validation failures.

How would you change the design if accelerator cost becomes the main constraint but synchronous requests still need reliable service?

I would protect synchronous capacity while making expensive work more efficient. I would increase safe cache reuse with version-aware and tenant-safe keys, use dynamic batching in online serving only within the allowed latency budget, and move large workloads through the asynchronous queue where the scheduler can form larger compatible batches. Autoscaling, tenant quotas, and queue priorities would prevent uncontrolled demand. I would also review retained embeddings, vector indexes, cache size, redundant model copies, and retraining frequency. I would not save money by silently using an incompatible or unevaluated model; compatibility, validation, and rollback remain hard boundaries.

42. Design an internal document-search agent.Machine Learning System DesignEasy

Question Details

Employees ask questions over changing internal documents whose access rights can change after indexing. Cover source ingestion, parsing, chunk and document provenance, versioning and deletion, permission filtering during retrieval, hybrid search, reranking, grounded answer generation, citations, and useful failure on insufficient evidence. Define evaluation questions and relevance labels, model and index registry, serving scale, caching without cross-user leakage, latency and cost budgets, feedback and re-index or retraining cadence, freshness and drift monitoring, injection defenses, auditability, and incident recovery.

Short Interview Answer (30-60 seconds)

I would ingest and version documents with chunk-level provenance, build vector and BM25 indexes, and enforce current ACLs before hybrid retrieval. I would rerank only authorized candidates, generate only from retrieved evidence, attach citations, abstain when evidence is weak, and monitor freshness, quality, security, latency, cost, and feedback.

Detailed Explanation

The system should answer employee questions from changing internal documents without exposing content the employee cannot currently access. I would ingest and parse source documents, keep document and chunk provenance, track versions and deletions, and build both semantic and lexical indexes. At query time, I would fetch the employee's current entitlements and current document ACLs before candidates enter retrieval. Authorized candidates go through hybrid search and reranking, then the LLM receives only the top authorized evidence. The answer includes citations, and the system returns a useful abstention when evidence is insufficient.

Useful Questions to Ask the Interviewer
  1. Which internal source types must be indexed, and how quickly should updates, deletions, and permission changes become visible?
  2. Are permissions defined at document level only, or can individual sections or chunks have different access rules?
  3. What answer behavior is preferred when no authorized evidence is strong enough: abstain, suggest a refined query, or suggest requesting access?
  4. Which matters most for the latency and cost budgets: fastest possible answers, strongest reranking and generation quality, or a balanced target?
  5. What feedback signals are available, such as ratings, corrections, unanswered queries, or citation complaints?
Design an internal document-search agent. diagram
How to Explain It in an Interview
1. Start with the security and freshness contract

The central rule is simple: an employee must receive answers only from evidence they are authorized to access now, not from permissions that were true when a document was indexed. Authorization is therefore part of the online retrieval path. A stale ACL copied into an old index entry is not enough.

Document freshness is also part of correctness. A document update, permission change, or deletion must propagate to the searchable state. The system keeps version and deletion metadata so it can distinguish current content from superseded or removed content.

2. Ingest, parse, and preserve provenance

Connectors ingest content from internal file shares, wikis or knowledge bases, email, databases, and other internal applications. Parsing extracts text and useful structure while normalizing content and handling duplicates where appropriate.

Each chunk keeps provenance such as document ID, chunk ID, source, document version, and a location such as page or section. The document store keeps content and metadata including an ACL reference or policy ID and a deletion or tombstone state. This lineage lets the final answer point back to the exact evidence used.

When content changes, the lifecycle path updates, deletes, or re-indexes the affected entries in both the vector and lexical indexes. Deleted or superseded content must not remain searchable as current evidence.

3. Build complementary retrieval indexes

I would maintain a vector index for semantic similarity and a lexical BM25 index for exact words, names, identifiers, and phrases. Semantic search helps when the employee uses wording different from the document. Lexical search helps when exact terminology matters.

The system also keeps a model and index registry. It records versions of the embedding model, reranker, LLM, index schema, and reproducible configuration so operators can identify exactly which serving state produced an answer and can recover or roll back when needed.

4. Enforce current permissions at query time

For every request, authenticate the employee and fetch the employee's current entitlements together with current document ACLs. Apply those permissions as part of retrieval so inaccessible documents do not enter the usable candidate set.

Then run permission-filtered hybrid search using the vector and lexical indexes. Merge and deduplicate the authorized results while retaining their provenance. The key invariant is that unauthorized content never becomes evidence for reranking or generation.

5. Rerank authorized candidates

Hybrid retrieval aims for strong recall, but first-stage ranking can still contain weak matches. A cross-encoder or LLM-based reranker can score the authorized candidates more precisely and select the strongest evidence for the question.

Reranking can improve relevance but adds latency and cost. I would therefore give it an explicit share of the overall latency and cost budgets rather than using an unlimited candidate set.

6. Generate a grounded answer with citations

The generation prompt contains the employee question plus the selected authorized chunks and their provenance metadata. The LLM is instructed to answer only from retrieved evidence rather than filling gaps from unsupported memory.

The response includes citations that resolve to source, version, and section or equivalent chunk location. Retrieved document text is treated as untrusted data. It cannot override system policy or authorization rules because internal documents themselves may contain malicious or misleading instructions.

If the available evidence is weak, contradictory, missing, or inaccessible, the system should not fabricate an answer. It returns a helpful insufficient-evidence response and can suggest refining the query or requesting access.

7. Evaluate retrieval and answer quality separately

Build an evaluation set of representative employee questions and human relevance labels for documents or chunks. Include normal questions, ambiguous questions, permission-sensitive cases, recently changed or deleted documents, insufficient-evidence cases, and adversarial content.

For retrieval, measure ranking quality with metrics such as nDCG and MRR. These tell us whether relevant evidence appears near the top.

Evaluate generated answers separately using groundedness, citation correctness, and abstention quality. Groundedness asks whether claims are supported by retrieved evidence. Citation correctness checks whether citations support the claims they accompany. Abstention quality checks whether the system refuses appropriately when the evidence is not sufficient.

8. Serve safely at scale

Use a stateless query-serving layer so instances can scale horizontally. Define an end-to-end p95 latency budget and a per-query cost budget, then allocate those budgets across authorization lookup, retrieval, reranking, and generation. End-to-end latency is the user-visible request time, not just model inference time.

Caching requires special care. Cache keys include the user or entitlement scope so results are not shared across users with different permissions. On every cache hit, re-check current ACLs instead of trusting the cached authorization decision. Invalidate affected cache entries when permissions, document versions, or deletion state change.

If the service is overloaded or a downstream model is unavailable, degrade safely. Return a clear temporary failure or another permission-safe fallback instead of bypassing authorization or inventing unsupported evidence.

9. Monitor freshness, drift, quality, and operations

Monitor index freshness so operators know whether source changes are reaching the searchable indexes. Track data and query drift, but do not treat drift by itself as proof that answer quality has declined. Measure retrieval quality, answer quality, latency, cost, and errors separately.

Feedback such as employee ratings and corrections should be linked to the query, retrieved evidence, answer, model version, and index version that produced it. Use those signals to decide whether to re-index content, adjust retrieval or reranking, or retrain or replace a model. The cadence should be driven by freshness, drift, or measured quality signals rather than an invented fixed schedule.

10. Make security, auditability, and recovery first-class requirements

Treat all retrieved content as untrusted input. Apply input and output filtering, and never allow retrieved text to override system policy. Authorization remains outside the LLM so prompt injection cannot grant access.

Audit logs should capture enough information to investigate a response, including an appropriate request identity or audit identifier, query, authorization decision, retrieved sources, relevant model and index versions, and final answer. The logs themselves must follow the organization's access policy.

For incidents, retain reproducible model and index versions, deletion and re-index procedures, and rollback capability. If a bad index, model, configuration, or ingestion change causes incorrect behavior, operators should be able to identify the affected version, stop or roll back serving, rebuild from known inputs, and verify the repaired system before returning it to normal traffic.

Technical Approach
  1. Connect to approved internal sources and ingest document changes.
  2. Parse and normalize content while preserving document and chunk provenance.
  3. Track document versions, ACL references, updates, deletions, and tombstones.
  4. Build and version both a semantic vector index and a lexical BM25 index.
  5. Register embedding, reranker, LLM, index schema, and reproducible configuration versions.
  6. For each employee query, authenticate the user and fetch current entitlements and current document permissions.
  7. Apply current permission filters before inaccessible documents can enter the usable retrieval candidate set.
  8. Run vector and lexical retrieval, merge and deduplicate authorized candidates, and preserve provenance.
  9. Rerank only authorized candidates and select the strongest evidence.
  10. Generate an answer using only retrieved evidence and attach source, version, and section citations.
  11. Abstain with a useful message when evidence is insufficient.
  12. Cache only with user or entitlement scope, re-check ACLs on cache hits, and invalidate caches after permission, version, or deletion changes.
  13. Evaluate retrieval with relevance labels plus nDCG and MRR, and evaluate generated answers with groundedness, citation correctness, and abstention quality.
  14. Monitor freshness, drift, latency, cost, errors, retrieval quality, and answer quality.
  15. Join feedback to the query, evidence, answer, model version, and index version, then trigger re-indexing or model changes when measured signals justify them.
  16. Maintain injection defenses, audit logs, reproducible registries, rollback, and incident-recovery procedures.
Practical Complexity & Trade-offs

The main tradeoff is quality versus latency and cost. Vector plus lexical retrieval improves coverage, but running two searches, merging results, reranking, and calling an LLM adds work to each request. More candidate chunks can improve recall but increase reranking and generation cost. Strong reranking can improve relevance but adds latency. Frequent re-indexing improves freshness but increases ingestion and indexing load. Permission checks add online work, but they are required because access can change after indexing. Caching reduces repeated work, but cache entries must be scoped by user or entitlement, revalidated on cache hits, and invalidated after permission, version, or deletion changes. Larger indexes require more storage and retrieval capacity. Stateless horizontal serving helps query scale, while ingestion and re-indexing run as separate background work. Evaluation and monitoring add maintenance cost, but they are needed to distinguish service failures, stale data, retrieval degradation, answer-quality problems, and security incidents.

Where it is used

This design is useful for internal knowledge assistants that search changing company policies, technical documentation, operational procedures, support knowledge, research notes, project documentation, and other access-controlled enterprise information. It is especially useful when different employees can see different documents and when permissions, documents, or versions can change after the content has already been indexed.

Why Interviewers Ask This

This question tests whether a candidate can design a secure retrieval-and-generation system around changing enterprise knowledge. The important judgment is not just choosing embeddings or an LLM. The candidate must preserve document provenance and versions, enforce current permissions before retrieval results reach the model, combine semantic and lexical retrieval, evaluate both retrieval and generated answers, control caching and serving costs, detect freshness problems, defend against malicious document content, and make the system auditable and recoverable.

Common interview mistakes

Common mistakes are checking permissions only during indexing instead of using current ACLs at query time; allowing unauthorized documents into the usable retrieval or reranking candidate set; using only vector search and missing exact lexical matches; losing document or chunk provenance so citations cannot be verified; failing to remove deleted or superseded content from indexes; letting retrieved text override system policy; sharing cached results across different users or trusting stale cached authorization; evaluating only the LLM while ignoring retrieval ranking; mixing retrieval metrics such as nDCG and MRR with answer-quality metrics; always forcing an answer when evidence is weak; inventing fixed latency, cost, or retraining numbers without requirements; treating drift alone as proof of quality loss; and lacking model/index versioning, audit logs, rollback, or a re-index procedure for incidents.

Interview tip

Lead with the hardest invariant: current permissions are enforced before evidence reaches the LLM. Then walk left to right from ingestion to permission-filtered hybrid retrieval to reranking and grounded generation. Finish with evaluation, caching, monitoring, security, and recovery, and make the insufficient-evidence path explicit.

Interviewer may ask next
What happens if an employee loses access to a document after the document and its chunks were already indexed?

I would not rely on the ACL captured during indexing. At query time, the system fetches the employee's current entitlements and the document's current ACL, then prevents inaccessible content from entering the usable retrieval candidate set. The permission change also triggers invalidation of affected caches and can update or re-index relevant metadata. On a cache hit, the system re-checks ACLs rather than trusting an old authorization decision. This prevents an old index entry or cached result from exposing content after access has been revoked.

How would you reduce latency and cost if reranking and generation become expensive without weakening access control?

I would keep authorization mandatory and optimize the work after it. First, use efficient permission-filtered hybrid retrieval to reduce the candidate set. Then rerank only a bounded number of strong authorized candidates and send only the evidence needed for the answer to the LLM. I would use entitlement-scoped caching for repeated work, but re-check ACLs on cache hits and invalidate entries when permissions, versions, or deletions change. I would measure the complete p95 request path and per-query cost, then tune candidate count, reranker use, context size, and model choice against retrieval quality, groundedness, citation correctness, and abstention quality rather than bypassing security.

43. Design a reliable multi-turn chatbot grounded in an approved document corpus.Machine Learning System DesignEasy

Question Details

Design one end-to-end system for authenticated users. Cover document creation, update, deletion, parsing, chunking, versioning, permission-aware indexing, retrieval and reranking, bounded conversation state, prompt assembly, generation, citation validation, and abstention when evidence is weak. Define offline retrieval and answer labels, model or prompt version registration, serving latency and fallback behavior, feedback and retraining or re-evaluation loops, drift and freshness monitoring, prompt-injection defenses, privacy, audit logs, reliability, and cost controls.

Short Interview Answer (30-60 seconds)

I would version and permission-index approved documents, retrieve and rerank only chunks the authenticated user may access, assemble them with bounded conversation state, generate a grounded answer, validate its citations, and abstain when evidence is weak. I would add offline evaluation, version registries, monitoring, audit logs, fallbacks, security controls, feedback loops, and cost limits.

Detailed Explanation

The system has two connected paths: a document lifecycle path and an online chat path. Approved documents are created, updated, or deleted, then parsed, chunked, versioned, permission-tagged, embedded, and indexed. An authenticated user's query goes through authorization and bounded conversation state before permission-aware retrieval and reranking. The prompt contains the user question, recent conversation context, retrieved evidence, and system rules. The model generates an answer with citations. A validation step checks those citations and the available evidence. Weak evidence leads to abstention or clarification instead of guessing.

Useful Questions to Ask the Interviewer
  1. Which document sources are approved, and who owns approval, sensitivity labels, retention, and deletion?
  2. What authorization model is required: user, group, role, attributes, or a combination?
  3. How quickly must document creates, updates, permission changes, and deletions become visible to retrieval?
  4. What end-to-end latency and availability objectives should the online chat service meet?
  5. How should the product behave when retrieval, reranking, the generation model, or another dependency times out?
  6. What kinds of citations are required: document links, chunk-level supporting spans, or both?
  7. How conservative should abstention be when evidence is incomplete or conflicting?
  8. What user feedback is available for offline evaluation and later re-evaluation?
Design a reliable multi-turn chatbot grounded in an approved document corpus. diagram
How to Explain It in an Interview
1. Define the system contract

The user is authenticated. The chatbot may answer only from the approved corpus and only from documents that the user is authorized to access. A successful response is therefore not merely fluent. It must be permission-safe, grounded in retrieved evidence, and citable. If the evidence is insufficient, the correct result is to abstain or ask a clarifying question.

I would treat one user request and its resulting response as the online serving unit. I would not invent a throughput target, fixed latency number, freshness SLA, or availability target because none is supplied. Instead, I would agree on those requirements with the interviewer and then monitor the resulting end-to-end service objectives.

2. Manage the approved document lifecycle

Approved sources enter a governance and ingestion path. Governance records the document owner or steward, approval state, sensitivity information, and retention policy. Create, update, and delete events must all be represented explicitly.

The parser extracts usable content such as text, tables, and metadata. The chunker creates semantic chunks while retaining useful document structure. Metadata records information needed for filtering, lineage, and citations. Versioning connects every chunk to the exact document version, parent document, and processing state that produced it.

An update creates a new searchable version instead of silently changing the meaning of an old artifact. A deletion must remove or invalidate the affected searchable material. Permission changes must also propagate so a user cannot retrieve content simply because an older index entry still exists. Index freshness is monitored because stale knowledge can produce an incorrect answer even when the model itself has not changed.

3. Build a permission-aware index

For each approved chunk, the indexing path creates an embedding and stores searchable metadata. The design uses a vector index together with metadata filters and access-control information.

The important rule is that authorization is enforced during retrieval. A user's current effective permissions are checked at query time. I would not rely only on permissions that existed when an embedding was originally created, because group membership, document ownership, or access policy may later change.

The index also keeps document and chunk versions so that a citation can identify the exact source used for an answer. Obsolete or deleted versions should not silently compete with the current approved content.

4. Keep conversation state bounded

Multi-turn chat needs context, but sending an unlimited transcript is expensive and increases privacy risk. A session manager keeps bounded conversation history, such as the most recent turns and, when needed, a compact summary of older turns. The conversation state store is encrypted.

Conversation state belongs to the authenticated user or session. It is context for interpreting the current question, not a replacement for approved evidence. It must never bypass document permissions or become a trusted source for factual claims that should be grounded in the corpus.

The API boundary performs authentication and authorization, rate limiting, request validation, and appropriate redaction or protection of sensitive information.

5. Retrieve and rerank permission-safe evidence

The current user query and permitted conversation context are used to perform retrieval. The retriever selects candidate chunks from the permission-aware index using vector similarity plus metadata filters. The current ACL check excludes content the user cannot access.

A reranker then reorders the candidate chunks for relevance. Initial retrieval is optimized for finding a useful candidate set quickly. Reranking can spend more computation on that smaller set and choose a better final Top-N context.

Each selected chunk carries the source identity and version information required for citation validation. Retrieval must never return a chunk merely because it is semantically similar when that chunk is unauthorized.

For offline retrieval evaluation, I would create relevant versus non-relevant chunk labels. Hard negatives are useful because they test whether the retriever and reranker can reject near-miss documents that sound related but do not support the answer.

6. Assemble a controlled prompt

Prompt assembly combines the system prompt and guardrails, bounded conversation state, retrieved context with citation identifiers, and the current user question.

Retrieved documents are treated as data, not as trusted instructions. The system prompt has higher authority and tells the model not to follow instructions found inside retrieved documents. This is an important prompt-injection defense.

The prompt builder also enforces token limits. If too much material is retrieved, I would reduce the number of chunks or use another evaluated context-reduction strategy rather than overflow the model context window or send unbounded input.

7. Generate, validate citations, and make an answer decision

The registered approved generation model receives the assembled prompt and produces a candidate answer with citations. Generation is not the final acceptance step.

Citation validation checks that every cited source exists in the retrieved context, is still permission-valid, and supports the associated claim. The validator also rejects fabricated source identifiers and citations to material that was never supplied to the model.

The response decision then has two mutually exclusive outcomes. If there is sufficient supporting evidence, return the answer with citations. If evidence is weak, missing, conflicting, or fails validation, abstain or ask a clarifying question. The system should not convert uncertainty into an unsupported answer simply to increase answer rate.

For offline answer evaluation, useful labels include grounded versus not grounded and the supporting citation spans. Human evaluation can also record whether an answer is helpful or unhelpful. Retrieval labels test whether the correct evidence was found. Answer labels test whether the final response is actually supported by that evidence.

8. Register serving versions and evaluation results

The model and prompt registry records prompt-template versions, generation-model versions, embedding-model versions, reranker versions, configuration, policies, and change history. Evaluation results are associated with the exact candidate configuration that produced them.

This gives artifact lineage. Instead of saying that 'the chatbot improved,' the team can identify which retrieval, reranking, prompt, embedding, or generation version produced a particular result. A release should be selected using retrieval, answer, safety, and operational evaluation rather than a single model or training metric.

9. Design online serving for safe degradation

The online serving path is API gateway, session manager, retrieval, reranking, prompt assembly, generation, citation validation, and final response decision. End-to-end latency includes all of these stages, not only model generation time.

I would monitor total P50 and P95 latency as shown in the design, along with stage-level latency, error rate, and availability. Timeouts, bounded retries, circuit breakers, bulkheads, health checks, and idempotent request handling help prevent cascading failures and inconsistent retries.

Fallback behavior depends on what failed. Retrieval can retry with fewer documents or use a simpler evaluated retrieval path. Generation can use a registered smaller fallback model when one has already passed evaluation. A cached result may be used only when its permissions, source versions, and freshness are still valid for the current request. If safe grounding cannot be maintained, the fallback is abstention or clarification rather than guessing.

10. Defend against prompt injection and protect privacy

Prompt-injection defenses are layered. Suspicious inputs can be scanned or classified. System instructions remain separate from retrieved content. Retrieved instructions are ignored as control instructions. Tool use, if present, must be constrained by policy. Output guardrails examine the response before it leaves the service.

Privacy controls include least-privilege authorization, appropriate row- or field-level security, encryption in transit and at rest, secrets management, and protection or redaction of personally identifiable information. Authorization is enforced by the application and retrieval layers, never delegated to the language model.

11. Keep immutable audit evidence

Audit logging records who performed the request, when it occurred, which document versions were accessed, the query, retrieved document references, the answer, its citations, and the serving model or prompt version. Sensitive values are redacted according to policy.

The purpose is reproducibility and accountability. If an answer is disputed later, the team can identify the approved evidence and serving configuration that produced it instead of relying on whatever happens to be in the current index.

12. Monitor reliability, quality, drift, freshness, and cost separately

Service monitoring covers latency, errors, availability, and dependency health. Data-quality monitoring covers parsing failures, missing metadata, malformed documents, and indexing failures. Freshness monitoring measures whether approved creates, updates, deletions, and permission changes have reached the searchable index.

Quality and safety monitoring can track abstention behavior, citation quality, groundedness or hallucination-related evaluation results, and user satisfaction when labels are available. Drift monitoring can examine embedding distributions and retrieval behavior. Drift is a signal to investigate, not proof that answer quality has declined.

Cost monitoring covers input and output tokens, retrieval depth, reranker work, embedding volume, caching, model routing, and budget consumption. These signals should remain separate enough that the team can tell whether a problem is caused by service health, data freshness, retrieval quality, model quality, safety, or cost.

13. Close the feedback and re-evaluation loop

User feedback can include positive or negative ratings, corrected answers, missing-document reports, and abuse or injection flags. Feedback should be stored with the original request, retrieved evidence references, final answer, citations, and exact serving versions. That keeps the feedback joined to the correct request and configuration.

Feedback can improve evaluation sets. Retrieval examples receive relevant or non-relevant labels. Answers receive grounded or not-grounded labels and supporting spans. Difficult near-misses become hard negatives.

Candidate retrievers, rerankers, prompts, embedding models, or generation models are compared offline before release. Depending on what changed, the improvement loop may mean retraining a learned component, refreshing the index, changing a prompt, selecting a different model, or simply re-running evaluation. A drift signal alone should not automatically trigger retraining.

14. Deploy and roll back safely

After a candidate passes the required evaluation gates, it can be introduced through a controlled deployment such as canary or A/B rollout when appropriate. The serving path records the exact model, prompt, retriever, reranker, embedding, configuration, and compatible index assumptions used for each response.

If service health, grounding, citation quality, safety, or other required evaluation evidence degrades, traffic should return to the last approved compatible configuration. Rollback therefore covers the full serving configuration, not just the generation model.

15. Control cost without weakening grounding

The design uses input and output token limits, tuned retrieval Top-K and reranked Top-N values, caching where permission and freshness rules allow it, and model routing by request complexity when alternative models have been evaluated.

Reducing retrieval depth or switching models can lower cost and latency, but those changes must pass the same retrieval, grounding, citation, safety, and reliability evaluation. Budget alerts detect unexpected spend. The goal is to remove unnecessary work without turning cost optimization into unsupported generation.

Final takeaway

I would describe this as a permission-aware retrieval-augmented generation system with a controlled document lifecycle and an explicit evidence gate. Authorization, retrieval, generation, and citation validation form one correctness chain. A document can be relevant but unauthorized. An answer can be fluent but unsupported. A citation can exist but fail to support the claim. The system returns an answer only when these checks succeed; otherwise it safely abstains or asks for clarification.

Technical Approach
  1. Define the contract: authenticated users may receive only grounded answers from approved documents they can currently access.
  2. Govern document creation, updates, deletion, approval, sensitivity, and retention.
  3. Parse documents, create semantic chunks, attach metadata, and record document and chunk versions plus lineage.
  4. Generate embeddings and build a vector plus metadata index that carries access-control information.
  5. On each request, authenticate and authorize the user and load only bounded encrypted conversation state.
  6. Retrieve candidates with current permission and metadata filters.
  7. Rerank the candidate chunks and select a small Top-N context with source identifiers.
  8. Assemble system rules, bounded history, retrieved context, citation policy, and the current question.
  9. Generate with a registered approved model.
  10. Validate that cited sources were retrieved, remain authorized, and support the corresponding answer claims.
  11. Return an answer with citations when evidence is sufficient; otherwise abstain or clarify.
  12. Log the request, retrieved sources, answer, citations, and serving versions for audit.
  13. Monitor service health, data quality, freshness, retrieval and answer quality, drift, safety, and cost separately.
  14. Join feedback to the original request and serving versions, create offline retrieval and answer labels, and re-evaluate candidate changes.
  15. Deploy approved versions gradually when appropriate and roll back to the last approved compatible configuration when quality, safety, or reliability gates fail.
Practical Complexity & Trade-offs

The main tradeoff is quality versus latency and cost. Retrieving more chunks can improve recall, but it increases retrieval, reranking, and prompt work. A stronger reranker or generation model may improve answer quality but adds latency and spend. Keeping more conversation history can help multi-turn understanding but increases token use and privacy exposure, so history is bounded. Faster index refresh improves correctness after document or permission changes but requires more ingestion work. Strict permission checks and citation validation add processing, but they are necessary because a fast answer that leaks unauthorized data or cites unsupported evidence is incorrect. Caching can reduce cost and latency, but cached material must still be valid for the current user's permissions, source version, and freshness requirements. The system also pays storage and maintenance costs for versions, embeddings, metadata, conversation state, logs, evaluation sets, registries, and monitoring.

Where it is used

This design is useful for authenticated internal knowledge assistants, policy and procedure chatbots, support systems that must answer from an approved knowledge base, document-heavy research assistants, and other applications where conversational answers must respect access rights, source citations, document freshness, privacy, and safe abstention.

Why Interviewers Ask This

This question tests whether I can design a complete retrieval-augmented chatbot instead of describing only an LLM call. I need to reason about the approved document lifecycle, permission-aware retrieval, bounded conversation state, grounding, citations, abstention, versioning, evaluation, security, privacy, reliability, monitoring, feedback, and cost. It also tests whether I understand that document freshness and authorization are part of answer correctness, and whether I can design safe fallback behavior when retrieval, generation, validation, or infrastructure fails.

Common interview mistakes

Common mistakes are treating the architecture as only vector search plus an LLM; indexing documents without query-time permission checks; forgetting creation, update, deletion, and permission-change propagation; failing to version documents, chunks, prompts, embedding models, rerankers, generation models, configuration, and evaluation results; sending unlimited conversation history instead of bounded state; allowing retrieved text to override system instructions; measuring only vector similarity while skipping reranking or offline retrieval labels; generating citations without checking that the sources were retrieved and actually support the claims; always answering when evidence is weak; reporting only model latency while ignoring end-to-end latency; using retries without timeouts, circuit breakers, or safe degradation; serving stale cached answers without validating permissions and source versions; treating drift as proof of quality loss; collecting feedback without joining it to the original request, evidence, answer, and serving versions; logging sensitive content without privacy controls; deploying a new serving configuration without evaluation and rollback; and reducing retrieval or model quality purely to cut cost without re-evaluating grounding and safety.

Interview tip

Draw two connected flows: document ingestion into a permission-aware versioned index, and the authenticated online request from bounded conversation state through retrieval, reranking, prompt assembly, generation, citation validation, and answer-or-abstain. Then add the version registry, monitoring, audit, security, feedback, reliability, and cost controls. Emphasize that permission checks and evidence validation are part of correctness, not optional extras.

Interviewer may ask next
What would you do if a user's follow-up question depends heavily on older turns, but the conversation has grown beyond the allowed context window?

I would keep conversation state bounded instead of sending the entire transcript. I would retain the most recent turns and, when useful, maintain a compact summary of older context. The current query would be interpreted using that bounded session state, and retrieval would run again against the permission-aware approved corpus. I would never treat the summary as authoritative evidence or let it bypass source grounding. If compression removes information needed to understand the request, I would ask a clarifying question rather than guess. The state and summary remain scoped to the authenticated session, encrypted, and governed by the same privacy and retention controls.

How would the design change if strict latency or cost pressure forced you to reduce retrieval depth or use a smaller generation model?

I would treat those changes as new serving configurations rather than harmless optimizations. I could reduce initial Top-K, reduce reranked Top-N, cache permission-safe and freshness-valid work, or route suitable requests to a smaller registered model. Each candidate configuration would be evaluated on retrieval relevance, grounded answer quality, citation behavior, abstention, safety, latency, reliability, and cost before rollout. During serving, I would monitor the same quality and operational signals. If the cheaper path cannot find sufficient evidence or produce a validated grounded answer, the system should escalate to a stronger approved path when available or abstain instead of saving cost by guessing.

44. Design a low-latency GPU inference service.Machine Learning System DesignMedium

Question Details

Requests arrive online with explicit deadlines, bursty traffic, varying shapes, and batch-dependent GPU service times. Define input and model-version contracts, admission control, shape-compatible dynamic batching, maximum wait, work-aware load balancing, optional cache correctness and invalidation, capacity arithmetic, backpressure and overload. Connect training and evaluation artifacts to a registry and rollout, then cover sustainable throughput versus tail latency, quality and drift monitoring, feedback and retraining, privacy, tenant isolation, failure recovery, and cost.

Short Interview Answer (30-60 seconds)

I would validate each request and model version, reject work that cannot meet its remaining deadline, batch only shape-compatible requests with a bounded wait, and route batches using predicted completion time and GPU load. I would add version-safe caching, bounded queues, overload shedding, sustainable-capacity planning, canary rollout, quality and drift monitoring, tenant isolation, recovery, feedback-driven retraining, and cost controls.

Detailed Explanation

I would design the service as a deadline-aware online inference path. Each request carries a validated input shape and dtype, an explicit deadline, model version, tenant or priority, and an idempotency key. Admission control checks whether the request can still finish within its remaining deadline and whether queue, rate, quota, memory, and resource budgets allow it. Admitted work enters a queue for its model version and compatible shape bucket. The batcher uses both a maximum batch size and a bounded maximum wait, then a work-aware router sends the micro-batch to the GPU expected to finish it soonest. The service protects tail latency with backpressure, safe rollout, monitoring, isolation, recovery, and capacity headroom.

Useful Questions to Ask the Interviewer
  1. Do we have hard per-request deadlines, p95/p99 latency SLOs, or both?
  2. Which input shapes may share a batch, and is padding between compatible shapes allowed?
  3. How bursty is traffic, and may we reject or degrade requests during overload?
  4. Does the caller choose a model version, or does the service resolve an approved active version?
  5. Are inference results deterministic enough to cache, and which request fields affect cache correctness?
  6. What tenant-isolation and quota guarantees are required when GPUs are shared?
  7. What delayed outcomes or feedback are available for evaluating quality and triggering retraining?
Design a low-latency GPU inference service. diagram
How to Explain It in an Interview
1. Define the request and model contracts

The unit of online work is one inference request. At ingress I would authenticate and authorize it, validate its schema, shape and dtype, deadline, model version, tenant or priority, and idempotency key. The model contract identifies the exact versioned artifact and compatible configuration. Invalid requests or unavailable model versions fail safely instead of silently falling back to an unrelated version.

This also keeps deployment behavior explicit. A new model version can coexist with an older one during canary rollout, so every request, cache entry, metric, and feedback record must remain attributable to the correct version.

2. Perform deadline-aware admission control

Admission control prevents hopeless requests from consuming GPU capacity. Let D_remaining be the request deadline minus the current time. A useful feasibility check is:

W_queue + S_batch + L_other <= D_remaining

Here W_queue is predicted queue or batching wait, S_batch is predicted GPU batch service time, and L_other is the remaining non-GPU latency. Admission also considers queue depth, GPU memory pressure, token or rate limits, and per-tenant quotas.

If the request cannot be served within its remaining budget, reject it early or use an explicitly supported fallback. Early rejection is better than allowing the request to occupy scarce capacity and time out later.

3. Use shape-compatible dynamic batching

Requests are grouped by model version and compatible shape bucket. The batcher collects requests until one of several conditions occurs: the maximum batch size is reached, the bounded maximum wait expires, or the oldest request is approaching its deadline.

This is the main performance tradeoff. Larger batches often improve GPU efficiency and throughput, but waiting to assemble a larger batch increases queueing delay and can hurt p95/p99 latency. Therefore maximum batch wait is an upper bound, not a target. If an urgent request is close to its deadline, dispatch a smaller feasible batch early.

4. Route batches using predicted work

Round-robin can perform poorly because GPU service time depends on batch size, shape, model version, and current queued work. The work-aware router should choose the GPU with the best predicted completion time or lowest deadline-miss risk.

Its estimate can use predicted service time, queue length and in-flight work, GPU memory pressure, GPU utilization, data or KV-cache locality when relevant, and tenant fairness. Inside each server, the service queue should use work-aware and deadline-aware ordering with a bounded queue wait.

The model worker may use optimizations such as CUDA Graphs, pinned host memory, and validated reduced precision. Reduced precision should be used only after confirming that model quality remains acceptable.

5. Make caching version-safe

Caching is optional. It is appropriate only when serving a previous result is semantically correct. A safe cache key includes every input that can change the output, such as model version, input hash or content, relevant parameters, shape, and tenant when isolation requires it.

Including the model version prevents cross-version cache hits during rollout. Old-version entries do not require unsafe global invalidation if the key already contains the version; they can expire through TTL or normal eviction. Cache misses continue through the normal GPU path.

6. Plan capacity from sustainable throughput

Capacity should be based on measured batch service behavior, not a peak synthetic number. If E[B] is the expected number of requests per batch and E[S_batch] is expected service time per batch, a simple per-server throughput estimate is:

R_max ~= U_target * E[B] / E[S_batch]

U_target is intentionally below full utilization so the service has headroom for bursts and variance. I would estimate capacity separately for important model and shape buckets because their batch service times may differ.

The final capacity decision must be validated against end-to-end p95/p99 latency and deadline-miss rate. High GPU utilization is not the objective by itself. The goal is sustainable throughput that still satisfies the latency SLO.

7. Apply backpressure before queues become unstable

The service should bound queue depth and react when queue wait, deadline misses, or GPU memory pressure rise. Mitigations include shedding low-priority work, reducing or capping batching wait, scaling out warmed capacity, and returning a retryable failure where the API contract permits it.

Retries should use backoff and jitter so clients do not turn an overload event into a retry storm. Increasing maximum batch wait is not a general overload solution because it can make tail latency and deadline misses worse.

8. Keep the model lifecycle separate from online serving

Offline training produces versioned model, configuration, tokenizer or other required artifacts, plus lineage information. Evaluation should include model-quality checks and serving-oriented tests such as compatibility and latency. A model should not be promoted based only on a training metric.

Approved artifacts are placed in the model registry. Deployment starts with a small canary traffic share, compares quality and SLO behavior, and gradually promotes the new version. If the new version regresses, traffic returns to the previous approved version. GPU workers should not receive traffic until the required artifact is loaded and warmed.

9. Monitor service health, quality, and drift separately

Service monitoring should include request rate, errors, end-to-end duration, queue wait, batch size, GPU utilization, and deadline-miss rate. End-to-end latency is different from GPU model execution time, so both should be measured at the proper boundary.

Model-quality monitoring uses online quality proxies or delayed outcomes when available. Data or embedding drift is a distribution change; it does not by itself prove that model quality declined. Delayed outcomes should be joined back to the correct prediction, request grain, model version, and time before evaluation.

That joined feedback can feed the next training cycle. Retraining may be triggered by validated quality degradation, meaningful data change, or an approved schedule, but every new model still passes evaluation, registration, canary rollout, and rollback safeguards.

10. Protect privacy and tenant isolation

Requests should be authenticated and authorized, network traffic encrypted, secrets protected, and sensitive data minimized. Tenant quotas or equivalent isolation controls should prevent one tenant from consuming all queue, request, or GPU-memory capacity.

Tenant identity must also be included wherever it affects authorization, accounting, or cache correctness. Access to models, features, retrieval data, and logs should follow least privilege, with audit logs for sensitive operations.

11. Design for failure recovery

Health checks remove unhealthy workers from routing. Planned shutdowns drain in-flight work before termination. Failed GPU workers are replaced, reload the required versioned artifacts from durable storage or the registry path, warm up, and become routable only after readiness checks pass.

Retries must preserve deadline and idempotency semantics. A retry that no longer has enough remaining deadline should be rejected rather than admitted automatically. The design should also retain enough capacity headroom that a single worker failure does not immediately create unbounded queues.

12. Control cost without sacrificing the SLO

Cost levers include right-sizing GPU types, increasing safe batching efficiency, improving locality, autoscaling, validated reduced precision, and choosing cheaper capacity only when its interruption characteristics are acceptable. Warm spare capacity costs money but can be necessary for burst handling and recovery.

The final objective is: maximize sustainable throughput subject to tail-latency and deadline-miss SLOs while controlling cost. That keeps optimization focused on user-visible latency and correctness rather than simply maximizing GPU utilization.

Technical Approach
  1. Define the request contract: schema, shape and dtype, explicit deadline, model version, tenant or priority, and idempotency key.
  2. Authenticate, authorize, validate the schema, and verify that the requested model version is available.
  3. Estimate remaining latency and admit only requests with enough deadline and resource budget.
  4. Place admitted requests into queues grouped by model version and compatible shape bucket.
  5. Form micro-batches using maximum batch size, bounded maximum wait, and oldest-request deadline headroom.
  6. Route each micro-batch to the GPU with the best predicted completion time while considering queued work, GPU memory and utilization, locality, and tenant fairness.
  7. Execute with the correct versioned model using only validated runtime optimizations.
  8. Use the optional cache only with a complete model-version-aware key; otherwise follow the normal GPU path.
  9. Bound queues and apply shedding, fallback, retryable rejection, or scale-out before overload creates unbounded tail latency.
  10. Estimate sustainable capacity from expected batch size and batch service time, then validate capacity against p95/p99 end-to-end latency and deadline-miss rate.
  11. Train and evaluate versioned artifacts offline, register approved versions, canary deploy them, gradually promote them, and roll back on regression.
  12. Monitor service health, quality, drift, feedback, tenant usage, failures, and cost as distinct signals, and feed validated outcomes into retraining.
Practical Complexity & Trade-offs

The largest tradeoff is batching. Bigger batches can process more requests per GPU operation and lower cost per request, but waiting for a bigger batch adds queueing delay. Work-aware routing is better than simple round-robin, but it needs service-time estimates and queue-state information. More replicas reduce queueing and improve recovery, but they cost more and can sit idle. Caching can save GPU work, but an incomplete cache key can return the wrong model version or leak tenant-specific results. Strong isolation, monitoring, warmup, rollback, and feedback handling add operational complexity, but they make the system safer under bursty traffic and model changes.

Where it is used

This design is useful for online GPU-backed model APIs where responses must arrive quickly, request shapes vary, batching materially changes service time, and traffic can arrive in bursts. Examples include interactive generation, ranking, embedding or retrieval services, computer-vision inference, and other real-time prediction systems where multiple model versions may coexist during rollout and the service must balance tail latency, throughput, reliability, tenant isolation, and infrastructure cost.

Why Interviewers Ask This

This question tests whether the candidate can design an online GPU inference system around end-to-end latency rather than model execution time alone. It evaluates request and model-version contracts, deadline-aware admission, shape-compatible dynamic batching, work-aware GPU routing, capacity reasoning, cache correctness, overload protection, rollout safety, monitoring, privacy, tenant isolation, failure recovery, retraining, and cost. The key judgment is balancing GPU efficiency and sustainable throughput against queueing delay, p95/p99 latency, and explicit request deadlines.

Common interview mistakes

Common mistakes are optimizing only GPU execution time instead of end-to-end latency; using round-robin without considering queued work; mixing incompatible shapes in one batch; waiting too long for a full batch; accepting requests that cannot meet their remaining deadline; treating maximum GPU utilization as the objective; using a cache key that omits model version or tenant-sensitive inputs; allowing unbounded queues; increasing batching wait as a generic overload response; allowing retry storms; promoting models from training metrics alone; treating drift as proof of quality loss; mixing service-health and model-quality metrics; failing to join delayed outcomes to the correct prediction and model version; allowing one tenant to monopolize capacity; and routing traffic to a worker before the correct model is loaded and warmed.

Interview tip

Draw the online path first: request contract, admission, shape-compatible batching, work-aware routing, and GPU execution. Explain the batching-versus-tail-latency tradeoff next. Then cover capacity, overload, version-safe caching, rollout, monitoring, feedback and retraining, isolation, recovery, and cost. Keep returning to the same objective: sustainable throughput without violating deadline or tail-latency SLOs.

Interviewer may ask next
What would you do if one request is close to its deadline while its shape-compatible queue is still waiting for more requests?

I would dispatch before reaching the preferred batch size. Maximum wait is an upper bound, and the oldest request's remaining deadline must also control dispatch. If W_queue plus predicted batch service time plus remaining non-GPU latency is approaching D_remaining, I would send the currently feasible batch early. If even immediate dispatch is predicted to miss the deadline, admission or overload logic should reject the request or use an explicitly supported fallback instead of consuming GPU capacity for work that is already too late.

How would the system react if traffic suddenly doubled and p99 latency and deadline misses started rising?

I would protect the SLO before trying to maximize throughput. Admission control should reduce accepted work, bounded queues should trigger shedding or retryable rejection, and batching wait should be reduced or capped if queueing is contributing to misses. Work-aware routing should avoid overloaded GPUs, and autoscaling can add warmed capacity when available. I would inspect end-to-end latency, queue wait, deadline-miss rate, batch-size distribution, GPU utilization, and memory pressure together. Larger batches are useful only when their throughput gain still fits inside request deadlines.

45. Design a real-time sequential personalized playlist recommender.Machine Learning System DesignMedium

Question Details

Generate and order exactly 10 tracks from 10,000 candidates before playback; the list cannot adapt midway. Use 28 days of playback, skip, replay, save, and sequence logs plus track, user, and request context. Define satisfaction labels, point-in-time features, exposure and position bias, candidate generation, item scoring, sequence-aware reranking, diversity policies, model registry, real-time serving and deterministic fallback, offline and online evaluation, feedback, drift and retraining, sensitive-feature governance, reliability, privacy, security, and cost.

Short Interview Answer (30-60 seconds)

I would train offline on 28 days of point-in-time exposure logs, adjust for exposure and position bias, and serve an approved model. Online, I would retrieve and score candidates, sequence-rerank for satisfaction and diversity, return exactly 10 fixed tracks, and use a deterministic eligible fallback on failure.

Detailed Explanation

The system must choose and order exactly 10 tracks from 10,000 candidates before playback starts, and the sequence cannot change during that session. I would use 28 days of playback, skip, replay, save, and sequence logs for offline learning, while each online request supplies user and request context. Training uses point-in-time features, post-exposure satisfaction labels, and exposure and position-bias adjustment. Serving retrieves a manageable candidate pool, scores each track, sequence-reranks the pool for satisfaction and diversity, then returns the fixed 10-track list or a deterministic eligible fallback.

Useful Questions to Ask the Interviewer
  1. How should satisfaction be defined from playback, skip, replay, and save behavior?
  2. Are there required eligibility, diversity, or repetition rules for the final 10 tracks?
  3. Which user, track, and request-context fields are available and approved for training and serving?
  4. What end-to-end response-time requirement must be met before playback begins?
  5. What behavior should the deterministic fallback use when the approved model is missing or the online path times out?
Design a real-time sequential personalized playlist recommender. diagram
How to Explain It in an Interview
1. Define the decision clearly

The user-facing decision is one ordered playlist of exactly 10 tracks selected from 10,000 candidates. The full sequence is produced before playback begins. It does not adapt after the session starts.

The first model output is a per-track satisfaction score for a candidate track under the current user and request context. Those scores are inputs to a second sequence-aware reranking step. The final prediction delivered to the product is the ordered 10-track sequence.

2. Build the offline history at the correct time boundary

Use the provided 28 days of playback, skip, replay, save, and sequence logs. Historical examples should represent tracks that were actually exposed in a request and the outcomes observed after those exposures.

For every logged request, use only feature values that were available at that request time. This is a point-in-time feature rule. It prevents future information from leaking into an older training example. The feature context can include the candidate track, user, and request context supplied by the problem, but no future values should enter the example.

3. Define satisfaction labels from post-exposure behavior

Playback, skip, replay, and save events are evidence about satisfaction after a track was shown. The exact label formula is not supplied, so I would agree on that definition with the interviewer instead of inventing unsupported weights.

The important timing rule is that the label comes from behavior observed after exposure, while the model features come only from information available when the request was made.

4. Adjust for exposure and position bias

Historical logs are not an unbiased sample of all 10,000 tracks. The previous recommender determined which tracks users saw, and the shown position can also influence observed behavior.

I would therefore adjust for exposure and position bias during offline learning. Propensity weighting is one possible method when reliable exposure propensities are available. The goal is to reduce bias, not to claim that observational bias has been completely eliminated.

5. Train offline and use an approved model version

Offline training produces the per-track satisfaction model. Training is not part of the synchronous recommendation request.

Evaluate the trained model before it is used online. Store the approved version in the model registry. The approved model version is then supplied to the Retrieve + Score stage. This keeps serving tied to a known model version rather than an arbitrary training artifact.

6. Retrieve a manageable candidate pool

A real-time request carries the current user and request context. Candidate generation starts from the 10,000-track universe and reduces it to a manageable pool for scoring.

This stage should retain only tracks that are valid for the recommendation request. The exact pool size is not supplied, so I would not invent one.

7. Score each candidate track

Use the approved model version to compute a per-track satisfaction score for each candidate using the candidate track, user, and request context.

At this point, the scores describe individual candidates. They are not yet the final playlist because choosing the best individual tracks independently can create a poor overall sequence.

8. Sequence-rerank the candidates

Feed the scored candidates into a sequence-aware reranker. This step considers the order of tracks and the user's history when constructing the list.

Optimize for sequence satisfaction while applying diversity and repetition policies. For example, the reranker should avoid unnecessary repetition when that would hurt the overall experience. The result is one ordered sequence containing exactly 10 tracks.

The main tradeoff is that sequence-aware reranking requires more computation than simply taking the highest individual scores, but it directly matches the requirement to optimize an ordered playlist rather than isolated tracks.

9. Serve exactly 10 fixed tracks

Return the ordered playlist of 10 tracks before playback begins. Once returned, the order stays fixed for that session.

The full online path is therefore: online request context → Retrieve + Score → Sequence Rerank → Serve 10 Tracks. Offline training remains outside that synchronous path.

If the model is missing or the request path times out, use a predefined deterministic eligible fallback ranking. The fallback still needs to produce a valid ordered playlist rather than leaving the user without a result.

10. Evaluate and learn from delayed outcomes

After playback, collect the supplied outcome signals: playback, skip, replay, and save, together with the shown positions. Join those outcomes back to the request that produced the playlist and the positions that were actually shown.

Offline evaluation should measure ranking and sequence quality. Online evaluation should examine user satisfaction signals such as skips and saves. Monitor both quality and drift. Drift means the data or model-input distribution changed; it does not by itself prove recommendation quality declined.

Joined feedback becomes new evidence for future offline training. Retraining can be triggered when enough new joined data is available or another approved trigger is reached. The newly trained version should still be evaluated before becoming the approved registry version.

11. Add safeguards around the serving path

Use access controls for sensitive features and perform subgroup fairness audits where appropriate. Protect user data with privacy and security controls.

Reliability, end-to-end response time, and cost are also serving constraints. Candidate retrieval, scoring, and sequence reranking all happen before playback, so their combined work must fit the required serving budget. The question gives no numeric latency or cost target, so those values should be clarified rather than invented.

Technical Approach
  1. Define the output as one ordered list of exactly 10 tracks chosen from 10,000 candidates before playback; keep it fixed for the session.
  2. Build offline examples from 28 days of logged exposures, playback, skips, replays, saves, and sequence history.
  3. For each historical request, construct point-in-time candidate-track, user, and request-context features using only information available at that time.
  4. Derive satisfaction labels from post-exposure behavior without inventing unsupported label weights.
  5. Adjust the training process for exposure and position bias, using a method such as propensity weighting only when the needed propensities are reliable.
  6. Train and evaluate a per-track satisfaction scorer offline, then register the approved model version.
  7. For an online request, generate a manageable candidate pool from the 10,000-track universe.
  8. Score each candidate track with the approved model using the user and request context.
  9. Sequence-rerank the scored pool jointly for sequence satisfaction while applying diversity and repetition policies.
  10. Return exactly 10 ordered tracks before playback and keep that sequence fixed for the session.
  11. If the model is missing or the online path times out, return a predefined deterministic eligible fallback ranking.
  12. Join post-playback outcomes and shown positions back to the originating request, evaluate ranking and sequence quality, monitor quality and drift, and feed new joined data into the next offline retraining cycle.
Time & Space Complexity

The main online cost comes from candidate generation, per-track scoring, and sequence reranking. Candidate generation reduces the 10,000-track universe so the more expensive scoring and reranking steps operate on a smaller pool. A richer sequence reranker can improve the final ordering but increases end-to-end response time and compute cost. Offline work also has cost because point-in-time joins and bias adjustment require careful data processing. Operationally, model versioning, feedback joins, fairness audits, privacy controls, monitoring, retraining, and fallback behavior add maintenance work. The question does not provide numeric latency, throughput, or cost limits, so those values should be treated as requirements to clarify rather than assumed.

Where it is used

This design is useful whenever a product must create a complete personalized sequence before consumption starts and cannot change the sequence midway. Examples include a fixed music playlist, a preordered video queue, or another recommendation experience where user history, request context, ordering, diversity, and position effects matter.

Why Interviewers Ask This

This question tests whether a candidate can design a recommendation system that is correct both statistically and operationally. The key judgment is separating offline learning from the real-time request path, creating point-in-time training examples, handling exposure and position bias, scoring individual tracks before sequence-aware reranking, serving a fixed 10-track list safely, using an approved model version, joining delayed feedback correctly, and balancing fairness, privacy, reliability, latency, and cost.

Common interview mistakes

Common mistakes include leaking future information into historical features; treating logged exposures as an unbiased sample; ignoring exposure and position bias; training synchronously in the online request path; taking the 10 highest independent scores without sequence-aware reranking; forgetting diversity or repetition policies; changing the playlist after playback starts even though the requirement says it is fixed; using an unapproved model instead of the registry version; sending the approved model directly to final serving rather than using it for scoring; omitting a deterministic eligible fallback for timeout or missing-model cases; joining outcomes without the original request and shown position; treating drift alone as proof that quality declined; ignoring sensitive-feature governance, privacy, security, reliability, or cost; and inventing numeric latency, throughput, pool-size, or metric targets that the question does not provide.

Interview tip

Draw the synchronous path first: online request → Retrieve + Score → Sequence Rerank → Serve 10 Tracks. Then add offline training, the approved model registry, delayed feedback, retraining, fallback, and safeguards around that path. This makes the training-serving boundary and the fixed-sequence requirement easy to explain.

Interviewer may ask next
How would you handle exposure and position bias in the 28 days of historical logs?

I would build examples at the logged request, exposed-track, and shown-position grain and keep only features available at that request time. Historical outcomes are biased because an earlier recommender chose what users saw, and position can affect behavior. If reliable exposure propensities are available, propensity weighting is one possible adjustment. I would also inspect weight stability because very small propensities can create high variance. I would treat the adjustment as bias reduction, not proof that all confounding has been removed, and I would evaluate the resulting model before approving its registry version.

What would you do if sequence reranking is too slow to meet the required pre-playback response time?

I would reduce online work while keeping the same decision contract. Candidate generation can produce a smaller manageable pool, and the sequence reranker can use a bounded deterministic method that still enforces the required diversity and repetition policies. I would measure end-to-end response time across retrieval, scoring, reranking, and serving rather than looking only at model compute time. If the request still times out or the model is unavailable, I would return the predefined deterministic eligible fallback. The system must still produce exactly 10 ordered tracks before playback, and the sequence must remain fixed for the session.

46. Design a production large-language-model inference serving system.Machine Learning System DesignHard

Question Details

Serve one or more immutable model versions for requests with different prompt lengths, output limits, and streaming needs. Define API validation, tokenization, admission and cancellation, registry and rollout, accelerator memory for weights, activations, and key-value cache, prefill and decode scheduling, continuous batching, fairness, parallelism, warm routing, autoscaling, overload, partial-stream retry boundaries, and crash recovery. Include quality evaluation labels, monitoring of each latency segment and token throughput, feedback, drift and retraining, privacy, tenant isolation, security, and cost.

Short Interview Answer (30-60 seconds)

I would put validated, token-aware admission and version-aware warm routing in front of accelerator workers. Workers use continuous batching, separate prefill and decode scheduling, bounded KV-cache memory, and the required parallelism. Immutable model versions support controlled rollout and rollback. I would add streaming cancellation, overload protection, autoscaling, segmented latency and token-throughput monitoring, quality feedback, privacy, tenant isolation, security, and cost controls.

Detailed Explanation

The system serves one or more immutable LLM versions while prompt length, output limit, tenant, and streaming behavior vary by request. The main online path is client to API validation, admission, routing, warm inference workers, and streamed response. Admission is token-aware because short and long requests consume very different amounts of accelerator memory and compute. Workers separate prefill from autoregressive decode, keep a per-request KV cache, and continuously batch compatible work. Around that path, the design needs controlled model rollout, cancellation, overload handling, crash recovery, autoscaling, observability, quality evaluation, feedback, privacy, tenant isolation, security, and cost management.

Useful Questions to Ask the Interviewer
  1. Must the API support streaming, non-streaming, or both?
  2. Can callers choose a model version, or should version selection come only from rollout policy?
  3. What fairness policy matters most: equal tenant sharing, priority classes, or latency protection for interactive traffic?
  4. Under overload, should requests be rejected, queued for a bounded time, or degraded to a smaller model or shorter output limit?
  5. Which quality labels matter most, such as helpfulness, correctness, safety, or task-specific quality?
  6. What privacy, retention, and tenant-isolation requirements apply to prompts, outputs, traces, logs, KV state, and feedback?
Design a production large-language-model inference serving system. diagram
How to Explain It in an Interview
1. Validate the request before using accelerator capacity

Clients can include interactive applications, backend services, partners, and batch callers. The API gateway authenticates and authorizes the caller, applies per-tenant rate limits, validates the schema, assigns a request ID, and checks model selection, prompt constraints, context-window limits, output-token limits, streaming options, and any required content-safety rules.

An idempotency key can protect request-side bookkeeping such as duplicate charging or duplicate submission handling. It should not be described as making stochastic generation identical after a retry.

2. Tokenize and perform token-aware admission

Resource demand is driven by tokens, not simply by request count. After validation, the system determines prompt length and considers the allowed generation budget. Admission uses bounded queues, concurrency limits, token budgets, and available accelerator or KV-cache capacity.

Per-tenant queues and fairness are important because one tenant or a few very long requests should not monopolize the system. A deficit-style round-robin policy is one reasonable design when request costs differ. Priority classes can be supported, but priority should be bounded so lower-priority traffic is not starved forever.

When the service is full, apply backpressure instead of allowing an unbounded queue. The diagram uses bounded queuing and 429-style shedding as an overload response.

3. Route through a versioned control plane

The model registry stores immutable versions together with the serving artifacts needed to reproduce that version, including weights, tokenizer, configuration, context limits, and quantization or adapter metadata when applicable. Optional LoRA or adapter variants can be cataloged separately while preserving an immutable base-version contract.

Rollout policy can direct a controlled percentage of traffic to a new version using canary, percentage-based, A/B, or multivariant routing. A router then selects a compatible warm worker using the chosen model version, capacity, accelerator locality, and data locality.

Warm routing matters because loading large weights can take significant time. Rollback is therefore primarily a routing change back to a previously registered immutable version rather than an in-place mutation of the current artifact.

4. Manage accelerator memory explicitly

The worker pool can use GPUs or other supported accelerators. Memory is divided among long-lived model weights, temporary activations, and per-request KV cache. The KV cache stores attention keys and values for previously processed tokens so autoregressive decode does not recompute the entire prefix at every step.

KV-cache demand grows with active sequence length and concurrency, so it is a first-class admission and scheduling constraint. Paged or block-based KV allocation can reduce fragmentation and release memory in smaller units as sequences finish or are cancelled. Optional cache spill to fast local storage can be used only when the serving runtime supports it and the latency tradeoff is acceptable.

Quantized weights, including formats such as AWQ or GPTQ when supported, can reduce memory pressure, but they must be quality-tested and hardware-compatible rather than assumed to be a free optimization.

5. Separate prefill and decode

Prefill processes the prompt, creates the initial KV cache, and performs the initial model computation. Decode is autoregressive: each scheduling step chooses the next token, updates the KV cache, and repeats until a stop condition or output limit is reached.

The two phases have different workload shapes. Long prefills can delay interactive decode work if allowed to dominate the device. Chunked prefill can break a large prompt into smaller pieces so decode work can continue between chunks.

Continuous batching means the batch changes over time. Finished or cancelled sequences leave, and new sequences can join at later scheduling iterations. This usually improves accelerator utilization and token throughput compared with waiting for every request in a fixed batch to finish together.

6. Choose parallelism based on model shape and hardware topology

Tensor parallelism can split tensor computation across accelerators. Pipeline parallelism can split layers across stages. Sequence parallelism can distribute supported sequence-related computation, and expert parallelism is relevant for mixture-of-experts models.

These techniques are not all required at the same time. More parallelism can let a model fit across more devices or increase capacity, but it also adds communication and synchronization overhead. Fast interconnects such as NVLink or InfiniBand can reduce that overhead where the hardware topology supports them.

The router therefore needs to select a worker group whose model version and parallel configuration match the request.

7. Schedule decoding and stream results

The decode loop selects next tokens according to the model's configured decoding policy. Depending on the product contract, this can include deterministic or stochastic controls such as temperature, top-k, top-p, minimum-p, repetition penalties, or speculative decoding. These are model-serving configuration choices, not substitutes for admission or scheduling.

For streaming clients, tokens are returned incrementally over a streaming protocol such as SSE or WebSocket. Network backpressure must be bounded so a slow client does not create an unlimited output buffer.

The worker should return final usage information and a finish reason when generation ends normally.

8. Propagate cancellation end to end

A client disconnect, explicit cancellation, or expired request deadline should propagate from the request manager to queue and worker state. The scheduler stops future generation as soon as it is safe, removes the sequence from subsequent scheduling iterations, and frees its KV-cache blocks and related request resources.

This matters financially as well as operationally because generating tokens for a disconnected client wastes accelerator time.

9. Define partial-stream retry boundaries

Before the first token has become visible to the client, a failed request can often be retried on another compatible warm worker if the deadline and idempotency policy allow it.

After part of the response has already been streamed, blindly restarting is unsafe because the client can receive duplicate text or a different continuation. The simplest contract is to terminate the stream with an error and require the client to explicitly restart.

If resumable streams are required, the system needs an explicit checkpointing and resume design. The checkpoint must contain enough generation state for the serving runtime's recovery semantics, along with stream position and request identity. Checkpointing adds I/O and complexity, so it should be used only when the reliability requirement justifies it.

10. Handle overload and degraded service explicitly

Every queue should be bounded. Under overload, the service can shed requests, return a retryable response, or apply an explicitly approved degradation such as a smaller model or lower maximum output-token limit. It should never silently switch quality levels unless that behavior is part of the product contract.

Circuit breakers can isolate unhealthy worker groups or dependencies. Multi-region failover can provide higher resilience when required, but it increases cost, replication complexity, and data-governance considerations.

11. Autoscale on LLM-specific work signals

Request count alone is a weak autoscaling signal because requests have different token lengths. Better inputs include queued token work, active sequences, accelerator utilization, accelerator memory pressure, KV-cache pressure, prefill load, decode load, and observed input- and output-token throughput.

Autoscaling must account for warm-up time. A newly allocated worker is not useful until the required model weights, tokenizer, configuration, and runtime are loaded. Scale-in should drain workers so active streams are not terminated abruptly.

Keeping more workers or model versions warm reduces cold-start latency but increases idle accelerator cost.

12. Recover from worker crashes

A failed worker must be removed from routing immediately. New traffic goes to healthy compatible workers.

If the crash occurs before any visible output, the request can be retried when its remaining deadline and policy permit it. If output was already streamed, the retry follows the partial-stream rule above.

Model artifacts belong in durable storage rather than only on ephemeral worker disks, so replacement workers can load the same immutable version. High-speed local storage can cache artifacts or runtime data, but it should not become the only durable copy.

13. Observe every important latency segment

End-to-end latency is not the same as model execution time. I would measure queue or admission latency, prefill latency, time to first token, decode latency per generated token, network or streaming latency, and total client-visible latency.

Throughput should be measured in tokens, including input tokens processed and output tokens generated, not only requests per second. The observability layer should also track accelerator utilization, memory pressure, errors, saturation, cost, traces, logs, alerts, and SLOs. Request and correlation IDs allow one request to be followed across gateway, queue, router, worker, and stream.

14. Measure quality separately from service health

A healthy accelerator does not prove that the model is producing good answers. Quality therefore needs separate labels and evaluation. The diagram uses human labels such as helpfulness, correctness, and safety, plus model-assisted evaluation using a reference or rubric when appropriate.

New versions should pass quality and safety evaluation before broad rollout. Canary traffic can provide additional production evidence. Quality, safety, service health, drift, and cost remain separate signals because one does not prove another.

15. Build a privacy-aware feedback loop

Prompts, outputs, request metadata, and opt-in feedback can feed the evaluation pipeline under the applicable retention and privacy policy. Sensitive information should be redacted or protected before long-term storage when required.

The feedback record must remain associated with the exact request and model version that generated the response. Feedback may include human ratings, review labels, or carefully defined implicit signals.

Compressed request and response storage can be used when policy permits. Feature or metrics stores can hold aggregated operational data. A vector database for retrieval-augmented generation is optional and belongs only in requests that actually use RAG; it is not required for base LLM inference serving.

16. Treat drift as a signal, not proof of quality loss

Analytics can monitor distribution changes in prompts or embeddings and compare quality trends by traffic slice. Drift means usage changed; it does not prove that quality declined.

If drift or feedback suggests a problem, investigate quality labels and error analysis. If improvement is justified, curate an approved dataset, retrain or fine-tune offline, evaluate the candidate, register it as a new immutable version, and deploy it through the same controlled rollout path.

Offline retraining and online inference remain separate systems. Production serving never edits a deployed model artifact in place.

17. Apply privacy, security, and tenant isolation end to end

Authentication establishes who is calling. Authorization controls which models or operations that caller may access. Per-tenant queues, quotas, budgets, and namespaces reduce noisy-neighbor effects. Higher-risk workloads can use dedicated worker pools when stronger isolation is required.

Prompts, outputs, metadata, traces, and feedback should be encrypted in transit and protected at rest according to policy. API keys and encryption keys should be managed through a secrets-management system rather than application source code.

Audit logs record security-relevant actions. PII protection and data-retention policies apply to request, response, trace, log, and feedback stores. Content-safety guardrails can protect inputs and outputs, but they are separate from authentication and authorization.

18. Treat cost as a design constraint

The main serving costs come from accelerator time, memory, warm idle capacity, interconnect use, storage, and inefficient scheduling. Continuous batching, effective KV-cache allocation, suitable quantization, right-sized models, and autoscaling can lower cost.

Every optimization has a tradeoff. Larger batches can raise throughput but hurt latency. More warm replicas reduce cold starts but cost more while idle. Dedicated tenant workers improve isolation but reduce pooling efficiency. Quantization can lower memory use but requires quality validation. More checkpointing improves possible recovery but increases I/O and implementation complexity.

The system should therefore track cost together with latency, token throughput, quality, reliability, and SLOs instead of optimizing accelerator utilization alone.

Final design

The final architecture has a secure API gateway, token-aware admission queues, cancellation-aware request management, a version-aware warm router, an immutable registry with controlled rollout, and an accelerator worker pool with explicit weight, activation, and KV-cache memory management. Workers use separate prefill and decode scheduling, continuous batching, and the parallelism required by each model. Streaming includes backpressure and clear partial-stream retry boundaries. Bounded queues, shedding, circuit breakers, autoscaling, worker draining, optional checkpoints, and multi-region failover provide resilience. Observability measures each latency segment and token throughput. A privacy-aware feedback and evaluation loop supports quality analysis, drift detection, and creation of new immutable versions. Tenant isolation, security, and cost controls apply across the entire lifecycle.

Technical Approach
  1. Define the request contract: model choice, prompt, maximum output tokens, streaming mode, tenant identity, deadline, request ID, and idempotency behavior.
  2. Validate authentication, authorization, schema, context and output limits, quotas, model availability, and required safety rules.
  3. Tokenize the prompt and calculate the token budget used for admission.
  4. Place admitted work into bounded tenant- and priority-aware queues using concurrency, token, and KV-cache constraints.
  5. Propagate cancellation from disconnect, explicit cancel, or deadline expiry through queues, scheduler state, generation, and KV-cache release.
  6. Select an immutable model version through registry and rollout policy, then route to a compatible warm worker using capacity and locality information.
  7. Reserve accelerator memory for weights, activations, and per-request KV cache; use block or paged allocation when supported.
  8. Schedule prefill and autoregressive decode separately; optionally chunk large prefills so they do not monopolize execution.
  9. Use continuous batching so completed or cancelled sequences leave and new work can join during later scheduling steps.
  10. Apply tenant-aware fairness so long prompts or high-volume tenants cannot monopolize shared capacity.
  11. Select tensor, pipeline, sequence, or expert parallelism only when required by the model architecture, memory limits, and hardware topology.
  12. Stream tokens with bounded network backpressure and return final usage and finish information.
  13. Retry before first visible output only when the deadline and idempotency policy allow it. After partial output, use an explicit resume protocol or terminate and require a new request.
  14. Bound every queue. Shed excess requests or use only explicitly approved degradation such as a smaller model or lower output limit.
  15. Autoscale from queued token work, utilization, memory or KV pressure, and token throughput while accounting for model warm-up time; drain workers during scale-in.
  16. Remove unhealthy workers from routing and recover requests according to their retry boundary. Use durable artifacts so replacements can load the exact immutable version.
  17. Measure queue, prefill, time-to-first-token, decode-per-token, network, and end-to-end latency plus input- and output-token throughput, utilization, errors, saturation, and cost.
  18. Evaluate quality separately using human or rubric-based labels and join feedback to the exact request and model version.
  19. Monitor distribution drift and quality trends separately. Retrain or fine-tune offline only when evidence justifies it, evaluate the candidate, register a new immutable version, and roll it out gradually with rollback available.
  20. Apply tenant isolation, encryption, secrets management, audit logging, PII protection, retention policy, content-safety controls, and cost budgets across the full request and feedback lifecycle.
Practical Complexity & Trade-offs

This is mainly a compute, memory, scheduling, and operations tradeoff rather than a traditional Big-O problem. Prefill cost grows with prompt work, while decode repeatedly performs next-token computation for active sequences. KV-cache memory grows with sequence length and concurrency, so long contexts can reduce the number of simultaneous requests. Continuous batching improves accelerator utilization, but aggressive batching can increase per-request latency. Parallelism helps large models fit across accelerators, but communication and synchronization add overhead. Keeping more models warm lowers routing latency but consumes expensive idle memory. Autoscaling reduces idle cost but can react slowly because model loading takes time. Quantization reduces memory use but requires quality validation. Dedicated tenant workers improve isolation but reduce pooling efficiency. Checkpointing and multi-region recovery improve resilience but add storage, network, and operational cost. The practical goal is to balance latency, token throughput, quality, reliability, privacy, isolation, and cost.

Where it is used

This design is useful for interactive chat, assistants, coding and analysis tools, API-based generation, batch clients, and other production applications that serve one or more LLM versions to concurrent users. It is especially useful when prompt and output lengths vary widely, clients need streamed tokens, multiple tenants share accelerator capacity, model versions must be rolled out safely, and operators need explicit latency, throughput, quality, privacy, security, resilience, and cost controls.

Why Interviewers Ask This

This question tests whether a candidate can design an LLM serving system as a complete production service rather than only explain model execution. The interviewer is looking for judgment around validation, token-aware admission, immutable model versions, safe rollout, accelerator memory, prefill and decode scheduling, continuous batching, fairness, parallelism, warm routing, streaming, cancellation, autoscaling, overload protection, crash recovery, observability, quality evaluation, feedback, privacy, tenant isolation, security, and cost. A strong answer also explains the tradeoffs between latency, token throughput, quality, reliability, isolation, and accelerator utilization.

Common interview mistakes

Common mistakes include admitting requests only by request count instead of token and memory demand; using unbounded queues; ignoring cancellation after disconnect; failing to separate prefill from decode; describing continuous batching as a fixed batch; forgetting per-request KV-cache memory; assuming more parallelism always improves performance; routing to workers without the requested model warm; autoscaling only on request count; measuring only total latency instead of queue, prefill, first-token, decode, network, and end-to-end segments; reporting requests per second without token throughput; retrying a partially streamed generation as though no output had been exposed; assuming an idempotency key guarantees identical regenerated text; mutating a production model instead of creating a new immutable version; deploying based only on an offline metric; treating drift as proof of quality decline; mixing service-health and model-quality signals; storing prompts or feedback without privacy and retention controls; failing to isolate tenant queues, quotas, budgets, or sensitive data; assuming optional RAG infrastructure is required for every request; and optimizing accelerator utilization so aggressively that latency, fairness, quality, reliability, or cost becomes unacceptable.

Interview tip

Present one request journey first: validate, tokenize, admit, route, prefill, decode, stream, cancel if needed, and release resources. Then add the control loops around it: version rollout and rollback, autoscaling, overload and recovery, observability, quality feedback, retraining, privacy, tenant isolation, security, and cost. Explicitly call out token-aware admission, KV-cache-aware continuous batching, and the retry boundary after partial streaming.

Interviewer may ask next
How would you prevent a few very long prompts from hurting latency for short interactive requests?

I would address this at admission and scheduling. Admission estimates work using prompt tokens, output-token budget, and available KV-cache capacity instead of counting each request equally. Requests enter bounded tenant- and priority-aware queues so one tenant cannot consume the entire system. In the worker, prefill and decode are scheduled separately, and large prefills can be chunked when supported so short decode work gets opportunities to run between chunks. Continuous batching lets completed work leave and new work join. I would use bounded priority or deficit-style fairness rather than strict priority that can starve lower classes. This does not guarantee that every short request is fast, but it prevents long prompts from freely monopolizing queue time, memory, and scheduler capacity.

What changes if a worker crashes after the client has already received part of a streamed answer?

That is a different retry boundary from a crash before any output is visible. Before the first token, the router can retry on another compatible warm worker when the remaining deadline and idempotency policy permit it. After partial output, restarting from the beginning can duplicate visible text or produce a different continuation, so the retry should not be hidden from the client. The simplest contract is to terminate the stream with an error and require an explicit client restart. If resumable streaming is required, the system must deliberately persist enough generation state and the acknowledged stream position for the runtime's resume semantics, then restore that state on a compatible worker. Request IDs can prevent duplicate charging or bookkeeping, but they do not guarantee identical regenerated tokens. Checkpointing adds I/O, storage, and operational complexity, so it should be enabled only when the reliability requirement justifies that cost.

47. Design a scalable and safe tool-using agent platform.Machine Learning System DesignHard

Question Details

Users submit goals and an untrusted model proposes steps that call approved read-only or high-impact tools. Define durable run and step state, queues, leases, idempotency after ambiguous tool success, capability-based authorization, schema validation, sandboxing, secret isolation, prompt-injection defense, human approval, loop and budget termination, and auditable redacted traces. Define task and safety evaluation labels, model and policy registry, serving scale, monitoring, feedback and retraining, drift, incident response, privacy, reliability, and cost allocation.

Short Interview Answer (30-60 seconds)

I would treat the model as an untrusted planner and put every action behind a trusted control plane. Runs and steps are durable, workers use queues and leases, tool calls are capability-authorized and schema-validated, risky actions can require approval, execution is isolated, ambiguous outcomes are reconciled before retry, and all decisions are redacted and auditable.

Detailed Explanation

The core design is to let the model propose actions without giving it direct authority to execute them. A user submits a goal, ingress validates it, and durable orchestration records the run and every step. The untrusted planner proposes a tool and arguments. A trusted safety layer checks capability scope, policy, schema, prompt-injection risk, and approval requirements before isolated execution. Queues, leases, idempotency, reconciliation, budgets, and loop limits make execution recoverable. Redacted traces support audits, while registries, monitoring, evaluation labels, feedback, drift detection, rollback, privacy controls, incident response, and cost attribution support safe operation over time.

Useful Questions to Ask the Interviewer
  1. Which approved tools are read-only, and which can create high-impact external side effects?
  2. What retry semantics do downstream tools provide, such as idempotency keys, operation-status lookup, or reconciliation APIs?
  3. What privacy and retention rules apply to user goals, tool arguments, results, traces, and stored artifacts?
  4. Which serving constraints matter most: end-to-end latency, concurrent runs, availability, queue depth, or cost per run?
  5. How should task quality and safety be evaluated before promoting a new model or policy version?
Design a scalable and safe tool-using agent platform. diagram
How to Explain It in an Interview
1. Start with the trust boundary

The model is the untrusted planner shown in the diagram. It can generate the next step, choose a registered tool, and propose arguments, but it has no direct tool access. The trusted platform owns authorization, validation, credential brokering, execution, persistence, approval, and auditing.

This is the most important design decision. Prompt text from the user, the model, or a tool result can influence a proposal, but it cannot grant authority by itself.

2. Ingress and guardrails

A user submits a goal through the UI or API. The ingress layer authenticates the user, applies rate limits and quotas, detects and redacts PII when appropriate, applies content-safety filters, and validates the goal.

Once accepted, the request becomes a durable run. This prevents the system from depending on one worker's memory or one long-lived process.

3. Durable run and step state

Persist run state with fields such as run ID, user ID, goal, status, budget, limits, creation time, update time, metadata, summary, and outcome.

Persist each step separately with step ID, run ID, parent step ID, model and prompt version, tool name and redacted arguments, status, attempts, lease expiry, observations or result reference, timestamps, token usage, latency, and cost where applicable.

The durable state store is the source of truth. If a worker fails, another worker can resume from persisted state rather than asking the model to guess what already happened.

4. Queues, leases, and concurrency

Use separate durable queues for planner work, tool execution, human approval, and retry or backoff. Workers acquire short-lived leases and renew them with heartbeats. If a worker disappears, its lease expires and another worker can take over.

Limit the number of in-flight steps per run. This prevents one looping or unusually active agent from consuming all worker capacity.

A lease only represents temporary ownership of work. It does not prove that an external side effect happened exactly once.

5. Idempotency and ambiguous tool outcomes

Give every logical tool call a stable idempotency key. Persist the execution intent before calling the tool and persist the known outcome afterward.

The difficult case is a timeout after sending a high-impact request. The external system might have completed the operation even though the worker did not receive the response. In that state, mark the outcome as unknown instead of blindly retrying.

If the downstream system supports operation lookup or reconciliation, query it first. If it confirms success, record the result and continue. If it confirms that the operation did not occur, retry according to policy. If the downstream API honors the same idempotency token, replay can also be safe under that contract.

If none of those mechanisms exists, the platform cannot promise exactly-once external side effects. A high-impact ambiguous outcome should stop or require human resolution rather than risk duplication.

6. Capability-based authorization

Every proposed tool call passes through Safety & Authorization before execution. A capability binds authority to the user, run, tool, operation, and permitted resource scope.

The policy engine evaluates allow or deny rules and risk constraints. The model cannot increase its privileges by asking for a broader operation or by repeating instructions from untrusted content.

This gives least privilege: each run receives only the authority needed for the approved action.

7. Schema validation and registries

The tool registry records approved tool definitions, including name, version, schema, resource scope, and whether the tool is read-only or high-impact. The policy registry is versioned, and the model registry stores model versions and evaluation results.

Before execution, validate tool arguments against the registered JSON Schema, including types, ranges, and enumerated values where defined.

Schema validation answers, 'Is this request structurally valid?' Authorization answers, 'Is this caller allowed to do it?' Both checks are required.

8. Prompt-injection defense

Treat user content and tool-returned content as untrusted data. Keep trusted instructions separate from that data, and validate every proposed tool call after the model has processed an observation.

A malicious document, web page, or tool result must not be able to redefine platform policy, expose credentials, or grant itself a capability.

Filters and jailbreak detection can help, but the stronger protection is architectural: the untrusted model proposes actions and the trusted platform independently decides whether those actions are allowed.

9. Secret isolation

Keep secrets in the encrypted Secret Vault. Never place credentials in model context or ordinary tool arguments.

When an approved tool needs authentication, the trusted execution path retrieves a scoped, preferably short-lived credential and brokers it only to the trusted tool adapter at execution time. The model never sees that credential, and it is not returned in tool-visible results or audit traces.

This limits the blast radius if the model is manipulated by prompt injection.

10. Isolated tool execution

Approved tools execute through the isolated Tool Execution path. The sandbox can use a container or microVM with resource limits, execution time limits, a filesystem allowlist, and no network access by default.

For a tool that legitimately needs network access, allow only the required destination or service instead of unrestricted egress.

The trusted tool adapter invokes the approved API, applies appropriate timeout and retry behavior, and returns a structured success or failure result.

11. Result handling

Store large or durable outputs in the encrypted object store and return a reference when appropriate. Before the result is fed back to the planner, redact or minimize sensitive fields and enforce size and content-safety limits.

The planner receives the observation needed for the next step, not unrestricted internal state, credentials, or raw confidential data that it does not need.

12. Human-in-the-loop approval

High-impact actions can be routed to the approval queue before execution. A reviewer sees the proposed tool, relevant arguments, resource scope, and context. The reviewer can approve, modify, or deny the request.

Every approval decision becomes part of the audit trail.

Approval should be risk-based. Sending every read-only action to a human would add unnecessary latency and cost, while automatically executing every high-impact action would weaken safety.

13. Termination controls

Every run has explicit limits: maximum steps, maximum tool calls, time budget, and cost budget. Detect repeated states or repeated actions as possible loops and detect stalled runs that stop making progress.

When a limit is reached, stop gracefully and store the termination reason and summary. The planner must never be allowed to continue indefinitely just because it can produce another step.

14. Happy-path request flow

The main flow matches the diagram:

  1. Ingress validates the user and goal.
  2. Durable orchestration creates the run and queues the planner step.
  3. The untrusted model proposes a tool call.
  4. Safety & Authorization checks capability, policy, and schema.
  5. If required, Human-in-the-Loop approval is obtained.
  6. Tool Execution runs the approved call inside the isolated runtime.
  7. Result Handling stores and redacts the result.
  8. The observation returns to orchestration and the planner receives the next step.
  9. The loop ends on success, failure, policy denial, a safety limit, or another termination condition.

Human approval is conditional, not a mandatory stage for every tool call.

15. Observability and audit

Maintain an append-only event log at run and step grain. Record the model's proposal, policy decision, tool-call metadata, result reference, decision outcome, latency, token use, cost, and relevant I/O metadata while redacting sensitive fields.

Tamper-evident hash chaining can make unauthorized log modifications detectable.

The audit trail should answer four questions: what did the model propose, what did policy allow or deny, what actually executed, and what result was observed?

Monitor latency, success, deny rate, tool errors, cost, queue depth, retries, and approval behavior. Alerts and dashboards support operators during normal operation and incidents.

16. Core encrypted stores and registries

The diagram uses encrypted core stores for run and step state, immutable event logs, artifacts, policy and tool registry data, and secrets.

The model registry tracks model versions and evaluation results. The tool registry tracks schemas and permitted scopes. The policy registry versions authorization and safety rules.

A run should be attributable to the exact model and policy versions that governed it so later audits and evaluations are reproducible.

17. Serving scale and platform operations

Keep planner and execution workers horizontally scalable around durable queues and state stores. Queue depth is an important capacity signal because it shows when incoming work is exceeding processing capacity.

Autoscaling can add workers, while per-run concurrency limits, quotas, and backpressure prevent overload. Multi-region serving can be used when the reliability requirement justifies it.

Track SLOs and error budgets. Maintain backups and disaster-recovery procedures for durable state. Attribute model, execution, storage, and tool costs by user, run, and tool as shown in Platform Operations.

18. Task and safety evaluation labels

Collect task labels such as task success or correctness and tool-choice quality. Collect safety labels such as policy violations and human-approval outcomes.

Also collect useful feedback such as user feedback, approval edits, automated evaluation results, regression-test failures, and red-team findings.

Feedback must be joined back to the exact run, step, model version, policy version, and tool decision that produced it. Mixing feedback across the wrong execution grain can teach the system from the wrong outcome.

19. Feedback, retraining, drift, and rollback

Build evaluation datasets from validated task and safety labels. Run automated evaluations and regression tests before promoting a new model or policy version.

If retraining or fine-tuning is justified, train a candidate and evaluate it offline before canarying it in production. Use canary rollout for a limited slice first and keep the previous approved model or policy available for rollback.

Monitor data-quality drift separately from task quality and safety quality. Drift means the observed distribution changed; it does not by itself prove that the model became worse. Retrain only when the evidence supports that response. Sometimes the correct fix is a tool, policy, or data-quality change instead of model retraining.

20. Privacy and reliability

Minimize stored personal data, redact traces, encrypt durable stores, restrict access, and apply retention and deletion rules to APIs, logs, and artifacts. Keep secrets out of prompts and traces.

Reliability mechanisms include durable queues and state, leases, retries with safe semantics, backups, SLOs, error budgets, capacity management, and graceful termination.

A failure in one worker should not erase the run, while a failure in a downstream tool should be contained rather than causing uncontrolled retries.

21. Incident response

Use alerts to detect unsafe or failing behavior. Automated playbooks can handle known operational cases. Kill switches and circuit breakers can immediately disable a broken model, policy path, or tool integration.

Then contain the incident, investigate using the redacted audit trail, remediate the affected component, and complete a postmortem. The resulting model, policy, tool, or operational change goes back through the normal evaluation and promotion process.

22. Main tradeoff

The main tradeoff is safety and recoverability versus latency, complexity, and cost. Durable state, policy checks, schema validation, sandbox isolation, reconciliation, redaction, and human approval all consume resources and can slow a run.

But weakening those controls can allow unauthorized actions, duplicate high-impact side effects, credential exposure, unrecoverable state, or unauditable behavior. The best design therefore keeps strong controls around authority and side effects while allowing lower-risk read-only actions to move through the same trusted platform with less friction.

Technical Approach
  1. Define the trust boundary: the model proposes actions; the trusted platform authorizes and executes them.
  2. Authenticate and validate the user's goal, then create a durable run.
  3. Persist every run and step with status, attempts, lease state, result reference, latency, cost, and version metadata.
  4. Schedule planner, tool execution, approval, and retry work through durable queues with short-lived worker leases.
  5. Give each logical tool call a stable idempotency key, persist intent before execution, and reconcile unknown outcomes before retrying.
  6. Check capability scope and policy for every proposed tool call.
  7. Validate arguments against the registered tool schema.
  8. Treat user content and tool results as untrusted data and revalidate every proposed action.
  9. Broker scoped credentials only through the trusted tool adapter and execute tools in an isolated sandbox.
  10. Require risk-based human approval for high-impact actions.
  11. Filter, redact, store, and return the structured tool result to the planner.
  12. Stop on success, failure, policy denial, loop detection, stalled progress, step limits, time limits, tool-call limits, or cost limits.
  13. Record append-only audit events and monitor service health, safety signals, task quality, drift, reliability, and cost.
  14. Evaluate model and policy versions with task and safety labels, use canary promotion, and retain rollback capability.
  15. Join validated outcomes and feedback to the exact run and step before using them for evaluation or retraining.
Practical Complexity & Trade-offs

The biggest costs are model calls, tool execution, sandbox resources, storage, audit logs, approval operations, and retries. Durable state and queues add infrastructure complexity, but they make recovery safer than storing run state only in memory. Sandboxing, policy checks, schema validation, reconciliation, and human approval add end-to-end latency, especially for high-impact tools. Capability-based authorization and short-lived credentials reduce the blast radius but require careful registry and policy management. Autoscaling improves throughput, but it cannot make a slow downstream tool faster, so queue limits and backpressure are still necessary. Longer trace retention helps investigations but increases storage cost and privacy exposure, so traces should be redacted and retained only as long as needed.

Where it is used

This design is useful when an AI agent can search internal information, retrieve documents, call business APIs, update records, send messages, or perform other actions where some approved tools are read-only and others can create meaningful side effects. It is especially useful when runs may outlive one request, workers can fail, sensitive information or credentials are involved, risky actions need approval, and operators must explain exactly why an action was allowed, denied, retried, reconciled, or stopped.

Why Interviewers Ask This

This question tests whether I can design an agent platform where the model is useful but never trusted with direct authority. I need to reason about durable orchestration, queues and leases, ambiguous tool outcomes, capability-based authorization, schema validation, sandboxing, secret isolation, prompt-injection defense, human approval, termination controls, auditability, serving scale, evaluation, feedback, drift, reliability, privacy, incident response, and cost. The central judgment is separating an untrusted planner from a trusted control plane that validates and authorizes every action.

Common interview mistakes

Common mistakes are giving the model direct credentials or direct tool access; treating schema validation as authorization; retrying a timed-out high-impact operation without checking whether it already succeeded; claiming exactly-once external effects when the downstream system cannot provide the required semantics; storing the whole run only in memory; treating a worker lease as proof that a side effect happened once; allowing tool-returned text to redefine trusted instructions or policy; sending every action to human review instead of using risk-based approval; logging secrets or raw sensitive payloads; using drift alone as a retraining trigger; mixing service health, task quality, safety quality, and data quality into one metric; failing to version models and policies; and omitting loop limits, cost budgets, kill switches, incident response, canary rollout, or rollback.

Interview tip

Anchor the answer on one trust boundary: the model proposes, while the platform decides and executes. Then walk through one tool call from durable state to authorization, approval, isolated execution, ambiguous-outcome handling, result storage, and audit. Finish with evaluation, serving reliability, privacy, incident response, and cost tradeoffs.

Interviewer may ask next
What would you do if a high-impact tool times out after the request is sent and you cannot tell whether the action succeeded?

I would mark the step as having an unknown outcome instead of immediately retrying it. The platform keeps the same stable logical tool-call idempotency key and the persisted execution intent. If the downstream system provides operation-status lookup or reconciliation, I query that first. If it confirms success, I record the result and continue without repeating the action. If it confirms that the operation did not occur, I can retry according to policy. If the downstream API honors the same idempotency token, replay may also be safe under that contract. If none of those mechanisms exists, I cannot guarantee exactly-once external side effects, so a high-impact ambiguous case should stop or require human resolution rather than risk a duplicate.

How would the design change if human approval became too slow for the required serving volume?

I would keep approval for genuinely high-impact cases and reduce unnecessary reviews with risk-based routing. Read-only or clearly low-risk actions can proceed automatically after capability, policy, schema, and sandbox checks pass. Higher-risk actions can be routed using tool type, operation, resource scope, and policy. I would autoscale approval processing where possible and monitor approval rate and latency. I would not remove the trusted authorization layer or allow the model to approve its own actions. If the latency target is still impossible, the product must narrow the actions allowed without approval or explicitly accept a different safety-versus-latency tradeoff.

48. What is an A/B test, and when is it the right way to measure a product change?ExperimentationEasy

Question Details

Define an A/B test as a randomized controlled experiment comparing a control with a treatment. Explain the unit of randomization, primary and guardrail metrics, exposure, sample size, duration, and decision rule, then identify situations where interference, low traffic, ethical constraints, or an irreversible change make another method more appropriate.

Short Interview Answer (30-60 seconds)

An A/B test randomly assigns eligible units to a control or treatment and compares predefined outcomes. It is best when assignment and exposure are clean, traffic is sufficient, groups do not meaningfully interfere, and the change is ethical and reversible. Otherwise, another causal design may be better.

Detailed Explanation

An A/B test is a randomized controlled experiment used to learn whether a product change causes a different outcome. Eligible users or other experimental units are assigned randomly to control A or treatment B. Control receives the current experience, while treatment receives the new experience. After valid exposure, both groups are measured with the same predefined primary metric and guardrail metrics. Before launch, define the randomization unit, meaningful effect, sample size, significance level, power, duration, and decision rule. A/B testing is strongest when assignment is stable, exposure is consistent, and interference between groups is limited.

Useful Questions to Ask the Interviewer
  1. What product decision should this experiment support: roll out, iterate, hold, or stop?
  2. What is the unit of randomization: user, device, account, or cluster?
  3. What is the primary metric, and what guardrail metrics must not get worse?
  4. How is exposure defined and logged for each assigned variant?
  5. What minimum detectable effect is practically meaningful?
  6. What significance level and statistical power should we plan for?
  7. How long must the experiment run to reach the needed sample and cover important weekly or business cycles?
  8. Could users affect one another, making individual randomization inappropriate?
  9. Is exposing users to the treatment ethical, safe, and reversible?
What is an A/B test, and when is it the right way to measure a product change? diagram
How to Explain It in an Interview

Start with the decision. The goal is to determine whether a product change should be rolled out, iterated, held, or stopped. The hypothesis is that the treatment changes the predefined primary outcome relative to the control.

Define the eligible population before assignment. Then choose the experimental unit, also called the unit of randomization. This is the unit assigned to one variant, such as a user, device, account, or cluster. Assignment should remain stable for the experiment. If a user is assigned to treatment, that user should not switch back and forth between control and treatment. The analysis must respect the randomization design.

Randomly assign eligible units to control A and treatment B. Control represents the current experience. Treatment represents the new experience. Randomization reduces selection bias and makes the groups comparable on average. Before interpreting an effect, verify that the assignment system worked as planned. In particular, check for sample-ratio mismatch, which means the observed control-treatment allocation is unexpectedly different from the planned allocation. A mismatch can indicate randomization, eligibility, or logging problems.

Define exposure clearly. Exposure means the assigned unit actually had an opportunity to experience its assigned variant. Log exposure consistently across groups and measure outcomes in the intended analysis window. Contamination, inconsistent exposure logging, or switching between variants can weaken the causal comparison.

Choose one primary metric before the experiment. The primary metric answers the main product question. In the approved diagram, sign-up rate is the example primary metric. Define its numerator, denominator, observation grain, measurement window, and direction of improvement before launch. Also choose guardrail metrics that protect users or the business. The diagram uses revenue and errors as guardrail examples and also shows measures such as churn, latency, customer-support contacts, and user complaints.

Plan the sample size before starting. First choose the minimum detectable effect, meaning the smallest effect that would be important enough to detect. Then choose a significance level, such as alpha = 0.05, and target power, such as 80%. Smaller effects generally require more observations. Low traffic can therefore make an ordinary fixed-horizon A/B test take too long or leave it underpowered.

Choose the experiment duration using both the sample-size requirement and product behavior. The approved diagram uses 14 days as an illustrative example and emphasizes covering weekly patterns and business cycles. Do not repeatedly check an ordinary fixed-horizon p-value and stop as soon as it becomes significant. Follow the predefined stopping rule unless a valid sequential design was planned in advance.

After the experiment, estimate the treatment effect and its uncertainty. For a rate metric, compare the treatment rate with the control rate and report the absolute or relative effect together with an appropriate confidence interval or other uncertainty measure. A p-value can help measure evidence against the null hypothesis, but statistical significance alone is not enough. The effect should also be practically meaningful, and the guardrails should remain acceptable.

For the simple two-sided decision example shown in the diagram, alpha is 0.05. If the result does not cross the predefined significance threshold, fail to reject the null hypothesis. That means there is not strong enough evidence of a difference; it does not prove the variants are identical. If the treatment shows convincing improvement and the guardrails are acceptable, the decision can be to roll out or continue ramping. If a guardrail breaks, investigate or stop even if the primary metric improved.

The approved diagram also shows one illustrative end-to-end experiment. It uses the user as the randomization unit, a 50/50 assignment, sign-up rate as the primary metric, revenue and error rate as guardrails, alpha = 0.05, 80% power, an illustrative planned sample of about 10,000 users per group, and a 14-day run. The example result is control sign-up rate 8.0%, treatment sign-up rate 9.2%, p-value 0.008, with guardrails acceptable. Under the diagram's stated decision rule, that illustrative result supports rollout. Those values are an example of the process, not universal requirements or a sample-size calculation derived from the information shown.

A/B testing is not always the right method. Interference or spillover occurs when one user's treatment can affect another user's outcome, such as in social feeds, marketplaces, or networked products. In that situation, user-level randomization may violate the assumption that groups can be treated as independent. A cluster-randomized experiment or another interference-aware design may be more appropriate.

Low traffic is another limitation. If there are too few eligible units to reach the required sample size in a reasonable time, a conventional A/B test may be too slow or too uncertain. Depending on the decision, alternatives can include a preplanned sequential design, a holdout, a Bayesian approach using defensible prior information, or another causal method. A multi-armed bandit can be useful for adaptive allocation, but it solves a different optimization problem and is not automatically a replacement for a standard causal A/B test.

Ethical or legal constraints can also make random assignment inappropriate. If assigning some users to a potentially harmful or unfair experience would be unacceptable, use a safer method such as offline evaluation, simulation, an observational study, or a carefully controlled rollout when appropriate.

An irreversible change is another warning sign. If the change cannot realistically be rolled back, such as permanent data deletion or another one-way action, a normal A/B test may create unacceptable risk. Offline analysis, backtesting, simulation, a small-scale pilot, or a phased rollout may be safer.

The main idea is simple: A/B tests provide strong causal evidence when randomization is clean, treatment assignment is stable, exposure is consistent, the experiment is adequately powered, and the design assumptions hold. When interference, low traffic, ethical constraints, or irreversibility break those assumptions, choose a method that fits the constraint instead of forcing an A/B test.

Technical Approach
  1. Define the product decision and the causal question.
  2. Define the eligible population and the unit of randomization.
  3. Specify control A and treatment B, then keep assignment stable.
  4. Define the exposure event and the analysis window.
  5. Predefine one primary metric, including its numerator, denominator, grain, window, and desired direction.
  6. Predefine guardrail metrics that must remain acceptable.
  7. Choose the minimum detectable effect, significance level, statistical power, allocation ratio, and required sample size.
  8. Choose a duration long enough to reach the sample target and cover important weekly or business cycles.
  9. Run the experiment without changing the primary decision rule or repeatedly peeking with an ordinary fixed-horizon test.
  10. Validate randomization and exposure data, including checks for sample-ratio mismatch, missingness, contamination, and logging problems.
  11. Estimate the treatment effect and uncertainty, then evaluate statistical evidence, practical value, and guardrails.
  12. Roll out, ramp, iterate, hold, stop, or rerun according to the predefined decision rule.
  13. Use another method when interference, low traffic, ethical constraints, or an irreversible change makes a standard A/B test inappropriate.
Practical Insights

The biggest costs are usually users, time, and product risk rather than computation. Detecting a smaller meaningful effect requires a larger sample, so low traffic makes experiments slower. Longer runs can capture weekly patterns better, but they delay the decision and may expose more users to a weak treatment. More guardrails provide protection but can create multiple-comparison issues if many tests drive the decision. Cluster randomization can handle some interference, but observations inside a cluster are correlated, so the effective sample size is smaller. Stable assignment, good exposure logging, and data-quality checks add operational work, but they are necessary for trustworthy results.

Why Interviewers Ask This

Interviewers want to know whether you understand why random assignment supports causal conclusions and whether you can design a trustworthy product experiment. They are testing your judgment about the randomization unit, exposure, primary and guardrail metrics, sample size, duration, statistical evidence, practical value, and the conditions under which an A/B test should not be used.

Common interview mistakes

Common mistakes are treating an A/B test as only a comparison of two averages, changing the primary metric after seeing results, using unstable assignment, measuring outcomes without a clear exposure rule, leaving the metric denominator or analysis window undefined, repeatedly peeking at an ordinary fixed-horizon p-value and stopping early, interpreting p >= 0.05 as proof of no effect, reporting significance without effect size or uncertainty, ignoring guardrail damage, skipping sample-ratio-mismatch checks, treating correlated observations as independent, and using individual randomization when users interfere with one another. Another mistake is forcing an A/B test when traffic, ethics, or irreversibility makes the design impractical or unsafe.

Interview tip

Explain the answer as one flow: randomize eligible units, keep assignment stable, expose control and treatment consistently, measure one predefined primary metric plus guardrails, plan sample size and duration, validate the experiment, estimate the effect with uncertainty, and apply the predefined decision rule. Then state clearly when those assumptions fail and another method is better.

Interviewer may ask next
What would you change if users in the treatment can affect users in the control?

That is interference or spillover, so ordinary user-level randomization may no longer give independent groups. I would first identify how users influence one another. If those interactions follow natural groups, I could randomize at a cluster level and analyze the experiment using that clustered design. Because outcomes within the same cluster are correlated, the power calculation must account for clustering and the effective sample size will usually be smaller. If a clean cluster-randomized design is not practical, I would use another causal method rather than claim that a standard user-level A/B test identifies the treatment effect.

What would you do if traffic is too low to reach the planned sample size in a reasonable time?

I would first confirm the minimum detectable effect and whether that effect is genuinely important for the product decision. If the required sample is still impractical, I would not pretend that an underpowered fixed-horizon test gives a reliable answer. Depending on the decision, I could consider a preplanned sequential design, a longer holdout, a Bayesian approach with defensible prior information, or another causal method suited to the available data. If operational risk is the main concern, a phased rollout may also be appropriate. The final conclusion should clearly communicate the larger uncertainty.

49. How would you design an A/B test for a new homepage layout?ExperimentationEasy

Question Details

A product team wants to compare the current homepage with one redesigned layout. Define the eligible population, randomization unit, control and treatment experiences, assignment and exposure logging, one primary metric tied to the product goal, guardrail metrics, pre-experiment checks, minimum detectable effect, runtime, and a pre-specified ship, hold, or reject decision rule. Address repeat visits and users who are assigned but never actually see the homepage.

Short Interview Answer (30-60 seconds)

I would randomize eligible logged-in users 50/50 by user ID, keep that assignment sticky across repeat visits, and compare the current homepage with the redesign. I would log assignment and exposure separately, use 7-day Signed-In Start Rate as the primary metric, monitor guardrails, check SRM, analyze by intent-to-treat, and apply a pre-specified ship, hold, or reject rule.

Detailed Explanation

I would run a user-level randomized A/B test because the same person may visit the homepage many times. Eligible users are logged-in adults on desktop or mobile web, in target countries, consented to analytics, and not already in another homepage experiment. Each user receives one stable 50/50 assignment. Control sees the current homepage, while treatment sees the redesigned homepage. I would log assignment when randomization occurs and log exposure only when the homepage actually renders. The primary outcome is 7-day Signed-In Start Rate, supported by user-experience guardrails and an intent-to-treat analysis.

Useful Questions to Ask the Interviewer
  1. What exact product action should the homepage increase, and is Signed-In Start Rate the agreed primary success metric?
  2. Are the eligibility rules fixed to logged-in users who are 18+, on desktop or mobile web, in target countries, consented to analytics, and not in another homepage test?
  3. What pre-defined amount of degradation is acceptable for each guardrail before the redesign should be held or rejected?
  4. Is this intended to be a fixed-horizon experiment with one final analysis after the required sample and outcome windows mature?
How would you design an A/B test for a new homepage layout? diagram
How to Explain It in an Interview

The business decision is whether to roll out the redesigned homepage, hold the decision because the evidence is inconclusive, or reject the redesign. The hypothesis is that the redesigned layout increases valuable starts from the homepage without materially harming core user experience.

The experimental unit is the user. This is important because one user can visit several times. I would assign each eligible user once with a deterministic hash of the user ID. Bucket values 0 through 49 go to Control A and 50 through 99 go to Treatment B, giving a 50/50 allocation. The assignment remains stable for the entire experiment, so a repeat visitor does not switch between variants.

Control A is the current homepage. Treatment B is the redesigned homepage. Stable user-level assignment prevents treatment switching across repeat sessions and keeps the analysis at the same grain as randomization.

Assignment and exposure are different events and should be logged separately. The assignment log records the user ID, experiment ID, assigned variant, and assignment time. The exposure event is written only when the assigned homepage actually renders. The exposure log contains the user ID, experiment ID, variant, timestamp, page, and device. Downstream outcome events also contain the user ID and timestamp so they can be joined to the experiment data.

The primary metric is Signed-In Start Rate, or SISR. The numerator is the number of assigned users who start a defined core action, such as adding to cart or starting checkout. The primary denominator is all assigned users because the main analysis is intent-to-treat. Higher is better. For users who actually render the homepage, the diagram uses a 7-day post-exposure outcome window. Assigned users who never render the homepage remain in the intent-to-treat population; they have no homepage exposure and no homepage-attributed start in that window rather than being removed from the experiment.

The guardrails are bounce rate, time to interactive at p75, page-load success rate, unsubscribe rate, and error rate. Bounce rate, time to interactive, unsubscribe rate, and error rate are better when lower. Page-load success rate is better when higher. Their grain, numerator or summary statistic, denominator, time window, and acceptable degradation threshold should be written down before launch. If several guardrails jointly determine the decision, I would control the multiple-testing error rate, for example with Holm-Bonferroni as shown in the diagram.

Before launch, I would finalize and validate the metric definitions, verify exposure logging in staging, test the randomization pipeline, configure sample-ratio-mismatch monitoring, review guardrail thresholds with Product and Engineering, agree on the minimum detectable effect, confirm the 50/50 allocation, prepare a launch and rollback plan, and QA the experiment on major devices and browsers.

For statistical planning, the diagram uses a control SISR baseline of 10.0%, an absolute minimum detectable effect of 1.0 percentage point, a two-sided alpha of 0.05, 80% power, and equal 50/50 allocation. Its planning value is approximately 15,700 users per variant. The MDE means the experiment is sized to have the planned power for a change of about 10% to 11%, not that a 1-point improvement is guaranteed to occur.

Calendar runtime depends on traffic and the required sample. A simple planning calculation is required users per variant divided by eligible users per day per variant. With the diagram's example of about 10,000 eligible users per day per variant, the raw sample could accumulate in roughly 1.6 days. However, I would enroll users for at least 7 full days to cover weekday effects. Because the outcome window lasts 7 days, I would also wait until the last included cohort has had its complete outcome window before the final analysis.

Before interpreting the treatment effect, I would check sample-ratio mismatch against the expected 50/50 assignment. A large unexplained mismatch can indicate a randomization, eligibility, logging, or data-loss problem. The diagram's 49% to 51% range can be used as a quick operational sanity check, but the actual SRM decision should use a formal statistical test against the expected allocation rather than treating a fixed percentage band as universally valid.

The primary estimand is the intent-to-treat difference in SISR between users assigned to treatment and users assigned to control. Because SISR is a user-level proportion, a two-proportion z-test is appropriate when its large-sample assumptions hold. I would report the absolute lift, the 95% confidence interval, and the p-value. The data should contain one analysis contribution per randomized user so repeated sessions do not create artificial independence.

I would also pre-specify an exposed-only comparison as a secondary sensitivity analysis. I would not make it the primary causal estimate because actual exposure occurs after assignment. Conditioning on exposure can select different kinds of users in the two groups and weaken the protection created by randomization.

I would not repeatedly peek at a fixed-horizon p-value and stop when it first becomes significant. The primary metric, exclusions, MDE, sample requirement, minimum runtime, analysis method, guardrail thresholds, and decision rule should all be specified before launch. The final analysis happens after the required data and outcome windows are complete.

The decision rule is also pre-specified. Ship the redesign when the primary lift is positive, the two-sided p-value is below 0.05, the lower bound of the 95% confidence interval is above zero, and no guardrail degrades beyond its allowed threshold. Reject when the primary lift is zero or negative, or when a guardrail has a statistically supported material degradation beyond its threshold. Otherwise, hold when the primary effect is positive but inconclusive, such as when its confidence interval crosses zero, or when a guardrail result needs more evidence while remaining within the agreed tolerance. This ordering removes overlap between hold and reject. The final judgment should use effect size, uncertainty, practical value, and user impact rather than statistical significance alone.

Technical Approach
  1. Define the business decision and hypothesis for the redesigned homepage.
  2. Define the eligible population: logged-in users, age 18+, desktop or mobile web, target countries, analytics consent, and no concurrent homepage experiment.
  3. Randomize at the user level with a deterministic user-ID hash and a 50/50 control-treatment split.
  4. Keep assignment stable across every repeat visit.
  5. Record assignment when randomization occurs and exposure only when the assigned homepage actually renders.
  6. Use Signed-In Start Rate as the primary metric, with assigned users as the intent-to-treat denominator and a pre-specified 7-day outcome window.
  7. Monitor bounce rate, time to interactive p75, page-load success rate, unsubscribe rate, and error rate as guardrails with pre-defined thresholds.
  8. Validate metrics, assignment, exposure logging, SRM monitoring, MDE, allocation, rollback procedures, and major devices and browsers before launch.
  9. Plan around the diagram's baseline SISR of 10.0%, absolute MDE of 1.0 percentage point, two-sided alpha of 0.05, 80% power, 50/50 allocation, and approximately 15,700 users per variant.
  10. Accumulate the required sample, enroll for at least 7 full days, and allow the final 7-day outcome window to mature before the final read.
  11. Diagnose sample-ratio mismatch before interpreting the treatment effect.
  12. Perform the user-level intent-to-treat analysis with a two-proportion z-test when its assumptions hold, and report lift, 95% confidence interval, and p-value.
  13. Use exposed-only analysis only as a pre-specified sensitivity check.
  14. Apply the pre-specified ship, hold, or reject rule using the primary metric, guardrails, uncertainty, and practical value.
Practical Insights

The biggest costs are users and calendar time. Detecting a smaller effect needs a larger sample. Asking for higher power also needs more users. User-level randomization is slightly more involved than counting page views, but it correctly handles repeat visits. Stable assignment requires a reliable user identifier. Logging assignment and exposure separately adds instrumentation work, but it is necessary to distinguish users who were randomized from users who actually saw the homepage. Several guardrails also make analysis more complex because multiple comparisons may need correction. Operational work includes staging validation, SRM monitoring, device and browser QA, outcome-window maturation, and a rollback plan.

Why Interviewers Ask This

This question tests whether a candidate can turn a product change into a valid causal experiment. The interviewer is looking for correct population and randomization choices, stable treatment assignment across repeat visits, a clear distinction between assignment and exposure, well-defined success and guardrail metrics, statistical power and runtime planning, detection of failures such as sample-ratio mismatch, an appropriate intent-to-treat analysis, uncertainty reporting, and a decision rule that is defined before the results are seen.

Common interview mistakes

Common mistakes include randomizing page views instead of users, allowing repeat visitors to switch variants, failing to log assignment separately from exposure, dropping assigned users who never render the homepage from the primary analysis, making an exposed-only analysis the primary causal estimate, leaving the primary metric denominator or time window unclear, choosing the metric after seeing results, ignoring guardrails, skipping the MDE and power calculation, forgetting to let the outcome window mature, repeatedly peeking at a fixed-horizon p-value, interpreting effects before diagnosing sample-ratio mismatch, using a fixed allocation-percentage band instead of a formal SRM test, testing many decision-driving guardrails without controlling multiple comparisons, and shipping because of statistical significance without considering effect size, confidence intervals, practical value, and user harm.

Interview tip

Explain the design in the same order the experiment runs: eligibility, sticky user-level randomization, control and treatment, assignment versus exposure logging, primary metric and guardrails, power and runtime, SRM and analysis, then the pre-specified decision rule. Call out repeat visits and assigned-but-unexposed users explicitly because they are the key edge cases in this question.

Interviewer may ask next
What would you do with users who are assigned to a variant but never actually see the homepage?

I would keep them in the primary intent-to-treat population because assignment is the randomized event. Their assignment remains recorded even when no homepage exposure is logged. Under the diagram's metric definition, they have no homepage-attributed exposure and no homepage-attributed start for that exposure window rather than being dropped from the denominator. I would report an exposed-only analysis only as a pre-specified secondary sensitivity check, because conditioning on exposure can introduce selection bias.

What would you do if the treatment improves Signed-In Start Rate but one guardrail becomes worse?

I would apply the guardrail threshold that was defined before launch. If the degradation is materially beyond that threshold and statistically supported, I would reject the rollout even if the primary metric improves. If the guardrail movement remains within the agreed tolerance but adds uncertainty, I would hold rather than automatically ship. I would consider the primary effect size, its confidence interval, the guardrail effect, and practical user impact together.

50. How would you determine the sample size for an A/B test?ExperimentationEasy

Question Details

Assume a two-arm randomized test with a known baseline conversion rate and a proposed minimum detectable absolute lift. Specify the significance level, desired power, treatment allocation, one- or two-sided alternative, variance assumptions, and any expected attrition or noncompliance. Explain how each input changes required sample size and how the calculation connects to the planned decision rule.

Short Interview Answer (30-60 seconds)

I would pre-specify the baseline conversion rate, absolute MDE, alpha, power, allocation ratio, test direction, and variance assumptions. Then I would calculate the two-proportion sample size, inflate it for expected attrition, account for noncompliance through the expected ITT effect, and analyze the experiment with the same pre-specified test and decision rule.

Detailed Explanation

For a two-arm A/B test with a binary conversion outcome, I would determine sample size before the experiment starts. I need the baseline conversion rate, the smallest absolute lift worth detecting, the significance level, desired power, treatment-to-control allocation, one- or two-sided alternative, and variance assumptions. These inputs determine how much statistical information the test needs. I would then inflate the calculated group sizes for expected missing outcomes. If noncompliance is expected to dilute the intention-to-treat effect, I would power for that smaller expected effect rather than treating noncompliers as missing observations.

Useful Questions to Ask the Interviewer
  1. What is the known baseline conversion rate for the control group?
  2. What minimum detectable absolute lift, or MDE, is practically meaningful?
  3. What significance level and desired power should we use?
  4. Should the alternative be one-sided or two-sided?
  5. What treatment-to-control allocation ratio is planned?
  6. Can we assume independent Bernoulli outcomes, or is there clustering or interference?
  7. What level of attrition or missing primary outcomes is expected?
  8. Is noncompliance expected to dilute the intention-to-treat effect?
How would you determine the sample size for an A/B test? diagram
How to Explain It in an Interview

Start with the effect I want to detect. Let p1 be the baseline control conversion rate and let Delta be the minimum detectable absolute lift. Under the planning alternative, the treatment conversion rate is p2 = p1 + Delta.

Next define the allocation ratio r = n2 / n1, where n1 is the control-group size and n2 is the treatment-group size. A 1:1 allocation means r = 1. With equal per-user cost and similar variances, a near-balanced allocation is usually efficient. Changing r changes n1, n2, and the total sample size.

For the normal approximation shown in the diagram, define the allocation-weighted planning proportion as p_bar = (p1 + rp2) / (1 + r). Then calculate the control-group size using:

n1 = [ { z_(1-alpha) * sqrt((1+r)p_bar(1-p_bar)) + z_(1-beta) * sqrt(rp1(1-p1) + p2*(1-p2)) } / { sqrt(r)Delta } ]^2

Then set n2 = rn1 and round required group sizes upward.

For a two-sided test, alpha* = alpha/2. For a one-sided test, alpha* = alpha. The value z_(1-beta) corresponds to the desired power, 1-beta.

Each input affects sample size in a predictable way. Lower alpha is stricter, so required sample size increases. Higher desired power also increases sample size. A smaller MDE is harder to detect, so it requires more observations. For a fixed absolute lift, Bernoulli variance is generally larger when the relevant conversion rates are nearer 0.5, so more information may be required. The allocation ratio also changes the number assigned to each arm and the total required sample.

The variance assumptions matter. The displayed calculation assumes independent binary outcomes and sufficiently large expected success and failure counts for the normal approximation. If observations are clustered or interfere with one another, they contain less independent information, so I would use a power calculation that reflects that dependence and matches the planned analysis.

After calculating the statistical sample size, I would account for expected attrition or unavailable primary outcomes. If L is the expected attrition rate, use the inflation factor F = 1 / (1-L). Then use n1_adj = ceil(n1F) and n2_adj = ceil(n2F).

Noncompliance is different from attrition. Under intention-to-treat, or ITT, users remain in the groups to which they were randomized. I would not remove noncompliers or count them as missing just because they did not follow their assigned treatment. If expected noncompliance dilutes the ITT effect, I would power the test using the smaller expected ITT MDE. A smaller expected effect means a larger required sample size.

Using the diagram's example, let p1 = 5%, Delta = 1 percentage point, alpha = 0.05, power = 80%, a two-sided alternative, and 1:1 allocation. The displayed normal-approximation formula gives n1 approximately 8,158 and n2 approximately 8,158 before attrition. With 10% expected attrition, dividing by 0.90 and rounding upward gives about 9,065 users per group.

Finally, the sample-size calculation must connect to the planned decision rule. I would analyze the test using a pre-specified two-proportion z-test and the variance rule that matches the planning calculation. For a two-sided test, reject H0 when the p-value is below alpha. For a one-sided test, reject only when the p-value is below alpha in the pre-specified beneficial direction. I would also report the estimated effect size and a confidence interval matched to the pre-specified test and alpha.

The main idea is that sample size is chosen to give the desired probability of detecting the specified meaningful lift while controlling the planned Type I error rate. I would not repeatedly peek at a fixed-horizon p-value and stop when it first becomes significant unless a valid sequential stopping procedure had been designed in advance.

Technical Approach
  1. Define the baseline conversion rate p1 and absolute MDE Delta, so p2 = p1 + Delta.
  2. Choose alpha, desired power 1-beta, one- or two-sided testing, and allocation ratio r = n2/n1.
  3. State the variance assumptions, including whether outcomes can be treated as independent Bernoulli observations and whether the normal approximation is appropriate.
  4. Compute p_bar = (p1 + rp2)/(1+r).
  5. Use the two-proportion normal-approximation formula to calculate n1, then calculate n2 = rn1.
  6. Round each required group size upward.
  7. Inflate each arm for expected attrition using F = 1/(1-L), then round upward again.
  8. If expected noncompliance dilutes the ITT effect, repeat the power calculation using the smaller expected ITT MDE rather than treating noncompliers as attrition.
  9. Run the experiment to the planned sample size and analyze it with the pre-specified test and compatible variance rule.
  10. Apply the planned p-value decision rule and report the effect estimate with uncertainty.
Practical Insights

The numerical calculation is cheap; the important costs are statistical and operational. A lower alpha, higher power, smaller MDE, or less efficient allocation can require more users and therefore more experiment traffic or time. Attrition increases enrollment because some primary outcomes will be unavailable. Noncompliance can dilute the ITT effect and therefore increase the required sample if the powered effect becomes smaller. Clustering or interference reduces effective independent information and requires a different power calculation. The main maintenance risk is allowing the planning assumptions and final analysis method to drift apart.

Why Interviewers Ask This

Interviewers want to see whether I understand that sample size is a design decision made before looking at experiment results. A strong answer connects the baseline conversion rate, minimum detectable absolute lift, significance level, desired power, treatment allocation, test direction, variance assumptions, attrition, and noncompliance to the required number of observations. It should also show that the sample-size calculation and the final hypothesis test use compatible assumptions and a pre-specified decision rule.

Common interview mistakes

Common mistakes include choosing sample size after seeing results, failing to define an MDE, confusing relative lift with absolute lift, using alpha incorrectly for a one-sided versus two-sided test, assuming 1:1 allocation when the actual traffic split differs, ignoring clustering or interference, treating ordinary non-conversion as attrition, treating noncompliers as missing under ITT, forgetting to inflate for expected missing outcomes, failing to round required group sizes upward, and using an analysis test or variance rule that does not match the power calculation. Another mistake is repeatedly checking a fixed-horizon p-value and stopping when it first becomes significant.

Interview tip

Explain the inputs first, then say how each one affects required sample size. Distinguish attrition from noncompliance, state the key independence and variance assumptions, and finish by connecting the power calculation to the exact pre-specified analysis and decision rule.

Interviewer may ask next
What would you change if observations were clustered instead of independent users?

I would not use the independent-user calculation unchanged. Correlation within a cluster means observations provide less independent information. I would define the randomization and analysis unit, estimate or assume the within-cluster correlation and cluster-size structure, and use a cluster-aware power calculation or appropriate design effect. The sample-size method should match the clustered analysis that will be used after the experiment.

What happens if expected noncompliance reduces the observed treatment effect?

Under intention-to-treat analysis, I would keep users in their randomized groups and would not count noncompliers as attrition. If noncompliance is expected to dilute the ITT effect, I would plan for the smaller expected ITT MDE. Because detecting a smaller effect requires more information, the required sample size increases. This keeps the power calculation consistent with the estimand and analysis I actually plan to use.

More questions load as you scroll

Disclaimer: This interview guide is for educational and informational purposes only. It is designed to help readers prepare, but it does not guarantee any interview result, hiring decision, offer, or outcome. Interview questions, hiring criteria, and preferred answers can vary by employer, interviewer, industry, location, and time. The examples and explanations reflect the authors' research and judgment, are provided without warranties of any kind, and should not be treated as the only correct approach. Diagrams are simplified illustrations intended to highlight the main components and their interactions; actual systems and implementations may be more complex. Alternative approaches may be equally valid or better suited to a particular question, context, or interviewer. To the fullest extent permitted by applicable law, the author, contributors, and publisher are not liable for decisions made, actions taken, or losses incurred based on this guide.

Content Accuracy and Verification: To the fullest extent permitted by applicable law, we do not represent or warrant that interview guides, questions, answers, examples, or diagrams are accurate, complete, current, error-free, or suitable for any particular purpose. You are responsible for independently reviewing and verifying the information before relying on it.