Compass Interview Question: Write a function to reverse a... | Glassdoor

Interview Question

Software Engineer Interview New York, NY

Write a function to reverse an integer, without using any

  intermediate storage except for other integer values.
Answer

Interview Answer

2 Answers

0

Something simple like:

def reverse_integer(integer):
    if not integer:
        return integer
    b = int(math.log(integer, 10))
    d = 0
    v = 0
    while True:
        i, integer = divmod(integer, 10 ** b)
        v += i * (10 ** d)
        if not b:
            break
        d += 1
        b -= 1
    return v

Interview Candidate on Jan 30, 2016
0

Something like this:

http://codepad.org/ZgQnTZA0

Adam on Feb 4, 2016

Add Answers or Comments

To comment on this, Sign In or Sign Up.