Format Exact Fractions with Parenthesized Repeating Digits

Quick Overview

Render exact signed fractions with finite decimals or parenthesized repeating digits using remainder-based long division.

Format Exact Fractions with Parenthesized Repeating Digits

Company: Goldman Sachs

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return the decimal representation of `numerator/denominator` as a string. Enclose a repeating fractional part in parentheses. If the fraction has a finite decimal representation, return a finite representation. When more than one valid representation is possible, any is acceptable. ```cpp string fractionToDecimal(int numerator, int denominator); ``` ### Examples ```text Input: numerator = 2, denominator = 7 One valid output: "0.(285714)" ``` The representation `"0.2(857142)"` also has the same repeating decimal value. ```text Input: numerator = 7, denominator = -4 Output: "-1.75" ``` ### Constraints & Assumptions - Both arguments span the signed 32-bit range. - The denominator is nonzero. - The source guarantees that an answer string has length less than 10000. - A terminating fraction must not be replaced by an infinite repeating representation. ### What a Strong Answer Covers - Exact sign, integer part and fractional digits, including minimum signed inputs. - Identification of a repeated remainder and correct parenthesis placement. - Finite termination, zero, and work proportional to produced digits. ### Follow-up Questions - What should be returned for a zero numerator and negative denominator? - Why does a repeated remainder identify the repeating digits?

Overview: Render exact signed fractions with finite decimals or parenthesized repeating digits using remainder-based long division.

Goldman Sachs logo
Goldman Sachs
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
1
0

Problem

Return the decimal representation of numerator/denominator as a string. Enclose a repeating fractional part in parentheses. If the fraction has a finite decimal representation, return a finite representation. When more than one valid representation is possible, any is acceptable.

string fractionToDecimal(int numerator, int denominator);

Examples

Input: numerator = 2, denominator = 7
One valid output: "0.(285714)"

The representation "0.2(857142)" also has the same repeating decimal value.

Input: numerator = 7, denominator = -4
Output: "-1.75"

Constraints & Assumptions

  • Both arguments span the signed 32-bit range.
  • The denominator is nonzero.
  • The source guarantees that an answer string has length less than 10000.
  • A terminating fraction must not be replaced by an infinite repeating representation.

What a Strong Answer Covers Guidance

  • Exact sign, integer part and fractional digits, including minimum signed inputs.
  • Identification of a repeated remainder and correct parenthesis placement.
  • Finite termination, zero, and work proportional to produced digits.

Follow-up Questions Guidance

  • What should be returned for a zero numerator and negative denominator?
  • Why does a repeated remainder identify the repeating digits?