Microsoft Interview Question for Software Engineer / Developers






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

int inc(int n)
{
int i=1;
while(n&i)
{
n &= ~i;
i <<= 1;
}
return (n|i);
}

- xjshi July 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good one!!!

- Avneesh July 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

found it easy n nice.. good work.

- learner July 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice!!

- CSK July 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i = 1;
		temp = n;
		while(n & i) {
			temp ^= i;
			i <<= 1;
		}
		printf("%d,", temp^i);

- Anonymous August 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i = 1;
		temp = n;
		while(n & i) {
			temp ^= i;
			i <<= 1;
		}
		printf("%d,", temp^i);

- Anonymous August 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how about n = (n | 1)

- Anonymous July 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

works only for odd numbers

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

for even numbers... sorry. :)

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

Why not even:

say 6 = 110
OR
001
Output should be 111 = 7.

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

The problem will be when you need to carry example
if number is 7 and you expect result 8. hmm.

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

-(~num)

- usha July 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you cannot use - .

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

int plus(int a)
{
int res=a;
int i=0;
res=res^(1<<i++);
while(a&1)
{
a>>=1;
res=res^(1<<i++);
}
return res;
}
And the second way:
a+=1;

- mifan July 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry, forgot about ++
int plus(int a)
{
int res=a;
int i=1;
res=res^i;
i<<=1;
while(a&1)
{
a>>=1;
res=res^i;
i<<=1;
}
return res;
}

- mifan July 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you just explain the logic you wrote above. I am lost a bit

- Anonymous July 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

We are starting on the right and change the values ​​of bits until we meet 0

- mifan July 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int inc(int value){
		
		// even value
		if( isEven(value) ){
			return value | 1;
		}
			
		// odd value
		int mask = 1;			
		while( (value & mask) != 0 ){
			value &= ~(mask);
			mask <<= 1; 				
		}
			
		value |= mask;			
		
		return value;
	}

- m@}{ July 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why does all people are too inclined to post code without any explanation? If they care to help others, I'd say first to give idea. Code would be a bonus (specially when it's hard to get the idea completely from narration)!

- anonymous July 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

great xishi simplest way to increment :)

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

The main thing to look out for is when you have a number such as 0x0011 where adding one will carry to the next bit.

MaskA does addition using & for each bit position. For each operation if there is a carry, the result will be greater than one.
Example:
0x0011 & 0x0001 = 0x0001
0x0011 & 0x0010 = 0x0010
(and so on). As long as the result is not zero then we will have a carry bit.

MaskB is used to keep track of these carry bits, every time the result of the above operation is greater than one, we shift as follows.
0x0001->0x0011->0x0111 etc.

Once we break out of the loop we xor the original value with MaskB to get the final result. Example, 0x0011 ^ 0x0111 = 0x0100, which is what we want.

The code works for negatives!! woot!

public void bitAdd(int val){
        int maskB = 0xFFFFFFFF - 1;
        int maskA = 0x0001;
        
        while(maskA>=1){
            if((maskA & val) == 0){
                break;
            } else {
                maskA = maskA << 1;
                maskB = maskB << 1;
            }
        }
        System.out.println("Adding one:" + (val ^ ~(maskB)));   
    }

- boqurant July 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey45223" class="run-this">#include<stdio.h>
#include<stdlib.h>

int main(int argc,char **argv)
{
if(argc<2) {printf("Usage ./a.out int\n"); return -1;}
int number=atoi(argv[1]);
unsigned char *charPointer = NULL;
charPointer = (unsigned char*)&number;
printf("%d\n",number);
if((number%2)==0)
charPointer[0]=charPointer[0] | 0x1;
//charPointer[0]=~charPointer[0];
else{
unsigned int complementNumber=~number;
number=(-1)*complementNumber;
}
printf("\n%d\n",number);

return 0;
}
</pre><pre title="CodeMonkey45223" input="yes">
</pre>

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

<pre lang="" line="1" title="CodeMonkey97417" class="run-this">The key is to find first zero and change the lower portion staring from that bit to 10....0. For example,

22 = 10110 -> 10111 = 23
23 = 10111 -> 11000 = 24
24 = 11000 -> 11001 = 25
25 = 11001 -> 11010 = 26
26 = 11010 -> 11011 = 27

public int minusOne(int n) {
int mask = 1;
int temp = ~n;
while ((mask & temp) == 0) {
mask |= mask << 1;
}
return mask ^ n;
}
</pre><pre title="CodeMonkey97417" input="yes">
</pre>

- Anonymous July 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

seems like it doesn't work for -1.

- boqurant July 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The method should be named as plusOne insttead of minusOne. Sorry for the misleading naming.

Alternatively, it can be implement as below:
"
public int plusOne(int n) {
int mask = 1;
int lowerSection = 1;
while ((mask & n) > 0) {
mask <<= 1;
lowerSection |= mask;
}
return n ^ lowerSection;
}
"

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

What about this?
if n is even, return n|1
else
n=n<<1;(n=n*2)
n=n|1; (now as n is even this gives n+1)
return ceil(n>>1); (ceil of n/2)

where ceil is ceiling function.

- dood July 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int add(int a, int b)
{
if(b==0)
return a;
int sum = a^b;
int carry=(a&b)<<1;
add(sum,carry);
}//its given in careercup book

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

int integerincrement(int n)
{
int i=1;
while(n&i)
{
n=n^i;
i=i<<1;
}
return(n|i);
}

- geeks July 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int increment(int n)
{
int j=1;
while(n&j)
{
n=n^j;
j=j<<1;
}
n=n^j;
return n;}

- raju engineer July 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about
{

(int)ceil(entered_no+0.01)

}
ceil-ceiling function
?

- Benhur July 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please read the question properly!

Given an integer n, write code to calculate n+1, *without using* +,-,++,--,*,/

- SymbolofSilence July 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int increment(int n)
{
if(!(n&1)) return n|1;
else
{
int x;
for(x=1;n&x;x<<=1) n=n^x;
return n^x;
}
}

- SymbolofSilence July 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can you simple statement

int x = value ^ 1

- seshu.chakka July 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(i=0;i<sizeof(int);i++)
{
if(!(n & (1<<i)))
break;
n=n & ~(1<<i);
}

n = n | (1<<i);

- mrn July 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int increment(int n)
{
int z=1;
int x=n;
while(z&x)
{
x=x^z;
z=z<<1;
}
n=z|x;
return n;
}




}

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

#include<stdio.h>
void main(){
int n;
printf("enter you no.");
scanf("%d",&n);
if(n%2==0)
n=n|1;
else
{
if((n&2)==0)
{
n=n>>1;
n=n|1;
n=n<<1;
}
else if((n&4)==0)
{
n=n>>2;
n=n|1;
n=n<<2;
}
else if((n&8)==0)
{
n=n>>3;
n=n|1;
n=n<<3;
}
else if((n&16)==0)
{
n=n>>4;
n=n|1;
n=n<<4;
}
else if((n&32)==0)
{
n=n>>5;
n=n|1;
n=n<<5;
}
else if((n&64)==0)
{
n=n>>6;
n=n|1;
n=n<<6;
}
else if((n&128)==0)
{
n=n>>7;
n=n|1;
n=n<<7;
}
else if((n&256)==0)
{
n=n>>8;
n=n|1;
n=n<<8;
}
}
printf("%d",n);
}

- saurav ambast July 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what about ((n << 1) | 1) >> 1 ?

- Anonymous August 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

will not work for odd numbers.
think about 7.
((7<<1|1)>>1)=7

- Anon November 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *p = n;
printf("%d",&p[1]);

- utkarsh srivastav May 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C# code without bit-twiddling

public static int PlusOne(int input)
        {
            return Enumerable.Range(input, 2).Last();
        }

- bero July 30, 2013 | 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