## NVIDIA Interview Question

Software Engineer Interns**Country:**United States

int pool[2] = {b, c};

int index = ((uint32)0x80000000 & ((uint32)(b-c))) >> 31;

int result = pool[index];

All I could come up with was (a&(~b))^(a||c)

a == 0 ==> 0^c = c

a == 1 ==> ~b^1 = b

Your answer just blew my mind away

Is this really correct? AFAIK any value not zero is true in C. A could be 2 and the result should be "b", but would it not be 2b here?

This is a clever answer!

I think a && (result = b, 1) || (result = c) should do though.

Explanation of the syntax:

Basic idea: (result = b, 1) always evaluates to "1". Just a clever way to set "result = b" while making sure the whole expression always evaluates to "1" even when value of b is 0.

Now the explanation is simple:

1. when a != 0, only (result = b, 1) gets executed effectively setting result to b.

2. when a == 0, no matter what, (result = b, 1) and (result = c, 0) gets evaluated in that order effectively setting result to c.

It's a good explanation. I am not it is perfectly complete, though. There is one property here that is necessary for it to work that is not mentioned and that is the short-circuiting of the disjunctive boolean operator when one of its operand predicates has evaluated to a true value. If that was not the case, the first part before the || operator could evaluate to true and "result" still be assigned to "c".

As to why (result = b, 1) always evaluates to "1", it is due to a sequence of statements always returning the value of the last expression.

BTW, I haven't put much thought into this, yet, but I am not sure this would work in more strongly typed languages like C# and the question does not ask about C/C++ specifically. I'll think about that a bit more.

Yeah, I was thinking that, too. Good catch! It's also better that you put parentheses around each operand, because it would then work for any integer-valued expression.

Assume b and c are integers:

```
int[] array = new int[2];
int[0] = b;
int[1] = c;
result = array[a];
```

int main()

{

int v,result;

int a,b,c;

scanf("%d",&a);

v=if(a)

result=v*b+(1-v)*c;

printf("%d",result);

}

```
int max_with_no_conditionals(int a, int b, int c)
{
int arr[] = { c, b };
return arr[!!a]; // force boolean (0 or 1) conversion
}
```

What happens if you use a switch? is if valid?

switch(a)

case a:

return b;

default:

return c;

How about following?

c^((b^c)&-(a&1))

Following is logic behind this.

1. First mask a to convert into either 1 or 0 using a&1

2. Now change (a&1) into negative number, in case of 1 it will be converted into -1 and in case of 0 it will be 0. When it will become -1 then in binary it will be full of 1 sequence.

3. Following are two cases.

a. When a&1 is 0 then result should be c

b. When 1&1 is 1 then result should be b.

Based on above two condition applied logic.

r = y ^ ((x ^ y) & -(x < y));

Why it works:

â€¢

C represents the Booleans

TRUE

and

FALSE

with

the integers

1

and

0

, respectively.

â€¢

If

x < y

, then

âˆ’(x < y) = âˆ’1

, which is all

1

â€™s in

twoâ€™s complement representation. Therefore,

we have

y ^ (x ^ y ) = x

.

â€¢

If

x â‰¥ y

, then

âˆ’(x < y) = 0

. Therefore, we have

y ^ 0 = y

.

Assuming all functions return

`uint8_t`

, how about:

```
uint8_t result = 0xFF;
(result & ((((a()) ^ 0) & (b())) | ((!((a())) ^ 0) & (c()))))
```

I used no conditional operators( >, < , ==) for my solution.

```
a = a*a+1;
int *array = new int[a];
array[a-1] = b;
array[0] = c;
result = array[a-1];
```

If the variable a was 0, then the length of the array is 1 and a-1 equals 0.

Therefore the variable c in array[0] = c overrides b in array[a-1] = b

/* key is to convert a = 1 if a!=0 and a =0 otherwise with out using condition - http://bits.stephan-brumme.com/sign.html */

// ((unsigned int) a) >> 31) will be 0x00000001 for negative numbers, else 0x00000000

// negateA >> 31 will be 0x00000001 for positive numbers, else 0x00000000

unsigned int negateA = (unsigned int) -a;

a = (int)((negateA >> 31) | ((unsigned int) a) >> 31));

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

```
<script type="text/javascript">
var a = false ;
result = nocondition(a);
console.log(result);
function nocondition(a){
var b=1;
var c=2;
while(a){
return b;
}
return c;
}
</script>
```

Though above code is written and tested in javascript which works. Similar construct is available in c. So this can be one of several solutions.

```
#include<iostream>
using namespace std;
int main()
{
int a, b, c;
cout<<" Enter a, b, c :- ";
cin>>a>>b>>c;
int result = (a&&0xFFFFFFFF)*b + !(a&&0xFFFFFFFF)*c; //result = a?b:c;
cout<<" Result = "<<result<<endl;
system("PAUSE");
return 0;
}
```

here is my solution that does not use any conditional operands, it just uses a hashtable consisting of 2 values. I am assuming that if a is false, a = 0x0. therefore

```
void* conditionLessComparison(int conditionA, void* resultTrue, void* resultFalse){
int result;
void*[] toReturn = {resultFalse, resultTrue};
return toReturn[conditionA%1];
```

}

basically if a is false, it will be 0x0, 0x0 % 1 = 0, if a is anything else, % 1 will be 1

```
1 #include<stdio.h>
2
3 int implement_ternary_operator(int a,int b,int c)
4
5 {
6
7 return ((!!a)*b + (!a)*c);
8
9 }
10
11 int main()
12
13 {
14
15 int a = 1,b =10,c =20;
16
17 printf(" result = %d\n",implement_ternary_operator(a,b,c));
18
19 a=0;
20
21
22 printf(" result = %d",implement_ternary_operator(a,b,c));
23
24 }
```

```
1 #include<stdio.h>
2
3 int implement_ternary_operator(int a,int b,int c)
4
5 {
6
7 return ((!!a)*b + (!a)*c);
8
9 }
10
11 int main()
12
13 {
14
15 int a = 1,b =10,c =20;
16
17 printf(" result = %d\n",implement_ternary_operator(a,b,c));
18
19 a=0;
20
21
22 printf(" result = %d",implement_ternary_operator(a,b,c));
23
24 }
```

The question clearly says, we shouldn't use Conditional statement.

#include <stdio.h>

int main()

{

int b = 3, c = 4;

int pool_values[2] = {c, b};

int a1 = 0, a2 = 5;

int result = 0;

result = pool_values[(a1 > 0)];

printf("result = %d\n", result);

result = pool_values[(a2 > 0)];

printf("result = %d\n", result);

return 0;

}

And the output will be:

result = 4

result = 3

- Anonymous February 24, 2014