Fraction to Recurring Decimal

Algorithms
Medium
Salesforce
108.8K views

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses.

Why Interviewers Ask This

Salesforce interviewers ask this to evaluate your ability to handle edge cases and manage state in deterministic algorithms. They specifically test if you can detect cycles in long division using hash maps, ensuring you don't rely on floating-point arithmetic which introduces precision errors. This problem assesses logical rigor and attention to detail, critical for building reliable enterprise financial or data processing systems.

How to Answer This Question

1. Clarify requirements immediately: confirm integer ranges, negative number handling, and zero denominator scenarios. 2. Handle signs separately by storing the result sign and working with absolute values to simplify logic. 3. Perform integer division to get the whole number part before addressing the decimal. 4. Use a HashMap to store remainders and their corresponding indices in the result string; this is the key mechanism to detect repeating decimals. 5. Iterate through the remainder multiplication process: if a remainder repeats, insert parentheses at the stored index; if the remainder becomes zero, the fraction terminates. 6. Combine the parts into the final string, ensuring correct placement of the negative sign and parentheses.

Key Points to Cover

  • Explicitly separating sign handling from the core division logic
  • Using a HashMap to track remainders for O(1) cycle detection
  • Avoiding floating-point arithmetic to prevent precision errors
  • Correctly identifying the insertion index for parentheses when a cycle is found
  • Handling edge cases like zero numerators or terminating decimals

Sample Answer

To solve the Fraction to Recurring Decimal problem efficiently, I would first address edge cases like a zero numerator or invalid denominators. I'll start by determining the sign of the result based on whether the numerator and denominator have different signs, then work exclusively with their absolute values to keep the core logic clean. First, I calculate the integer part using standard division. If the remainder is zero, we return the integer part immediately. Otherwise, I append a decimal point and begin tracking the fractional part. The crucial step involves using a Hash Map to store each remainder we encounter alongside its position in our result string. In every iteration, I multiply the current remainder by ten and divide by the denominator to find the next digit. Before appending this digit, I check if this new remainder exists in our map. If it does, we've found a cycle; I insert an opening parenthesis at the stored index and close it at the end. If the remainder becomes zero, the decimal terminates, and we simply append the remaining digits without parentheses. Finally, I prepend the negative sign if needed. For example, inputting 1 and 6 yields '0.(1)', while 4 and 333 yields '0.(012)'. This approach ensures O(N) time complexity where N is the length of the repeating cycle, avoiding floating-point inaccuracies entirely.

Common Mistakes to Avoid

  • Relying on floating-point types (double/float) which lose precision for large integers
  • Failing to detect cycles because the remainder storage logic is flawed or missing
  • Incorrectly placing parentheses by not tracking the exact index where the cycle begins
  • Neglecting to handle negative numbers properly, leading to incorrect output formats

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 49 Salesforce questions