## Cognzant Technology Solutions Interview Question for Software Engineer / Developers

Country: India
Interview Type: Written Test

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

// we want to find the minimum of x and y

int x; int y;

// the result goes here

int r;
// min(x, y)
r = y ^ ((x ^ y) & -(x < y));

It works because if x < y, then -(x < y) will be all ones, so r = y ^ (x ^ y) & ~0 = y ^ x ^ y = x. Otherwise, if x >= y, then -(x < y) will be all zeros, so r = y ^ ((x ^ y) & 0) = y.

NOTE:
On some machines, evaluating (x < y) as 0 or 1 requires a branch instruction, so there may be no advantage.

Reference: graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax

To understand this problem more clearly you may like to watch

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

LOL@ using x < y.

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

Like

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

x<y means if x<y then o/p 1 else 0

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

``````int x, y;
int max = x * (x > y) + y * (y > x);``````

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

Just an edit, you could use x>=y for one case to cover if both numbers end up being equal

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

superrr..:)

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

how is (x > y) implemented by the compiler without if check ?

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

``````int max(int i, int j)
{
int iMax = i - j;
unsigned int uiMax = static_cast<unsigned int> (iMax);
uiMax = uiMax >> 31;
return (j*uiMax) + (i * (1-uiMax));
}``````

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

1 bitwise op, 3 adds, 2 multiplies.

The top answer uses much cheaper operations. Something like 4 bit ops and 1 or 2 adds.

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

yes but top size also uses comparation operator <.
This solution seems very cute!

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

max=(x*(x/y)+ y*(y/x))/(y/x + x/y);

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

I don't think it would work if one of the no. is negative with a larger modulos , or if both are negatives .

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

Here is an approximate answer and untested... might have to replace 31 with bigger/smaller number...

``````double min(double x, double y) {
int n = 1 << 31;
double min = pow (pow (x, -n) + pow (y, -n), n);
}

double max(double x, double y) {
int n = 1 << 31;
double min = pow (pow (x, n) + pow (y, n), n);
}``````

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

Convert integers to doubles and convert back.

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

assuming 32 bit arch...else modify code

``````void max_min(int a,int b)
{
int i= (((a-b)&(1<<31))&&1)*a + (((b-a)&(1<<31))&&1)*b;
int j= (((b-a)&(1<<31))&&1)*a + (((a-b)&(1<<31))&&1)*b;
printf("min = %d max = %d",i,j);
}``````

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

a, b two numbers
((a-b) && x)b+!((a-b) && x)a
where x is the highest integer with a power of 2 possible in the machine.
eg.
for an 8 bit machine
highest power of 2 is 128 or bitwise 10000000 or in hex it is 80h.
a-b if neg set the MSB
if positive MSB=0

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

c=a-b;
let k is msb of c,i.e k=c>>7(for 8 bit numbers similarly 31 in place of 7 for 32 bit numbers)
d=a-k(a-b)
d is maximum of a and b.
if a is less then b in that case c is negative so k=1 ,if a is greater we have k=0

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

int main()
{
((a > b) && printf("a is bigger\n")) || printf("b is bigger\n");
return;
}

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

``````min(x,y) ==> r = y ^ ((x ^ y) & -(x < y));
max(x,y) ==> r = x ^ ((x ^ y) & -(x < y));``````

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

int max(int a, int b) {
int c = (unsigned int)(a-b) & 0x8000000;
return((~(!!c-1) & b) | ((!!c-1) & a));
}

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

int max(int a, int b)
return a>b? a:b

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

This answer is not what an interviewer would have wanted, but under many compilers, this answer technically not generate any branch instructions, and instead uses conditional adds, or else uses the same sort of bit hacks discussed in this thread. Many compilers are smart enough to use the approaches in this thread without a human writing the code that way.

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.