Given two integers, numerator and denominator, return the decimal representation of the fraction numerator/denominator as a string.
- If the fractional part terminates, output the exact decimal (trailing zeros are acceptable).
- If the fractional part is repeating, enclose the repeating sequence in parentheses.
- You may assume denominator ≠ 0 and the sign should be handled correctly.
Examples
- input = 12, 5 → output =“2.4”(or“2.40000”)
- input = 1, 6 (0.1666…) → output =“0.1(6)”
Simulate long division. Track each remainder’s first index in the fractional string via a hash map. When a remainder repeats, insert parentheses around the substring from its first occurrence to the end. Handle sign, zero integer part, and terminating case when remainder becomes zero.
The VOprep team has long accompanied candidates through various major company OAs and VOs, including Oracle, Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for these companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.