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

Country: India
Interview Type: Written Test

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

int v; // we want to find the absolute value of v
unsigned int r; // the result goes here
int const mask = v >> sizeof(int) * CHAR_BIT - 1;

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

You can also watch this video to understand this solution

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

Good One.

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

can we not do it simply using abs function..??
like.......r=abs(v);

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

@second anonymous: i really don't know what to say to you. you are aware that functions don't just magically exist, that they have to be implemented somehow before they exist?

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

That's a good one! I was going to solve it less efficiently with something like:

``````int mask = v >> (sizeof(int) * CHAR_BIT - 1);

Explanation: when v is negative, mask is 11111....1111 and ~mask is 00000...0000. All the bits of (-v) will be kept and none of the bits of v, so we'll get -v + 0 = -v. When v is positive or zero, the mask is 00000...0000 and ~mask is 11111...1111, which means all bits of -v are discarded, and all the bits of v are kept. So we get 0 + v = v.

-v when v < 0
v when v >= 0

That's the definition of absolute value.

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

use 2's complement conversion(complemeent and add one), if no is in 2's complement, utilize the first bit of number and
the xor operation(with 00000...00 or 111111..111) which either complements or keep the number same

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

Using the property of multiplication: multiplying either two negative or either two positive numbers the result is always positive.

``int x=-10;``

``abs= sqrt((float)(x*x));``

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

the best one line solution i can think of:
//a be the no. whose abs has to befind...

cout<<(a^(-(0>a)))+(0>a);
//tested worked well for signed integers...:)

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

``````while (v < 0) {
return -v;
}
return v;``````

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

``````public int abs(int n)
{
try{
int a = int.MaxValue + n;
return -n;	//if it gets here, n is negative
}
catch{
return n;	//if gets here, n is positive
}
}``````

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

The question asks you to do it without "branching". Try-catch blocks definitely qualify as "branching", even if they don't contain if-else statements (internally exceptions use a mechanism similar to if statements).

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

``````#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
int array[] = {70, 30};
printf("largest: %d\n",array[!(array > array)]);
return 0;
}``````

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

basic assumption is incorrect here.
abs(-v) = v and abs(v) = v
you can refer any maths book for that.

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

abs(x) : if (x <0) then return 2's complement of x else return x

result = ( (x<0) & (~x+1)) | ( (x>0) & x )

means if x < 0 then find 2's complement of negative number and return
if x is positive return x anyways

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

x=(x>0)*x+(x<0)*-x;

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

``x=(x>0)*x+(x<0)*-x;``

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

n & (n – 1) == 0 //include 0
n & !(n & (n – 1))

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

``````int myabs(int a){
int mask = a >> (sizeof(int)*8-1);
return a;
}``````

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

(1&&(a&(1<<31)))*(-a) + (1&&((-a)&(1<<31)))*(a);

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.