Render a Fraction as a Decimal With the Repeating Part Marked

Last updated: November 29, 2026

Quick Overview

Given an integer numerator and denominator, return the decimal string, enclosing any repeating fractional part in parentheses, for example 1/3 gives '0.(3)' and 1/6 gives '0.1(6)'.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given an integer numerator and denominator, return the decimal string, enclosing any repeating fractional part in parentheses, for example 1/3 gives '0.(3)' and 1/6 gives '0.1(6)'.

Asked in Google onsites to test careful long division with a hash map, in the style of a calculator feature in Search. The interviewer watches for the remainder-to-position map that detects the cycle, and for sign handling and edge cases like negative zero and the minimum integer.

What the Interviewer Expects
  • Handles sign separately and works with absolute values
  • Computes the integer part with divmod
  • Records each remainder's position to detect repetition
  • Inserts parentheses at the first repeated remainder's position
  • Handles exact division and zero numerator
Key Topics to Cover
Hash Map
Long Division
Cycle Detection
Strings
Math
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What is the maximum length of the repeating part for denominator d, and why?
  • Convert a repeating decimal string back into a reduced fraction.
  • How would you output in base b instead of base 10?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: integers num, den with den != 0.
  • Output: a string such as '2', '-0.5', '0.(3)', '0.1(6)'.
  • No trailing zeros, no negative zero....
High-Level Design

Handle the sign: the result is negative if exactly one operand is negative and the numerator is non-zero. Work with absolute values. divmod gives the integer part and a remainder. If the remainder i...


Submit Your Answer
Markdown supported