FastPrepFraction to Recurring Decimal

Fraction to Recurring Decimal

Goldman Sachs logoGoldman Sachs● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given signed integers numerator and nonzero denominator, return their exact decimal representation.

If the fractional part repeats, enclose the shortest repeating suffix in parentheses. Do not include unnecessary trailing zeroes.

Function

fractionToDecimal(numerator: int, denominator: int) → String

Examples

Example 1

numerator = 1denominator = 2return = "0.5"

The decimal terminates.

Example 2

numerator = 4denominator = 333return = "0.(012)"

The remainder repeats after the digits 012.

Constraints

  • -2^31 <= numerator <= 2^31 - 1
  • -2^31 <= denominator <= 2^31 - 1
  • denominator != 0

More Goldman Sachs problems

See Goldman Sachs hiring insights
public String fractionToDecimal(int numerator, int denominator) {
  // write your code here
}
numerator1
denominator2
expected"0.5"
Checking account…