Reverse in integer in Java without using Strings or Arrays.
Salesforce.com, Market Street San Francisco asked me this question on an interview.
Three things you need to know:
1. Taking the input integer and modding (%) by 10 will extract off the rightmost digit. example: (1234 % 10) = 4
2. Multiplying an integer by 10 will "push it left" exposing a zero to the right of that number, example: (5 * 10) = 50
3. Dividing an integer by 10 will remove the rightmost digit. (75 / 10) = 7
Pseudocode:
a. Extract off the rightmost digit of your input number. (1234 % 10) = 4
b. Take that digit (4) and add it into a new reversedNum.
c. Multiply reversedNum by 10 (4 * 10) = 40, this exposes a zero to the right of your (4).
d. Divide the input by 10, (removing the rightmost digit). (1234 / 10) = 123
e. Rinse lather and repeat.
public int reverseInt(int input)
{
long reversedNum = 0;
long input_long = input;
while (input != 0)
{
reversedNum = reversedNum * 10 + input_long % 10;
input_long = input_long / 10;
}
if (reversedNum > Integer.MAX_VALUE || reversedNum < Integer.MIN_VALUE)
{
throw new IllegalArgumentException();
}
return (int)reversedNum;
}
JUnit Tests prove the above function works
import org.junit.*;
import static org.junit.Assert.*;
public class Reverse_the_intTest
{
@Test
public void test_Reverse_the_int01()
{
Reverse_the_int r = new Reverse_the_int();
assertEquals(1, r.reverseInt(1));
}
@Test
public void test_Reverse_the_int02()
{
Reverse_the_int r = new Reverse_the_int();
assertEquals(123, r.reverseInt(321));
assertEquals(213, r.reverseInt(312));
assertEquals(231, r.reverseInt(132));
}
@Test
public void test_Reverse_the_int03()
{
Reverse_the_int r = new Reverse_the_int();
assertEquals(-5, r.reverseInt(-5));
}
@Test
public void test_Reverse_the_int04()
{
Reverse_the_int r = new Reverse_the_int();
assertEquals(-25, r.reverseInt(-52));
}
@Test
public void test_Reverse_the_int05()
{
Reverse_the_int r = new Reverse_the_int();
assertEquals(0, r.reverseInt(0));
}
@Test(expected=IllegalArgumentException.class)
public void test_Reverse_the_int06()
{
Reverse_the_int r = new Reverse_the_int();
r.reverseInt(2147483647);
}
@Test(expected=IllegalArgumentException.class)
public void test_Reverse_the_int07()
{
Reverse_the_int r = new Reverse_the_int();
r.reverseInt(-2147483648);
}
@Test
public void test_Reverse_the_int08()
{
Reverse_the_int r = new Reverse_the_int();
assertEquals(846384741, r.reverseInt(147483648));
}
@Test
public void test_Reverse_the_int09()
{
Reverse_the_int r = new Reverse_the_int();
assertEquals(-846384741, r.reverseInt(-147483648));
}
@Test
public void test_Reverse_the_int10()
{
Reverse_the_int r = new Reverse_the_int();
assertEquals(11111, r.reverseInt(11111));
}
@Test
public void test_Reverse_the_int11()
{
Reverse_the_int r = new Reverse_the_int();
assertEquals(-11111, r.reverseInt(-11111));
}
@Test
public void test_Reverse_the_int12()
{
Reverse_the_int r = new Reverse_the_int();
assertEquals(1, r.reverseInt(10000));
}
@Test
public void test_Reverse_the_int13()
{
Reverse_the_int r = new Reverse_the_int();
assertEquals(1, r.reverseInt(00001));
}
}