Interview Question


Country: United States




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

#include <stdio.h>
#include <limits.h>
int main()
{
	unsigned int num;
	printf("enter the num \n");
	scanf("%d",&num);
	
	int i=INT_MAX ^ (INT_MAX>>1);	

	while(!(i&num))
		i>>=1;

	int k=1;
	while(i>k)
	{
		int a=i&num
		int b=k&num
		if( (a == 0 && b >0) || (a > 0 && b == 0)){
			num^=i;
			num^=k;
		}			
		i>>=1;
		k<<=1;
	}

printf("the num is %d \n",num);
return 0;
}
//Explanation
1> First i found the length of binary representation of given number.
2> Then using two pointer, one pointed to LSB and another pointed to MSB of number.
3>If both pointed bit is not same than swaped the bit.
4>Now pointer pointed to MSB shifted to left and Pointer Pointed to LSB shifted to Right.

- Aalok July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice

- Bin December 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int main()
{
unsigned num,x,y;
bool temp=0;
cin>>hex>>num;
x = 1 << (sizeof(unsigned) * 8 - 1);
y = 0x1;
for(int i=0;i<sizeof(unsigned int)*4;i++)
{
temp = num & x; //LHS
if(num & y) num |= x;
else num &= (~x);
if(temp) num |= y;
else num &= (~y);
x>>=1;
y<<=1;
}
cout<<hex<<num<<endl;
return 0;
}

- Anonymous July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

main()
{
char a[]= "1101";
int i, len,t;

len = strlen(a);
printf (" Given String is : %s ", a);
for(i=0;i<(len/2);i++)
{
t=a[i];
a[i]=a[len-1-i];
a[len-1-i]=t;
}
printf ("\n In place algo gives : %s ", a);
getch();
}

- Aditya Verma July 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Aditya Verma, You have to reverse the bits of a given integer.

- Aalok July 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

: Its In-place Algo .... Its not reverse

- Aditya July 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

main()
{
//char a[]= "1101";
char a[4]={0};
int n=4,k=0;
int i=0, len,t;
while(n!=0)
{
a[i]=n&1;
n=n>>1;
i++;
}

len = strlen(a);
printf (" Given String is : %s ", *a);
for(i=0;i<(len/2);i++)
{
t=a[i];
a[i]=a[len-1-i];
a[len-1-i]=t;
}
printf ("\n In place algo gives : %s ", a);
getch();
}

- Aditya: Updated for given Integer July 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>
main()
{
int j,n;
char a[10],b[10];
printf("enter the number");
scanf("%s",a);
n=strlen(a);
for(j=0;j<n-1;j++)
{
b[j]=a[n-2-j];
}
b[n-1]='\0';
printf("%s",b);
}

- Siva Teja August 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main() {
int n;
scanf("%d", &n);
while (n) {
if (n & 1)
printf("1");
else
printf("0");

n >>= 1;
}
printf("\n");
}

Output:
25
10011

- jainrajrahul December 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int reverseIntInPlace(int a)
{



int b = a;
int r = 0;
while(b>0)
{
r = r<<1;
r = b%2+r;
b = b/2;
}
}

- Bin December 12, 2012 | 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