The Fraction to Recurring Decimal interview question asks you to convert a given numerator and denominator into its decimal string representation. The catch is that if the fractional part repeats infinitely, you must enclose the repeating sequence in parentheses. For example, 1 / 3 becomes "0.(3)", and 1 / 2 becomes "0.5".
This is a classic Math and Hash Table interview pattern asked by companies like Airbnb, Meta, and Google. It tests your ability to manually simulate long division, a process everyone learns in elementary school but few implement in code. It evaluates your attention to detail regarding edge cases (like zero, negative signs, and integer overflow) and your ability to detect cycles.
The solution uses Long Division Simulation combined with a Hash Map.
Numerator: 4, Denominator: 33
"0."{4: 2} (remainder 4 seen at index 2)."0.1"{4: 2, 7: 3}."0.12""0.(12)".-2^31 (Integer.MIN_VALUE) exceeds the maximum positive 32-bit integer. You must cast the numerator and denominator to 64-bit integers (long) before doing abs()."0".When simulating long division, always map the remainder to the index, not the quotient digit. Different remainders can produce the same quotient digit, but seeing the same remainder guarantees the exact same sequence of calculations will follow.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Integer to Roman | Medium | Solve | |
| Reconstruct Original Digits from English | Medium | Solve | |
| Roman to Integer | Easy | Solve | |
| Count Number of Texts | Medium | Solve | |
| Maximum Manhattan Distance After K Changes | Medium | Solve |