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)'.
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample Answer
Requirements
- Input: integers
num,denwithden != 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...