Bloomberg LP Interview Question
InternsCountry: United States
If the interviewer tell me to throw an exception, I'll do the following.
#include <iostream>
#include <climits>
using namespace std;
class overflow {};
int reverse_int(int n) {
auto r = 0;
const int boundary = n > 0 ? INT_MAX / 10 : INT_MIN / 10;
const int remainder = n > 0 ? INT_MAX % 10 : INT_MIN % 10;
while (n) {
auto digit = n % 10;
if ((r > 0 && (r > boundary || r == boundary && digit > remainder)) ||
(r < 0 && (r < boundary || r == boundary && digit < remainder))) {
throw overflow();
}
r = r * 10 + digit;
n /= 10;
}
return r;
}
A recursive version:
void printReverse(int x){
if (x< 0){
cout <<"-";
printReverse(-x);
}
else if (x< 10) cout <<x<<endl;
else {
cout <<x%10;
printReverse(x/10);
};
};
Here's some python code:
def max_place_value(x):
'''max_place_value(x) - find the maximum place value in the integer, x'''
if x == 0:
return -1
return max_place_value(x // 10) + 1
def reverse(x):
'''reverse(x) - reverse the digits in the integer, x'''
# store if x is negative or not
if x < 0:
neg = -1
else:
neg = 1
x = abs(x)
n = max_place_value(x)
r_num = 0
for i in range(0, n + 1):
place_value = 10 ** (n - i)
digit = x // place_value
r_num += digit * 10 ** i
x -= digit * place_value
return r_num * neg
print(reverse(-4872345)) # outputs -5432784
print(reverse(10478)) # outputs 87401
#include <string>
#inlclude <iostream>
void swap( char* a, char* b ){
char temp = (*a);
(*a) = (*b);
(*b) = temp;
}
int reverseInt( int a ){
std::string astr = std::to_string(a);
// reverse the string
for( int i = 0; i<astr.size()/2; ++i )
swap( &astr[i], &astr[astr.size()-i] );
return atoi(astr.c_str());
}
#include <iostream>
#include <sstream>
using namespace std;
void outReverse(int val)
{
stringstream s;
s << val;
//go to the end of the buffer
s.seekg( 0, s.end );
//init end position
int pos = s.tellg();
//iterate buffer in reverse order and out char
while( --pos >= 0 )
{
s.seekg( pos );
char c = s.peek();
cout << c;
}
cout << endl;
}
int main()
{
int val;
cin >> val;
outReverse(val);
return 0;
}
Python
def reverse(n):
arr1 = list(str(n)) # arr1 = ['1','2','3','4','5']
length=len(arr1) # length = 5
arr2 = ['']*length # arr2 = ['', '', '', '', '']
for char in arr1:
arr2[length-1] = char
length = length -1
# arr2 = ['5','4','3','2','1']
output = ('').join(arr2) # output = 54321
return int( output )
Below are two different Java methods to reverse the input of int:
public void reverseNumbers ( int input){
StringBuilder stringInput = new StringBuilder ( String.valueOf(input));
System.out.println(stringInput.reverse().toString());
}
public void reverseNumbersTwo ( int input){
String StringInput = String.valueOf(input);
char [] charArrayOfInput = StringInput.toCharArray();
for (int i =(charArrayOfInput.length-1); i>=0;i-- ){
System.out.print(charArrayOfInput[i]);
}
Below are two different Java methods to reverse the input of int:
public void reverseNumbers ( int input){
StringBuilder stringInput = new StringBuilder ( String.valueOf(input));
System.out.println(stringInput.reverse().toString());
}
public void reverseNumbersTwo ( int input){
String StringInput = String.valueOf(input);
char [] charArrayOfInput = StringInput.toCharArray();
for (int i =(charArrayOfInput.length-1); i>=0;i-- ){
System.out.print(charArrayOfInput[i]);
}
I also thought of a different approach. Please feel free to comment. I am open to feedback.
public class reverseNum {
public static int reverse(int num){
String numStr = String.valueOf(num);
StringBuilder reversedStr = new StringBuilder();
for (int i = numStr.length(); i > 0; i--){
reversedStr.append(numStr.substring(i-1, i));
}
int reversedNum = Integer.parseInt(reversedStr.toString());
return reversedNum;
}
public static void main(String[] args) {
int testNum = 1234;
reverse(testNum);
}
}
#include <cstdlib>
#include <iostream>
using namespace std;
void reverse(int & n);
/*
*
*/
int main(int argc, char** argv) {
int n;
cout<<"Enter n: ";
cin>>n;
cout<<endl;
reverse(n);
cout<<"Reversed number is : "<< n << endl;
return 0;
}
void reverse(int & n) {
int r = 0;
while(n > 0) {
r = r * 10 + (n % 10);
n /= 10;
}
n = r;
}
public class ReverseNumberPrinting {
public static void main(String[] args) {
int number = 12345;
int reverseNumber = reverseit(number);
System.out.println(reverseNumber);
}
private static int reverseit(int number) {
// TODO Auto-generated method stub
int reverse = 0;
int temp;
while (number != 0) {
temp = number % 10;
reverse = reverse*10 + temp;
number = number / 10;
}
return reverse;
}
}
Apparently, we have to ask the interviewer what should we do if the number will overflow after reversing.
- ravio September 23, 2014