Robust Python Components And API Failure Handling
Asked of: Machine Learning Engineer
Last updated

What's being tested
These questions test defensive Python programming: validating inputs, isolating side effects, handling exceptions, and producing deterministic outputs. They also probe metric correctness—especially precision/recall—and whether you can make an evaluation component robust to flaky APIs, malformed responses, retries, and partial failures.
Patterns & templates
-
Separate pure computation from I/O: fetch API results first, then compute metrics or scores in deterministic, easily unit-tested functions.
-
Validate at boundaries with explicit types, required fields, ranges, and allowed values; reject malformed data instead of silently coercing it.
-
Wrap network calls narrowly with
try/except; distinguish timeout, connection, HTTP-status, parsing, and semantic-validation failures. -
Use bounded retries with exponential backoff and jitter; retry transient
429and5xxresponses, not invalid requests or malformed payloads. -
For top-k evaluation, deduplicate results and normalize identifiers before calculating and .
-
Treat zero denominators explicitly: return
0.0,None, or a documented convention rather than allowingZeroDivisionErroror misleading perfect scores. -
Represent prices as integer cents or
Decimal, never binary floating point; keep validation, calculation, and presentation separate.
Common pitfalls
Pitfall: Counting duplicate API results as separate predictions inflates true positives and can make precision exceed the intended interpretation.
Pitfall: Catching broad Exception and returning an empty result hides outages; preserve an error state or structured failure reason.
Pitfall: Retrying every failure can amplify load and duplicate side effects; use timeouts, bounded attempts, and idempotency where applicable.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
- Pizza Price Function, Then a Shop Pricing Screen and the Pricing Microservice QuestionMicrosoft · Machine Learning Engineer · Technical Screen · medium
- Measure LLM Quality and Detect Hallucinations, Then Implement an Evaluation ComponentMicrosoft · Machine Learning Engineer · Onsite · medium
- Compute precision/recall from a flaky top-k APIMicrosoft · Machine Learning Engineer · Technical Screen · medium
Related concepts
- Reliable API Integration And Distributed DesignSystem Design
- Resilient APIs And External Dependency IntegrationSystem Design
- API Integration And External Service DesignSystem Design
- Resilient API Aggregation And Operational DebuggingSoftware Engineering Fundamentals
- Object-Oriented Design, API Design, And TestabilityCoding & Algorithms
- Production Hardening and TestingSoftware Engineering Fundamentals