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;

r = (v + mask) ^ mask;

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

You can also watch this video to understand this solution
youtube.com/watch?v=zJGts7hOX9s

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

Good One.

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

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

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

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

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

int mask = v >> (sizeof(int) * CHAR_BIT - 1);
return ( (-v) & mask) + (v & ~mask);

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.

- eugene.yarovoi September 11, 2012 | Flag
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

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

- Anonymous September 11, 2012 | Flag Reply
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...:)

- saraf2206 September 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

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

What about try-catch?

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
	}
}

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

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

- eugene.yarovoi November 11, 2012 | Flag
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[0] > array[1])]);
    return 0;
}

- surendra.emb October 28, 2014 | Flag Reply
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.

- anonymous December 16, 2014 | Flag Reply
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

- Anonymous December 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- athul April 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- athul April 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- truongkhanh February 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- swapnilsj August 06, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

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