Assertions and Behavior Verification Questions
Choosing what to assert and how to verify behavior meaningfully. Covers assertion strategy, verifying observable behavior over implementation detail, avoiding weak or over-specified assertions, and using assertion libraries effectively. Emphasizes assertions that fail for the right reasons and give a clear diagnosis.
A training script pulls features from an external feature store and checks GPU availability before launching. Sketch pytest tests that mock the feature-store client and simulate both GPU-available and GPU-unavailable environments. Use unittest.mock or pytest monkeypatch and show key code snippets for mocking.
Given a small function that computes recall:
def recall(y_true, y_pred):
# y_true, y_pred: lists/arrays of 0/1
tp = sum(1 for t, p in zip(y_true, y_pred) if t == 1 and p == 1)
fn = sum(1 for t, p in zip(y_true, y_pred) if t == 1 and p == 0)
return tp / (tp + fn)
Write unit tests that check correct values, handle division-by-zero, and include messages describing the failing scenario (e.g., "expected recall 0.75 for case X but got Y"). Use pytest.
Given the following Python function, write pytest unit tests that cover normal inputs, zero-vector edge case, and invalid inputs (NaN/Inf). Include clear assertion messages to aid debugging.
import numpy as np
def normalize(v):
v = np.asarray(v, dtype=float)
norm = np.linalg.norm(v)
if norm == 0:
return v
return v / norm
Write tests in Python using pytest and numpy; include parametric tests where appropriate.
Design integration tests for an inference microservice that hosts a transformer model. Requirements: max sequence length 1024, expected p95 latency < 100ms for batch size 1 on a GPU, correctness of outputs for a set of canonical prompts, and graceful error handling for invalid inputs. Describe the test types (unit/integration/perf), tools you'd use, and failure isolation strategies.
Propose a test strategy to detect model drift after deployment. Include offline tests (data-snapshot comparisons, distributional checks) and online tests (shadow traffic, monitoring metrics). Define what metrics you would monitor and how you would set statistical alarms to reduce false positives.
Unlock Full Question Bank
Get access to all 40 Assertions and Behavior Verification interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.