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
youtube.com/watch?v=C95-S1jsbMo

- fresherparty September 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL@ using x < y.

- Anonymous September 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Like

- jiangok2006 September 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous September 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 4 vote

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

- Vaibhav.D September 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- vabhdman September 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

superrr..:)

- pkm September 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous April 07, 2014 | Flag
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));
}

- barcod September 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1 bitwise op, 3 adds, 2 multiplies.

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

- Anonymous September 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Nathaniel September 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

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

- Ganesh R September 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- vabhdman September 11, 2012 | Flag
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);
}

- Anonymous September 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Convert integers to doubles and convert back.

- Anonymous September 11, 2012 | Flag
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);
}

- AB September 11, 2012 | Flag Reply
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

- Anonymous October 12, 2012 | Flag Reply
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

- Anonymous September 21, 2013 | Flag Reply
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;
}

- keshav singh June 15, 2016 | Flag Reply
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));

- swapnilsj August 06, 2016 | Flag Reply
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));
}

- Toseef October 24, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

- Anonymous September 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Anonymous September 11, 2012 | 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