Skip to content
Problem 166 · LeetCode 166(opens in a new tab)

Fraction to Recurring Decimal

MediumHash tables

You get two integers, numerator and denominator. Return their quotient as a string in decimal form.

If the digits after the point go on forever, they must fall into a block that repeats. Write the part before the block as usual and put the repeating block in brackets, so 1 / 6 is 0.1(6). If the division ends, return the exact result with no brackets, and a whole result has no point at all.

Example 1

Input:  numerator = 5, denominator = 12
Output: "0.41(6)"
Why:    0.41666…, and the 6 repeats forever
2 more examples and the constraints

Example 2

Input:  numerator = -3, denominator = 8
Output: "-0.375"
Why:    the division ends after three digits

Example 3

Input:  numerator = 12, denominator = 4
Output: "3"
Why:    a whole number, so no decimal point

Constraints

  • −2³¹ ≤ numerator, denominator ≤ 2³¹ − 1
  • denominator ≠ 0
  • The answer is shorter than 10,000 characters

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Long division, scan for repeatsO(L²)
  2. Long division with a remainder mapO(L)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for a 10,000-digit answerVerdict
Long division, scan for repeatsO(L²)
Correct, but rescans every remainder
Long division with a remainder mapO(L)
Best; one lookup per digit

L = number of digits in the answer, under 10,000. L² / 2 is about 50 million comparisons.

Edge cases to test

  • Zero on top. 0 / −5 is 0, with no minus sign.
  • Overflow. −2,147,483,648 / −1 is 2,147,483,648, which does not fit in a 32-bit int.
  • Exact results. 12 / 4 is 3 and −3 / 8 is −0.375: no brackets, and no point for whole numbers.
  • Digits before the cycle. 7 / −12 is −0.58(3): the brackets open after the 58, not at the point.
  • Long cycles. 1 / 9967 repeats a block of 9,966 digits.

Common mistakes

  • Detecting the cycle by repeated digits instead of repeated remainders; 1 / 9967 starts 0.0001…, with zeros that do not repeat as a block.
  • Taking absolute values in 32 bits, which overflows on −2³¹.
  • Putting the opening bracket right after the point every time.

How to talk through it

  1. 1Clarify. Can either number be negative or zero? Is the answer length bounded?
  2. 2Recall long division. Each digit comes from the remainder times 10, and the next remainder is what is left.
  3. 3Find the cycle. The remainder is the whole state, so the first repeated remainder marks the start of the repeating block.
  4. 4Make it fast. Store remainder → digit index in a hash map: one lookup per digit, and the index tells where to open the bracket.
  5. 5Test out loud. Run 5 / 12, a whole result like 12 / 4, a negative, and −2³¹ / −1.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?