Error Handling and Defensive Programming Questions
Making code robust against bad input and failure: exceptions versus error returns, input validation, guard clauses, graceful degradation, and designing for the unhappy path. Covers where to handle versus propagate errors and how to fail safely without hiding bugs. A recurring probe of production maturity.
Walk through a post-incident review for an outage caused by an unhandled exception in a scheduled job that silently aborted processing and caused data drift (or a corrupted feature-ingestion stream that caused systematic label shift in a deployed model, or a dependency upgrade that silently altered a serialization format). Describe how you would reconstruct the timeline, collect logs/traces/metrics, perform root-cause analysis, and produce remediation actions (code changes, alerts, tests, release-process changes) that prevent a similar silent failure.
Explain the standard log levels (DEBUG, INFO, WARNING, ERROR, CRITICAL) and give a concrete example of what belongs at each level in a backend or ML-serving context (for example: DEBUG for per-batch loss, WARNING for a missing optional feature, ERROR for a failed model load). Discuss how log-level choice affects storage volume and alerting strategy in production.
Implement an in-process circuit breaker with states CLOSED, OPEN, and HALF_OPEN: it should open after a configurable number of failures within a sliding window, stay open for a cooldown period, then allow a single (or limited) probe request in the half-open state before deciding whether to close or re-open. Provide the class API and a thread-safe (or otherwise concurrency-safe) implementation, describe the race conditions that arise under concurrent load and how you'd test for them, and name the metrics you would emit to tune the thresholds in production.
Design a consistent error RESPONSE schema for an HTTP API used by both external clients and internal services: correlation id, a machine-readable error code, a human-readable message, optional field-level validation details, and a trace id. Map common failure situations (validation error, auth failure, transient downstream failure, rate limit) to HTTP status codes, distinguishing 4xx from 5xx with concrete edge-case examples of each. Provide a JSON example for a validation-failure payload, and explain how you would version the schema and keep it backward compatible without leaking internal implementation details.
Given code that catches a broad exception and silently swallows it (try: ... except Exception: pass, or except Exception: print('error'), or a health-check that catches everything and always returns 200), explain why this is dangerous in production, and refactor it: catch specific exception types, log with full context and preserve the stack trace, decide when to re-raise versus recover, instrument a metric, and ensure resource cleanup still happens. Describe the unit tests you would add to prove failures are now visible.
Unlock Full Question Bank
Get access to all Error Handling and Defensive Programming interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.