Bloomberg LP Interview Question for Interns


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
5
of 7 vote

Don't need to convert to String .. do it using %, * and + operators :)

Keep in mind -> 756 %10 = 6 .. (6 * 10) + 5 = 65 ... so you can come up with the algorithm :)

- marcelovox2 January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But how do you extract the 5?

- isandesh7 January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

You have to use related subtraction... that is the way to get the 5 ... I wrote a simple code below showing it ..

public void test() {
		int i = 123456789;
		
		int r = reverse(i);
		
		System.out.println(r);

	}

	private int reverse(int i) {
		
		int r = 0;
		int d = 10;
		int s = 0;
		
		while(i > 0){
			int remainder = i % 10;
			r = (r * 10) + remainder;
			s = 0;
			while(i > 10){
				i -= d;
				s++;
			}			
			i = s;			
		}
		return r;
	}

- marcelovox2 January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

/* Here is the code without using / and & operators...
Complexity is O(d) where d is the number of digits. 
Since for every digit the inner for loop can take atmost 10 units of time which is constant */

int revInt(int in) {
    int ten = 10, ans = 0, prevDigit = 0, i = 0, nTen, sTen = 1;
    while (sTen < in) {
        nTen = (in % ten) - prevDigit;
        for (i = 0; nTen > 0; i++, nTen -= sTen);
        ans = ans * 10 + i;
        prevDigit = (in % ten);
        ten = ten * 10;
        sTen = sTen * 10;
    }
    return ans;
}

- isandesh7 January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

1. Convert it to a string.
2. Read the string character-wise and push each character on the stack
3. Pop the stack and print the answer (reversed string)

- r_king January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how do u convert to a string ..prolly using "/" and "%"...so not sure if this is the way

- bbarodia January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

use sprintf to convert int to string, then reverse the string , and use sscanf to convert string to int. using duoble pointers to reverse.

- yingsun1228 January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can do / by just using + if allowed
756/10
int i=0,k=-1;
while(i<756)
{i+=10;
k++;}
return k;

- zyfo2 January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the key is how to get the every digits in the number. eg. how we get 5 in 756. We could use this way:
756%10 = 6 => 756-6 = 750 => 750%100=50 => and then we get 5 to use operator +
int k = 0;
int n = 0;
while(n<50){n+=10;k++}
At the end , k will be 5.

We could use the same method to get 7...

- Guo Xu January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string convertInt(int number)
{
if (number == 0)
return "0";
string temp="";
string returnvalue="";
while (number>0)
{
temp+=number%10+48;
number/=10;
}
/* for (int i=0;i<temp.length();i++)
returnvalue+=temp[temp.length()-i-1];
return returnvalue;*/
return temp;
}

- Raj January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
int inumber,i;

scanf("%d",&inumber);
char pNumber[10];
printf(" Number:%d",inumber);

sprintf(pNumber, "%d", inumber);

int iLength=strlen(pNumber);
char temp;

for(i=0;i<iLength;i++)
{
temp=pNumber[i];
pNumber[i]=pNumber[iLength-1];
pNumber[iLength-1]=temp;
iLength--;
}

inumber=atoi(pNumber);

printf("\n\nReverse no is %d",inumber);

return 0;
}

- Raja January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main()

{
int a;
printf("Enter the number to be reversed:");
scanf("%d",&a);
int c_mod=0,k=10,b=0,flag=0;
float j=0.1,i=10,temp;
while(a)
{
flag=0;
j=0.1;
c_mod=a%k;
j=j*k;
i=j;
temp=i;
if(i<c_mod)
{
while(i<c_mod)
{
i+=j;
flag++;
}
}

if(c_mod == temp)
{
b=(b*10)+1;
a = a - c_mod;
if(!a)
break;
}
else if(flag)
{
b=(b*10)+(flag+1);
a = a - c_mod;
}
else
{
b=(b*10)+c_mod;
a = a - c_mod;
}
k=k*10;
}
printf("The reversed disgit is:%d\n",b);
}

- Amit January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not a single use of library function nor i have used "/" or "&".

- Amit January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
#include<string.h>


int main(){
int num;
char str[10];
char str1[10];
int i;
printf("\n Enter the number");
scanf("%d",&num);

sprintf(str, "%d", num);

int len = strlen(str);

printf("\n %s",str);

for(i=0;str[i]!='\0';i++){
str1[i]=str[len-1-i];
}
str1[i]='\0';
printf("\n %s",str1);
getch();
return 0;

}

- Ankit February 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Python,

def reverse_number(num) {
	return int(str(num)[::-1])

- Mayur February 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not using % and / -> so use multiplication:
{
int x = 756;
int ret = 0, j=0, pos=(7-1);
int d[7] = {1000000, 100000, 10000, 1000, 100, 10, 1};
while(x<d[j]) //find first pow
++j;
while(j<7)
{
int i=0;
while((i+1)*d[j]<=x)
++i;
ret += i*d[pos--];
x-=i*d[j++];
}
}

- Anonymous March 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

# include <iostream>
# include <sstream>
# include <stack>
using namespace std;

int reverse_number(int number) {
stack<char> charStack; string str;
string strText = static_cast<ostringstream*>( &(ostringstream() << number) )->str();
for(int i = 0; i< strText.size(); ++i) charStack.push(strText[i]);
while(!charStack.empty()) { str+=charStack.top(); charStack.pop(); }
int result; istringstream(str) >> result;
return result;
}

int main() {
int number = 756;
cout << reverse_number(number);
return 0;
}

- ftim0107 August 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

int main()
{
	int i = 756;
	char c[2] = " ";
	sprintf(c,"%d",i);
	printf("%s\n",c);
	for(i=2;i>=0;i--)
		printf("%c",c[i]);
	return 0;
}

- agmegharaj November 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int reverseint(int a )
{
int i=0,j=10,r=0,k,m=0;
while(a>0)
{
r=r*10+a%10;
a=a-a%10;
k=a;
m=0;
while(k>0)
{
k=k-j;
m++;
}
a=m;
}
return r;
}

- Anonymous December 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why complex solutions:

public static void reverseANumber () {
    int n = 756;
    
    int rem = 0;
    while (n != 0) {
      rem = n%10;
      System.out.print(rem);
      n = n/10;
    }
    System.out.println("");
  }

- shekhar2010us March 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

None of the above answers are correct. Convert to string, reverse the characters and then convert back to value is not allowed. Repeatedly subtracting 10 is not efficient.

The correct answer is that although you are not allowed to divide by 10, you are allowed to right shift bits, which is equivalent to divide. So consider that

X / 10 = X * (1/10) = X*(2^N/10)/2^N = (X*(2^N/10))>>N

Pick a reasonable N, like N=9. The calculation becomes:
X/10 = (X*(512/10))>>9 ~= (X*51)>>9

This is only approximately but gets you pretty much close to using divide directly.

So original number is 756:

(756*51)>>9 = 38556>>9 = 75 which is the quote.

We check the remainder:
756 - 75*10 = 6, which is within expected range 0 to 9. If it is not you increment the quote and try again. In this case we successfully split 756 into 75x10 + 6.

Next (75*51)>>9 = 7, the quote,
and the remainder is:
75 - 7x10 = 5, which looks good.

This is a powerful technique to convert expensive division to merely multiplication and shift right. I use this trick a lot.

- Anthony Mai October 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main(int argc, char const *argv[])
{
	int n;
	float n_float;
	printf("Enter the number\n");
	scanf("%d", &n);
	n_float = n;

	int temp = 0;
	while((int)n_float != 0)
	{
		temp =  temp * 10 + (int) n_float % 10;
		n_float = (int) n_float * 0.1;
	}
	printf("Reverse: %d\n", temp);
	return 0;
}

- Siraj December 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

since "/" is not allowed, first convert the number to a string, then iterate the string from last to first character. In java, the code should be like:
int result = 0;
for(int i = str.length() - 1; i >= 0; i--){
result = result * 10 + str.charAt(i) - '0';
}
We should consider the overflow situation as the reversed number may be larger than the max value of integer.

- LP January 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'd point out that the string conversion function is likely to use / in doing its work.

- Anonymous January 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 3 vote

With Given Constraints.. the only Solution is the pointer.

1> Point to given integer.
2> Read value from the pointer and advance to next after putting the value to the Link List.
3> Reverse the list.
4> Read the value of the list and put it into the integer.

- hprem991 January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you read value from an integer pointer you get the entire number .. How are you getting the individual digits out??

- DigitalFire April 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

hprem991 wishes to use char *.. not int * to get individual digits.
But this will work for hex numbers only. it will not work for decimal.

- satish June 12, 2013 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More