Goldman Sachs Coding Interview Question | Repeating Decimals | Python



Repeating Decimals is one of the most famous and difficult Goldman Sachs Interview Questions. In this video, we will discuss the algorithm to solve the Mystery of Repeating/Recurring Decimals in Python.

There is also a question for you to add to the solution at the end. make sure to check it out, solve and put your solution down in the comments section below.

Check out the other video on a Goldman Sachs Coding Interview Question here:-

Check out my website –

Join me on Instagram –

Check out my facebook page –

Check out my twitter handle –

source

Avatar of PyLenin

By PyLenin

23 thoughts on “Goldman Sachs Coding Interview Question | Repeating Decimals | Python”
  1. can you solve this problem please?? #### Challenge – Pig Latin

    Pig Latin is a farcical "language" used to entertain children, but also to teach them some valuable language concepts along the way. Translating English to Pig Latin is simple:

    1) Take the first consonant (or cluster of consonants) of a word, move it to the end of the word, and add a suffix of "ay"

    2) If a word begins with a vowel, just add "way" at the end

    3) For the sake of readability, separate the Pig Latin-ized parts of the word with a dash `-`

    Your challenge is to implement the method `pigLatinize` that takes a string phrase and translates it to Pig Latin. You're free to add additional classes, variables, and methods if you would like.

    The input phrase could be as short as a single word, or as long as multiple sentences or paragraphs. Whitespace, capitalization, and punctuation should be honored and maintained in your final answer.

  2. Good question. Here is my code solved using pure REGEX. 

    number_list=[(1,3),(1,4),(1,6),(1,7),(1,8),(1,9),(1,-3),(-1,6),(-1,-4)]

    def repeat_decimal(member):

    num,dem=member

    if (dem==0 or num==0 or num%dem==0):
    return
    else:
    number=num/dem

    single_repeat=re.match(r'(.*.)(?=(d)(?=2))',str(number)) # say 0.333 case

    group_repeat=re.match(r'(.*.)(?=(d+)(?=2))',str(number)) # say 0.12341234 case

    second_repeat=re.match(r'(.*.)(?=(d)(d)(?=3))',str(number)) # say 0.1555 case

    if bool(single_repeat):
    print(f'{member} –> {single_repeat.group(1)}({single_repeat.group(2)})')
    elif bool(group_repeat):
    print(f'{member} –> {group_repeat.group(1)}({group_repeat.group(2)})')
    elif bool(second_repeat):
    print(f'{member} –> {second_repeat.group(1)}{second_repeat.group(2)}({second_repeat.group(3)})')
    else:
    print(f'{member} –> {number}')

    for i in number_list:
    repeat_decimal(i)

    answer: 

    (1, 3) –> 0.(3)
    (1, 4) –> 0.25
    (1, 6) –> 0.1(6)
    (1, 7) –> 0.(142857)
    (1, 8) –> 0.125
    (1, 9) –> 0.(1)
    (1, -3) –> -0.(3)
    (-1, 6) –> -0.1(6)
    (-1, -4) –> 0.25

  3. Hi Pylenin, I think something might be missing.
    if the repeating part contains a decimal that occurs more than once ( 1/17= 0.(0588235294117647 ) in the case of 1/17, the second occurrence of 8 would break the while and we won't be getting the right answer.

Leave a Reply

Your email address will not be published.

Captcha loading...