An engineer used a floating-point type to store monetary amounts, and now a totals report is off by a cent in some cases. Walk me through why this happens and what you'd use instead.

technical-conceptual · Senior level · software-engineering

What the interviewer is really asking

Assesses whether the candidate understands how IEEE 754 represents values in binary, why decimal fractions and summation order cause rounding error, and can choose a representation appropriate to money rather than reciting 'don't use floats' without the mechanism.

What to say

What to avoid

Example answers

Strong: The mechanism is that 0.10 has no exact binary representation in IEEE 754, so every amount carries rounding error, and because float addition isn't associative the batched sum drifted differently than the row-by-row total. I moved money to integer cents end to end, with rounding only at explicit display and tax boundaries. The penny discrepancies disappeared and totals became reproducible regardless of summation order.

Weak: Floats lose precision, so I'd round the total to two decimal places at the end, and switch from float to double for more accuracy in the meantime.

Want questions matched to your role? Paste a job title, job description, or CV and get a personalized set, or go Pro to unlock the full bank.