Code Quality, Error Handling, and Defensive Programming Questions

Writing robust, high-quality code that fails safely. Covers defensive programming, input validation, error handling and fault tolerance, logging for diagnosability, and general engineering-quality standards. Includes anticipating failure modes and making code resilient to bad inputs and unexpected states.

MediumTechnical
25 practiced

Describe the role of assertions and invariants in maintaining code correctness. When should assertions be used versus throwing exceptions? Provide an example where an assertion detects a developer error early and avoids a costly runtime check in production, and describe how this maps to design-by-contract thinking (preconditions, postconditions, invariants).

HardTechnical
26 practiced

You are implementing a high-performance financial aggregation in C++ that sums millions of int64 values. Describe strategies to avoid overflow and preserve precision over large sums: a wider accumulator, periodic reduction, compensated summation (Kahan summation), and a big-integer fallback. Discuss the performance-versus-correctness trade-offs of each.

EasyTechnical
21 practiced

Explain patterns for handling missing or null values in strongly-typed languages like Java and dynamically-typed languages like Python or JavaScript. Include examples of Option/Maybe-style types, exceptions, and sentinel values, and explain when you would use an assertion compared to throwing a recoverable error.

HardTechnical
31 practiced

You are developing firmware for an IoT device that sometimes loses power mid-write and ends up with corrupted state. Describe defensive strategies at the software and firmware level to ensure state consistency and recovery: journaling, atomic writes, checksums, transactional updates, wear-leveling, and graceful degradation for the case where the state cannot be repaired.

MediumTechnical
31 practiced

Implement bool add_will_overflow(int32_t a, int32_t b) in C++ that returns true if a + b would overflow a 32-bit signed integer. Do not use a 64-bit type. Include unit tests for edge cases such as INT_MAX + 0, INT_MAX + 1, and negative overflows, and explain your approach.

Unlock Full Question Bank

Get access to all 10 Code Quality, Error Handling, and Defensive Programming interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.