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.
Tell me about a time you found and fixed code that was failing silently (a swallowed exception, an empty catch block, or a missing validation that let a bug reach production repeatedly). Using the STAR structure, describe how you detected the issue, the fix you made, how you convinced others to accept a defensive change that might slow development, and what you did to prevent recurrence.
You're designing structured error logging for a service (a REST API, or an ML inference endpoint). An on-call engineer investigating an incident should be able to go from one error log line to a full picture of what happened and to whom, without needing to reproduce the bug. Design the log entry's field set to make that possible, and explain why each field you chose earns its place. Contrast a plain stdout print with a structured JSON logging framework, and describe how you would avoid logging sensitive PII while preserving diagnostic value.
A nightly batch job (or a multi-stage data-preprocessing DAG) processes millions of records and occasionally crashes mid-run. Design a resume/restart mechanism so that on restart, processing continues from the last consistent checkpoint without duplicating side effects on already-processed records. Discuss checkpoint granularity, idempotent re-runs, transactional updates, state management for a DAG with dependent tasks, and the throughput-versus-checkpoint-frequency trade-off.
Describe a safe graceful-shutdown and connection-draining sequence for a stateless HTTP service running in containers behind a load balancer: stop accepting new requests, drain in-flight requests within a timeout, handle any background jobs, and configure Kubernetes readiness/liveness probes to support this lifecycle. Which OS signals should be trapped, and how do you coordinate shutdown across worker threads or asyncio tasks?
Explain the circuit breaker pattern: what problem it solves (preventing cascading failures by stopping calls to a dependency that is likely down), its states and thresholds, and half-open probing logic. Then describe how you would write integration tests that verify the breaker's behavior under flapping (rapidly alternating success/failure) dependencies without making the tests brittle.
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.