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.
Implement and test defensive integer-overflow handling: write a function that adds (or multiplies) two signed integers and either raises/returns an explicit error on overflow, or (in a language like Go) safely parses a string to an integer and returns an explicit error on overflow rather than wrapping silently. Cover boundary and negative-value test cases, and explain why detecting overflow explicitly is safer than letting it wrap.
A long-running service has been up for weeks and its memory and connection usage keep creeping upward until an operator has to bounce it. Walk through where you'd look for the leak across the service's different resource types (not just memory in the abstract), and for each source you identify, describe one concrete defensive coding or configuration approach that would have prevented it from exhausting the system in the first place.
Architect an approach that combines circuit breakers with bulkhead isolation for a set of microservices. Describe where you would place bulkheads (thread pools, connection pools), how you would configure per-client or per-route limits, the emergency fallback behavior when a bulkhead is exhausted, the metrics you would monitor, and how this design prevents healthy requests from starving during a partial failure.
Describe a test strategy for exercising ERROR PATHS specifically: invalid schemas, out-of-range inputs, I/O failures, corrupted files, schema drift, partial records, and transient failures. Include a property-based test (for example using Hypothesis) that asserts an invariant holds for all valid inputs (a preprocessing function never returns NaN/Infinity, or shape invariants hold), and a test plan for a data-ingestion pipeline covering deduplication, ordering, watermarks, and late-arriving or out-of-order data.
Write a function that validates an incoming request payload for a small set of required fields (for example: a non-empty username, a well-formed email, and an optional integer age within a sane range). On invalid input, raise a clear exception (or error) naming the offending field, and on success return a normalized, sanitized version of the payload (trimmed strings, lower-cased email). Explain why raising a descriptive error is preferable to returning null or a generic failure.
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.