Google Interview Question for Software Engineer / Developers






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

#include <iostream>
#include <string>
using namespace std;

void main()
{
	int dec = 7;
	string binary = "";

	while(dec)
	{
		if ((dec & 1) == 1)
		{
			binary = "1" + binary; 
		}
		else
		{
			binary = "0" + binary;
		}
		dec >>= 1;
	}

	cout << binary << endl;
	
}

- Anonymous October 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have to give you -1. you didn't consider negative value:

void dectobin(int x,int count)
{
	unsigned a;
	if(x<0)
	{
      a=0xffffffff+x+1;
	}
	if((x==0)&&(count==0))
	{
	printf("%d",x);
	return;
	}
	else
	{
	  a=x;
	}
	if(a>0)
		dectobin(a>>1,count+1);

	if(a!=0)
	printf("%d",a%2);
	return ;
}

- Charles January 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int bin[20] = {0};
int dec = 0, rem = 0;
int i = 1;
cout<<"\n**** Converting a Decimal no to Binary ****\n";
cout<<"\n Enter the Decimal no: ";
cin>>dec;
while(dec>0){
rem = dec%2;
if(rem==1) //Case for odd numbers
bin[20] = 1;
bin[20-i] = rem;
dec = dec/2;
i++;
}
cout<<"\n No in Binary is: ";
for(int j=0; j<20; j++)
cout<<bin[j];

- avadh.786 September 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public string DecToBin(int dec)
{
if (dec == 1)
{
return (1).ToString();
}
else
{
//Divide the number by two and append '0' if even or '1' if odd.
return DecToBin(dec / 2) + (dec % 2).ToString();
}
}

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

public string DecToBin(int dec)
{
//if 1 return 1.
if (dec == 1)
{
return (1).ToString();
}
else
{
//Divide the number by two and append '0' if even or '1' if odd.
return DecToBin(dec / 2) + (dec % 2).ToString();
}
}

- Ashwath September 29, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

U gonna get stack overflow for dec == 0. Here is a quick fix:

public string DecToBin(int dec) {
if (dec <= 1){
  return (dec).ToString();
} else {
  //Divide the number by two and append '0' if even or '1' if odd.
  return DecToBin(dec / 2) + (dec % 2).ToString();
  }
}

- Stas September 30, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Come on, guys. This is C, not Java or C#.

#include <stdio.h>
#include <string.h>
int main(int argc, char** argv) {
  if (argc < 2) {
    printf("Give me a number.\n");
    return 0;
  }
  int num = atoi(argv[1]);
  printf("decimal: %d\n", num);
  char bin[33];
  memset(bin, '0', 32);
  char* c = &bin[32];
  *c = 0;
  while (num) {
    c--;
    if (num & 1) *c = '1';
    num = ((unsigned int) num) >> 1;
  }
  printf("binary: %s\n", c);
}

- Ryan October 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String dec2bin( int value ) {
	if ( value == 0 )
		return “0”;

	StringBuilder sb = new StringBuilder();

	while ( value != 0 ) {
		sb.append( value % 2 );
		value /= 2;
	}

	return sb.reverse().toString();
}

- Igor S. October 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

who says it's only about integer? could be a float too

- swk October 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

int main()
{
int n, rem, num, i=1;

printf("enter no\n");
scanf("%d", &n);

while(n>0 )
{
rem = n % 2;
n = n/2;
num = (rem * i)+num;
i = i * 10;
}

printf("binary no: %d", num);
}

- ridercoder October 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

THIS CODE IS WORK FOR ALL WHETHER IT IS DECIMAL OR FLOAT

{
{
{

#include <iostream>
#include <string>
using namespace std;



int main()
{
float dec = 7.5;
string binary = "";

char *c=(char *)&dec;

int var_size=sizeof(dec); //calculate size of decimal number

for(int i=0;i<var_size;i++){ // loop to traverse each byte of number


char num = *c;

for(int x=0;x<8;x++) // find each bit in a byte
{

if ((num & 1) == 1)
{


binary = "1" + binary;

}
else
{

binary = "0" + binary;
}
num >>= 1;

}


c++;
}

cout << binary << endl;
getchar();
}



}
}
}

- ridercoder October 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

FLOAT WILL BE PRINTED IN IEEE FORMAT

- ridercoder October 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void binaryConverter(int n) {
if (n == 0)
return;
binaryConverter(n / 2);
System.out.print(n % 2);
}

- Pedro November 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

std::string DecToBin(int x)
{
	std::string bin;
	if(!x)
		return "0";
	while(x)
	{
		std::ostringstream os;
		os << (x&1);
		x = x>>1;
		bin = os.str() + bin;
	}
	return bin;
}

- limaye.nikhil November 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <math.h>
int main()
{
int num ;
int i ;
printf("Enter the number:");
scanf("%d" ,&num);

for(i=0;i<16;i++) /* 16 is to display 16 bits */
printf("%d",((num << i & 1<<15) ? 1:0));

printf("\nCome to end+++++++++\n");


}

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

#include<stdio.h>
int main()
{
long long int a,p,m,q;
printf("enter any intiger to convert it into binary\n");
scanf("%Ld",&q);
a=q;
p=1;
while( a > 1 )
{
a=a/2;
p=p*2;
}
while( p >= 1 )
{
m=q/p;
if( m % 2 == 0 )
{ printf("0"); }
else
{ printf("1"); }
p=p/2;
}
}

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

#include<stdio.h>
int main()
{
long long int a,p,m,q;
  printf("enter any intiger to convert it into binary\n"); 
  scanf("%Ld",&q);
  a=q;
  p=1;
  while( a > 1 )
  {
    a=a/2;
    p=p*2;
  }
   while( p >= 1 )
   {
      m=q/p;
      if( m % 2 == 0 )
      { printf("0"); }
      else 
      { printf("1"); }
      p=p/2;
    }
    }

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

#include<stdio.h>
int main()
{
long long int a,p,m,q;
printf("enter any intiger to convert it into binary\n");
scanf("%Ld",&q);
a=q;
p=1;
while( a > 1 )
{
a=a/2;
p=p*2;
}
while( p >= 1 )
{
m=q/p;
if( m % 2 == 0 )
{ printf("0"); }
else
{ printf("1"); }
p=p/2;
}
}

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

#include<stdio.h>
int main()
{
long long int a,p,m,q;
printf("enter any intiger to convert it into binary\n");
scanf("%Ld",&q);
a=q;
p=1;
while( a > 1 )
{
a=a/2;
p=p*2;
}
while( p >= 1 )
{
m=q/p;
if( m % 2 == 0 )
{ printf("0"); }
else
{ printf("1"); }
p=p/2;
}

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

#include<stdio.h>
int main()
{
long long int a,p,m,q;
printf("enter any intiger to convert it into binary\n");
scanf("%Ld",&q);
a=q;
p=1;
while( a > 1 )
{
a=a/2;
p=p*2;
}
while( p >= 1 )
{
m=q/p;
if( m % 2 == 0 )
{ printf("0"); }
else
{ printf("1"); }
p=p/2;
}
}

- Anonymous December 18, 2016 | Flag Reply


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