Fraction to Recurring Decimal
Updated
Problem statement
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) → StringExamples
Example 1
numerator = 1denominator = 2return = "0.5"The remainder becomes zero after one decimal digit.
Example 2
numerator = 2denominator = 3return = "0.(6)"Remainder 2 repeats, so digit 6 is enclosed in parentheses.
Example 3
numerator = -50denominator = 8return = "-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.
More Airbnb Online Assessment questions
- In-Memory File SystemOA · Seen Nov 2026
- Task Management SystemOA · Seen Nov 2026
- Recipe Management SystemOA · Seen Nov 2026
- Progressive Banking SystemOA · Seen Nov 2026
- Credit Accounts with Scheduled TransfersOA · Seen Oct 2026
- Parcel Attribute Store with TTLOA · Seen Oct 2026
- Parcel Event TrackingOA · Seen Oct 2026
- Median Home Price by CityOA · Seen Sep 2026
- Worker Management (Promotion Track), Part 4: Full-Shift Double PayOA · Seen Sep 2026
- Worker Management (Promotion Track), Part 3: Promotions and SalaryOA · Seen Sep 2026
Similar questions at other companies
- Stacked Item and Category DiscountsPalantir · OAPalantir interview questions
- Bouncing Square Reaches a Screen CornerDuolingo · OADuolingo interview questions
- Arithmetic Triplets After QueriesCapital One · OACapital One interview questions
- Fractional Share Inventory FlatteningRobinhood · PHONE SCREEN · ONSITE INTERVIEWRobinhood interview questions
- Group-Relative Policy AdvantagesAmazon · ONSITE INTERVIEW
- Per-Segment SpeedingOnePay · PHONE SCREENOnePay interview questions
- Stock System with Updates and Percentage ChangeLead Bank · ONSITE INTERVIEWLead Bank interview questions
- Transaction Fee Calculator — Channel Volume TiersStripe · PHONE SCREENStripe interview questions
- Pro-Rata Order BookIMC Trading · ONSITE INTERVIEWIMC Trading interview questions
- Organization Reputation After Employee DeparturesHSBC · OAHSBC interview questions
Interview reports that mention this question
- Airbnb Intern OA and Onsite Rounds: 16 Experiences (Part 5 of 7)
- Airbnb Intern OA, Diplomacy Game: 19 Experiences (Part 4 of 7)
- Airbnb Intern OA, Halloween Candy: 22 Experiences (Part 3 of 7)
- Airbnb Intern OA, Donut Challenge: 22 Experiences (Part 2 of 7)
- Airbnb SWE Intern OA / Interviews: 122 Experiences (Part 1 of 7)