Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
2
of 4 vote

If the number,n, is an exact power of two, return n-1
else find the nearest power of 2 to the number, find their difference,d, and return d-1
also, if n=0 return 1
ex: n=14
nearest power=16
d=16-14=2
return d-1=1
if n=16, since it is an exact power of 2, return 16-1=15

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

cool seems to work.

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

does not work with 5

- suvrokroy February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah, it works... there is little typo I think.... nearest power of 2 greater than the number itself.

- immodi91 February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If given number is 9, then one's complement in 8-Bit Integer machine would be 246.

As per your algorithm, it will return 6 which is incorrect.

- Sudhindra.A February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Implemented Alex's algorithm. Code works. Thanks.

#include<iostream>
#include<math.h>
using namespace std;

//one's complement
bool check_power_2(int n)
{
	if( (n & (n-1))== 0)
		return true;
	else
		return false;
}

int next_power(int n)
{	
	int c =0;
	while(n>0)
	 {
	 	n = n >> 1;
	 	c++;
	 }
	return pow(2,c);
}
int main()
{
	int n;
	cin >> n;
	
	if (n == 0)
	 	cout << 1 <<endl; 
	else if(check_power_2(n))
		cout << n-1 << endl;
	else 
		cout << next_power(n) - (n+1) << endl;

	return 0;

}

- David Billa February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sudhindra,
one's complement of 9 is 6.

- David Billa February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What's your definition of one's complement? One's complement could be either a binary representation of integers or an operator (the bitwise complement operator). In any case this answer assumes an arbitrary definition of one's complement, and the question is ambiguous too.

- dev.cpu February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 8 vote

xor with the all the 1 sets ...

x = ( (1 >> 31 ) ^ x )

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

why not simply use bit wise negation operator (~) and also I think that result of right shifting 1 by 31 will be 0 which when XOR with x is going to give x

- int80h February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the algo is wrong. it will just output x.

- David Billa February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

use complement operator or use lemma : ~a=-(a+1) :D

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

It will give you non-value assignment error

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

He is just pointing out the lemma there, not the actual code.

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

/*
Code according to Alex's solution.
If the number,n, is an exact power of two, return n-1
else find the nearest power of 2 to the number, find their difference,d, and return d-1
also, if n=0 return 1
ex: n=14
nearest power=16
d=16-14=2
return d-1=1
if n=16, since it is an exact power of 2, return 16-1=15

- alex on January 31, 2013

#include <math.h>
int c153148581sCompliment(int num){
    int closestExp2 = ceil(log10((double)num)/log10((double)2));
    int closestPower2 = pow(2,closestExp2);
    if(num==closestPower2)
        return num-1;
    else
        return closestPower2-num-1;
}

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

I think the most efficient way is to propagate all the bits:

unsigned getComplement(unsigned p) {
  if (p == 0)
    return 1;
  unsigned q = p;
  q = q | (q >> 1);
  q = q | (q >> 2);
  q = q | (q >> 4);
  q = q | (q >> 8);
  q = q | (q >> 16);
  return q - p;
}

- JacVlD February 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Why is everyone trying to write a program for it when you can use the complement operator (~) like Deepak pointed out?

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

dude that wont work.
If you try to do once complement of 3 .Compiler take all 32 like
000000000....11 and then 1's complement of 1111111...00.
so that is wrong answer compiler does not know.how many bits he needs to take care.

- Anonymous February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Let given number be x.

x's compliment = (power( 2, sizeof(int) -1) - x

For example, if given number is 13 and size of integer is 8. then,
13's compliment = (power(2,8)-1) - 13
= 255 - 13 = 242

- Sudhindra.A February 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include <stdio.h>

int main()
{
	int num;
	scanf("%d",&num);
	if((num & 0x000F) == num)
	{
		num = num ^ 0x000F;
		printf("The 1s complement is %d\n",num);
	}
		
	else if((num&0x00FF) == num)
	{
		num = num ^ 0x00FF;
		printf("The 1s complement is %d\n",num);
	}
	else if((num&0x0FFF) == num)
	{
		num = num ^ 0x0FFF;
		printf("The 1s complement is %d\n",num);
	}
	else 
	{
		num = num ^ 0xFFFF;
		printf("The 1s complement is %d\n",num);
	}
	return 0;
}

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

1s compliment of 6 == 1. your code is returning 9.. since you are taking six as 0110 and then inverting the bits == 1001.

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

@- isandesh7 Sorry i didnt get ur point...1's complement of 6 is 9 only

- Anonymous February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ncalculators.com/digital-computation/1s-2s-complement-calculator.htm

- Anonymous February 01, 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