## Microsoft Interview Question for Software Engineer / Developers

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

James comment is insightful.
Joker's explanation is good.

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

swap (a,b) {
a = a + b
b = a - b
a = a - b
}

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

swap(a,b)
{
a = a^b;
b = a^b;
a = a^b;
}

Comment hidden because of low score. Click to expand.
0

there could be a problem if one of then is 0

Comment hidden because of low score. Click to expand.
0

it works perfect u dumbo!

Comment hidden because of low score. Click to expand.
0

Thanks Mr. Anonymous!

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

Subtraction/addition may be less efficient when expanded to assembly.

Comment hidden because of low score. Click to expand.
0

This is wrong.

Adding and subtracting only takes one clock. While multiply takes 6-8 clocks, division takes from 40-80 clocks.

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

Shouldn't the question be swap two numbers in place?Just wondering..

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

Srini, since we are not using an extra temporary variable, I am assuming that the two methods discussed above are in-place.

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

Jack, i didn't get u r answer. what does a^b mean?

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

a^b , we are dealing with XOR here.

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

first one could cause overflow; second should be rite

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

in case it is int numbers
a and be should be float or double
for a = 5, b=6
a = (b*10) + a; // = 65
b = a; // = 65

a = (a/10); // automatic casting from double
b = (b%10);

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

a=a^b;
b=a^b=(a^b)^b=a^0=a
a=a^b=(a^b)^(a)=a^a^b=0^b=b

a=a+b;
b=a-b=(a+b)-b=a
a=a-b=(a+b)-a=b
-------------------------------------
Groovy!!

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

a=a^b;
b=a^b=(a^b)^b=a^0=a
a=a^b=(a^b)^(a)=a^a^b=0^b=b

a=a+b;
b=a-b=(a+b)-b=a
a=a-b=(a+b)-a=b
-------------------------------------
Groovy!!

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

a=a^b;
b=a^b=(a^b)^b=a^0=a
a=a^b=(a^b)^(a)=a^a^b=0^b=b

a=a+b;
b=a-b=(a+b)-b=a
a=a-b=(a+b)-a=b
-------------------------------------
Groovy!!

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

a^=b^=a^=b;

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

b=(a+b)-(a=b);

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

Guys, check out for the case when a == b. The XOR logic will make a = b and b = b. Sh, the entire xor logic should be in if (a != b) { XOR logic goes here }

Comment hidden because of low score. Click to expand.
0

What is the problem? Since a==b, then a = b and b = b is the correct result.

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.

### 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.