Walt Disney Company Interview Question: Write code to reverse the dig... | Glassdoor

Interview Question

Software Engineer Interview Seattle, WA

Write code to reverse the digits in an integer.

Answer

Interview Answer

5 Answers

8

int reverse(int a) {
        int b = 0;
        while (a>0) {
            b *= 10;
            b += a%10;
            a /= 10;
        }
        return b;
    }

no one on Mar 1, 2013
1

I am not sure this solution would work because if a was 93, your program would produce the answer 39.3, but I am not really a software guy so idk. my logic is that

a=93
93%10 = 3
b=3
a=9.3
*next iteration*
b=30
9.3%10 = 9.3
b = 39.3

is this correct? just as a disclaimer, I am not trying be an jerk, I am just practicing interview questions lol

Robert Hannum on Jun 12, 2015
1

To Robert: Remember integer division will truncate the value, so 93/10 = 9, not 9.3.

Anonymous on Jul 1, 2015
0

function reverseNum(num) {
    let result = 0,
         isNeg = num 0) {
        result = result * 10 + num % 10;
        num = Math.floor(num / 10);
    }
    return isNeg ? -1 * result : result;
}

Charles Hill on Jul 30, 2016
0

The above formatted incorrectly.

function reverseNum(num) {
    let result = 0,
        isNeg = num 0) {
        result = result * 10 + num % 10;
        num = Math.floor(num / 10);
    }
    return isNeg ? -1 * result : result;
}

Charles Hill on Jul 30, 2016

Add Answers or Comments

To comment on this, Sign In or Sign Up.