Render an Ad Spend Ratio as a Decimal With Repeating Parts
Last updated: November 29, 2026
Quick Overview
Given an integer numerator and denominator, return the fraction as a decimal string, enclosing any repeating part of the fractional digits in parentheses, for example 1/6 -> "0.1(6)".
Meta
November 29, 20260
0
0 solved
Given an integer numerator and denominator, return the fraction as a decimal string, enclosing any repeating part of the fractional digits in parentheses, for example 1/6 -> "0.1(6)".
A Meta onsite question that combines long division with cycle detection. Interviewers expect you to track remainders in a hash map and handle signs and zero correctly, since that is where most candidates slip.
What the Interviewer Expects
- Simulate long division one digit at a time
- Detect repetition by remembering where each remainder first appeared
- Insert parentheses at the right position
- Handle negative signs and a zero numerator
- Explain why the cycle length is at most the denominator
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
- How long can the repeating part be for denominator d?
- Convert a repeating decimal string back to a reduced fraction.
- How would you cap output length for very large denominators?
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 numerator and denominator, denominator != 0. Output: string with integer part, and if needed a '.' with fractional digits, repeating part in parentheses. Examples: 1/2 -> "0.5", 2/1 ->...
High-Level Design
Determine the sign from the XOR of the signs (and no sign if the numerator is 0). Work with absolute values. The integer part is n // d; the remainder r = n % d drives the fractional digits.
Each...