Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Return `c * 9.0 / 5.0 + 32`. Use floating-point (or multiply before dividing) so 9/5 isn't truncated to 1.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you anticipate integer-division truncation in a formula.
Where candidates slip
Computing 9/5 in ints and getting 1.
How to stand out
Volunteer the truncation pitfall and use floating-point.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Direct formula
1Hint 1Explore hintHide hint
It's a direct formula — what numeric detail can still go wrong?
2Hint 2Explore hintHide hint
What does `9/5` evaluate to in integer arithmetic?
3Hint 3Explore hintHide hint
Use doubles or reorder so the fraction survives.
A · Core idea
Apply the formula F = c × 9/5 + 32. There's no algorithm — the interview signal is numeric care, specifically avoiding integer-division truncation.
Why `9/5` is the trap. In integer arithmetic, 9 / 5 evaluates to 1 (truncated), which would turn the formula into c + 32 and corrupt every result. Use floating-point — c * 9.0 / 5.0 + 32 — or reorder to c * 9 / 5 so the multiplication happens before the division.
Why mention it on a 'trivial' question. Volunteering the integer-division pitfall on an easy conversion is exactly the kind of detail that signals carefulness — the same instinct that prevents real bugs in money/units code.
Order of operations. Multiply/divide before adding 32; the formula's structure must be preserved.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
c = 1009/5 int
= 1 (truncated)
100+32=132 (WRONG)212.0C · Code with teaching comments
D · Why it works and complexityO(1) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
On a 'trivial' conversion, mentioning the integer-division pitfall is exactly what distinguishes a careful answer.
ComplexityO(1) time, O(1) space — a constant number of arithmetic operations.
E · Edge cases and pitfalls5c = 0 — 32.0 (freezing).Explore edge casesHide edge cases
Edge cases handled
c = 032.0 (freezing).c = 3798.6 (body temp).c = -40-40.0 (the scales meet).
Pitfalls
- Integer `9/5` evaluating to 1.
- Applying the formula in the wrong order.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic double toFahrenheit(double c) { return c * 9.0 / 5.0 + 32; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Integer `9/5` evaluating to 1.
- Applying the formula in the wrong order.
Revise
Carry the pattern into the interview
Pattern to remember
Direct formula + type care
Code pattern to memorize
F = c * 9.0 / 5.0 + 32Rules to remember
- 1F = c·9/5 + 32.
- 2Use doubles for 9/5.
- 3Watch integer division.
Use this pattern when
- Formula evaluation with fractional factors
Choose another pattern when
- Integer 9/5 truncating to 1
Interview takeaway
Even a formula question tests awareness of integer division.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Direct formula + type care. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups