Workday interview question

Reverse an integer without using string operations.

Interview Answers

Anonymous

Mar 12, 2019

I give up on copy/pasting code. Keeps mangling it. The basic idea is a while statement with orig > 10. Inside of loop add (mod 10 of orig) to reversed. multiply reversed by 10, divide orig by 10. Last digit will be after exiting loop. Just add remaining orig to reversed. There is an extra check for a negative number. Really easy once you figure out the mod 10 idea.

1

Anonymous

Mar 12, 2019

yet again mangled. while orig greater than or equal to 10.

Anonymous

Mar 12, 2019

That was fun.

Anonymous

Mar 12, 2019

Oops. answer would help. :) #include #include using namespace std; /* * */ int main(int argc, char** argv) { int orig = -12345; int reversed; bool neg = false; cout 10) { reversed += orig % 10; reversed *= 10; orig /= 10; } reversed += orig; if (neg) { reversed *= -1; } cout << "reversed = " << reversed << "\n"; return 0; }

Anonymous

Mar 12, 2019

Ugh. Code got mangled. Sure wish I could delete an errant comment. #include #include using namespace std; /* * */ int main(int argc, char** argv) { int orig = 12345; int reversed; bool neg = false; cout 10) { reversed += orig % 10; reversed *= 10; orig /= 10; } reversed += orig; if (neg) { reversed *= -1; } cout << "reversed = " << reversed << "\n"; return 0; }