Bloomberg LP Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

while(num) {
   rem  = num%10;
   num  = num/10;
   str += rem+'0';
}
return reverse(str);
//'0' -ASCII 48 to convert int to char --standard way :)

- code_pro March 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if num < 0 ?

- Anonymous September 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Or recursively to avoit the reverse cmd:

void nrOut(int nr){
if(nr) {
nrOut(nr/10);
cout << '0' + (char) nr%10;
}
}
void itoa(int nr) {
if(nr) nrOut(nr) else cout<<"0";
}
}

- Grisch November 17, 2009 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Sorry, I mean
Or recursively to avoid the reverse cmd:

void nrOut(int nr){
 if(nr) {
  nrOut(nr/10);
  cout << '0' + (char) (nr%10);
 }
}

void itoa(int nr) {
 if(nr) nrOut(nr) else cout<<"0";
}

- Grisch November 17, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

need to handle negative numbers explicitly

- ranjanbs84 October 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

My C# code for the same, may be helps someone :)

class itoa
    {
        string result = string.Empty;

        static void Main()
        {
            itoa sample = new itoa();
            int input = -4821;
            if (input != 0)
            {
                sample.itoaRecur(input);
            }
            else
            {
                sample.result = "0";
            }
            Console.WriteLine(sample.result);
        }


        public void itoaRecur(int input)
        {
            if (input < 0)
            {
                result += '-';
                input *= -1;
            }

            if (input > 0)
            {
                itoaRecur((input / 10));
                result += (char)input % 10;
            }
        }
    }

- ranjanbs84 October 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void reverse(char *s)
{
int i,j;
for(i=0,j=strlen(s)-1;i<j;i++,j--)
{
char t=s[i];
s[i]=s[j];
s[j]=t;
}
}
char* itoa(int n)
{
static char str[50];
int i=0;
while(n)
{
str[i++]=(n%10)+'0';
n=n/10;
}
str[i]='\0';
reverse(str);
return str;
}
main()
{
int n=1234;
char *s;
s=itoa(n);
printf("string is %s\n",s);
return 0;
}

- rajnesh March 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//another solution for that problem is


#include<stdio.h>
#define max 50
char* itoa(int n)
{
static char str[max];
int i=max-1;
str[i--]='\0';
while(n)
{
str[i--]=(n%10)+'0';
n=n/10;
}
return &str[++i];
}
main()
{
int n=1234;
char *s;
s=itoa(n);
printf("string is %s\n",s);
return 0;
}

- rajnesh March 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <malloc.h>

int main(){
int n=123;

// Calculate the length of integer
int length=0;
int tmp=n;

do{
 tmp=tmp/10;
 ++length;
}while(tmp!=0);

if (n<0){++length;}

// Convert it into string
char *s = (char*)malloc(sizeof(char)*(length+1));
s[length]='\0';
int i=length-1;

tmp=abs(n);
do{
 int rm = tmp%10; 
 s[i] = rm + '0';
 tmp = tmp/10;
 --i;
}while(tmp!=0);

if(n<0){s[0]='-';}
printf("%s\n",s);
return 0;
}

Running time O(n)

- Jaikishan Jalan March 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <malloc.h>

int main(){
int n=123;

// Calculate the length of integer
int length=0;
int tmp=n;

do{
 tmp=tmp/10;
 ++length;
}while(tmp!=0);

if (n<0){++length;}

// Convert it into string
char *s = (char*)malloc(sizeof(char)*(length+1));
s[length]='\0';
int i=length-1;

tmp=abs(n);
do{
 int rm = tmp%10; 
 s[i] = rm + '0';
 tmp = tmp/10;
 --i;
}while(tmp!=0);

if(n<0){s[0]='-';}
printf("%s\n",s);
return 0;
}

Running time O(n)

- Jaikishan Jalan March 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int
itoa (int num, char *str, int radix)
{
register int i, neg = 0;
register char *p = str;
register char *q = str;

if (radix == 0) radix = 10;
else if (radix < 2 || radix > RADIX_MAX) {
return (radix);
}
if (num == 0) {
*p++ = '0';
*p = 0;
return (0);
}
if (num < 0) {
neg = 1;
num = -num;
}
while (num > 0) {
i = num % radix;
if (i > 9) i += 7;
*p++ = '0' + i;
num /= radix;
}
if (neg) *p++ = '-';
*p-- = 0;
q = str;
while (p > q) {
i = *q;
*q++ = *p;
*p-- = i;
}
return (0);
}

- T March 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This wasn't me either!

- the original T March 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nor was that !

- The Very Original T April 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int i = 6000;
stringstream ss<<i;
return ss.c_str();

- Anonymous September 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use sprintf
sprintf(str,"%d",a);

- Dude October 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *ita(int a)
{//cout<<a;
char c[10],tmp;
int i=0,a1=a,sgn=0;

if (a<0)
{ sgn=1; a*=-1; }



// cout<<a<<endl;
while(a1!=0){
a1/=10;//cout<<a1<<endl;
i++;}
// cout<<i<<endl;
for(a1=0;a1<i;a1++)
{
c[a1]=(a%10) + 48;
a/=10;
} c[i]='\0';
a1=0;i--;
cout<<c<<endl;

while(a1<i)
{
tmp=c[i];
c[i]=c[a1];
c[a1]=tmp;
a1++;
i--;
}

i=strlen(c);
if (sgn==1)
{
for(a1=strlen(c);a1>0;a1--)
{
c[a1]=c[a1-1];
}
c[0]='-';
c[i+1]='\0';
}
cout<<c;
}

- its working.. March 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I contribute a simple one.

void my_itoa(int x, char* s)
{
int base = 1;
int digit = 0;
int y = x;

while(y>0 && y/10>0)
{
base*=10;
y = y/10;
}

y = x;

while(base>0)
{
digit = y/base;
*s++ = digit + '0';
y-=digit*base;
base/=10;
}

*s = '\0';
}

- dullboy April 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This considers value 0 and < 0 too

void myitoa(int int_val, char * str)
{
	int abs_val = int_val ;
	char tmp_str [32] ;
	int str_index = 31 ;

	if (int_val == 0 )
	{
		strcpy(str, "0") ;
		return ;
	}

	abs_val = abs( int_val ) ;
	
	tmp_str[str_index--] = '\0' ;
	
	while(abs_val)
	{
		int cur_digit ;

		cur_digit = abs_val % 10 ;
		abs_val /= 10 ; 

		tmp_str[str_index--] = cur_digit + '0' ;
	}
	
	if( int_val <0 )
	{
		tmp_str [str_index] = '-' ;
		strcpy ( str, tmp_str + str_index ) ; 
	}
	else
	{
		strcpy ( str, tmp_str + str_index + 1 ) ; 
	}
	
}

- Rishi December 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Easy, in C# :-)

string itoa(int n) {
    return n.ToString();
}

- Anonymous March 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

How dumb can you get? This is never a thing interviwer wanted see. You need to implement YOUR 'Itoa' function.

- T March 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey! That wasn't me!

- The original T March 26, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

lol, you made my day!

- Ankur July 19, 2009 | 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