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 a thread-safe token-bucket rate limiter (tryAcquire()/refill()) supporting configurable burst capacity and a sustained replenishment rate. Then describe how you'd apply per-tenant rate limiting, backpressure, and resource isolation (CPU/GPU/QoS) in a multi-tenant online-inference service so one noisy tenant can't degrade others, including the error-handling policy for a rejected request (429 versus queueing versus a degraded response).
In Java, write a method String getNormalizedEmail(User user, String fallback) that safely returns the lowercase email of a possibly-null nested object chain (user -> profile -> contact -> email) without throwing a NullPointerException, returning fallback if any link is null or empty. Implement it using idiomatic Java (Optional or defensive null checks) and justify when you would favor Optional versus explicit defensive checks in a codebase.
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.
In C++, what happens if a destructor throws an exception while the stack is already unwinding because of another exception? Explain the language rule, the resulting call to std::terminate, and best practices (noexcept destructors, avoiding throwing from cleanup code) to avoid this scenario. Give a brief example illustrating the danger and the correct way to handle a cleanup failure.
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.
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.