Reported February 2022
Airbnbhash table

Fraction to Recurring Decimal

Reported by candidates from Airbnb's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Airbnb OA. Under 2s to a working solution.
Founder's read

The hash map is the whole game in this one. Airbnb candidates reported Fraction to Recurring Decimal in February 2022, and it looks like simple long division until the repeating digits show up. You've got an OA coming and this is the kind of problem where one missed edge case sinks you. Negative signs, integer results, and overflow all bite. The idea is small, but you have to write it cleanly under pressure. If you blank on how to detect the cycle, StealthCoder is the invisible safety net that reads the problem and hands you the approach live.

The problem

Given two integers numerator and denominator, return their exact decimal representation as a string.
If the fractional part terminates, return it normally.
If part of the fractional digits repeats forever, enclose the repeating part in parentheses.
Include a leading minus sign exactly when the result is negative.
The result must not contain unnecessary leading zeros or a decimal point when the value is an integer.

Function
fractionToDecimal(numerator: int, denominator: int) → String

Examples
Example 1
numerator = 1
denominator = 2
return = "0.5"
The remainder becomes zero after one decimal digit.
Example 2
numerator = 2
denominator = 3
return = "0.(6)"
Remainder 2 repeats, so digit 6 is enclosed in parentheses.
Example 3
numerator = -50
denominator = 8
return = "-6.25"
The signs differ and the fractional part terminates.

Constraints
-2^31 <= numerator <= 2^31 - 1.
-2^31 <= denominator <= 2^31 - 1.
denominator != 0.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Do long division by hand and track remainders. Handle the sign first: the result is negative only if exactly one input is negative. Convert both to absolute values using a 64-bit type, since abs(-2^31) overflows a 32-bit int. Append the integer part. If the remainder is zero, you're done with no decimal point. Otherwise add the point, then loop: store each remainder in a hash map pointing to the index in the output where its digit will land. Multiply the remainder by 10, append remainder divided by denominator, then take the new remainder. If you see a remainder already in the map, insert an opening parenthesis at the stored index, append a closing one, and stop. The classic pitfall is storing digits instead of remainders, or forgetting the zero numerator case, which must return "0" with no minus sign. If you freeze during the live OA, StealthCoder can surface this remainder-map structure so you just type it out.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Fraction to Recurring Decimal cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as fraction to recurring decimal. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Airbnb's OA.

Airbnb reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Fraction to Recurring Decimal FAQ

What's the trick to Fraction to Recurring Decimal?+

Track remainders in a hash map from remainder to the position in the result string. When a remainder repeats, the digits from that stored position onward cycle forever. Wrap them in parentheses. A repeated remainder guarantees a repeated digit sequence, because the next step depends only on the remainder.

How hard is this problem really?+

Medium in difficulty, but the algorithm is short. Most failures come from edge cases, not the idea. Negative numbers, a zero numerator, integer results with no decimal point, and 32-bit overflow on -2^31 are where candidates lose points. Test those four by hand before submitting.

Why do I need 64-bit integers?+

The constraints allow -2^31 for either input. Taking the absolute value of -2^31 overflows a signed 32-bit int. Cast both to long before calling abs, and do all remainder math in long too, since remainder times 10 can also get large.

How do I decide where the opening parenthesis goes?+

When you first see a remainder, save the current length of the result string in the map. That's the index where the next digit will be written. If the remainder shows up again later, insert the opening parenthesis at that saved index and append the closing one at the end.

How do I prepare for this in 48 hours?+

Write it from scratch twice without looking. Then run these cases: 1/2, 2/3, -50/8, 0/5, -1/-3, and the minimum int divided by -1. If those all pass, you're ready. The pattern is hash map plus simulation, and it's reusable for cycle-detection questions.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Airbnb.

OA at Airbnb?
Invisible during screen share
Get it