## NVIDIA Interview Question for Software Engineer Interns

Country: United States

Comment hidden because of low score. Click to expand.
10
of 12 vote

``````Type pool = {b, c};
Type result = pool[ a == 0];``````

Comment hidden because of low score. Click to expand.
-2

int pool = {b, c};
int index = ((uint32)0x80000000 & ((uint32)(b-c))) >> 31;
int result = pool[index];

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

I can see that all the proposed solutions using conditional check internally. The programmer may not use it but but the compiler does unless we have a separate hardware so this operation differently. Please correct me in case i am wrong.

Comment hidden because of low score. Click to expand.
6
of 8 vote

b*a + c*(1-a)

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

What if a is 2?

Comment hidden because of low score. Click to expand.
5
of 7 vote

This is solution for C++:

``result = a * b + !a * c;``

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

actually its result = !!a * b + !a * c;

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

Awsome one.

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

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

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

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?

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

a may not be 1, a can be any number except 0. then your result will be failed.

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

int x = a&1;
int res = x*b + (1-x)*c;

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

nitpick: it shouldn't be a&1 but a != 0 // hopefully this is 1 in every relevant language. a might be 2, eg.

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

``a && (result = b, 1) || (result = c, 0)``

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

Hi,

If its not much of a trouble. Can you please explain the logic behind this ?

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

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.

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

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.

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

s/I am not it is perfectly complete/I am not sure it is perfectly complete/m

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

result = ((!(!a)) * b) + ((!a) * c);
since a can be any non-zero value !!!

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

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.

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

Err ... strike the part about the integer-valued expression. It has nothing to do with being integer-valued, nor should it.

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

Assume b and c are integers:

``````int[] array = new int;
int = b;
int = c;

result = array[a];``````

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

in C get false only if a is 0. True for +ive and -ive a's so my correction to this (based on the trick used in an answer above_
int[] array = new int;
int = c;
int = b;

result = array[!a];

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

Find a way to turn non-zero value of a into an all-ones bit pattern using only bitwise operators.

a |= a >> 1; a |= a >> 2; a |= a >> 4; a |= a >> 8; a |= a >> 16;
a |= a << 1; a |= a << 2; a |= a << 4; a |= a << 8; a |= a << 16;

result = (a & b) | (~a & c)

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

above code works for binary values of a..here is more general solution
int t= (a-1)<0;
int result = (1-t)*b + t*c;

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

result = b+c - a*c + (a-1)*b

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

result= a*b + (1-a)*c
Simple !!!!!!!

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

int main()
{
int v,result;
int a,b,c;
scanf("%d",&a);
v=if(a)
result=v*b+(1-v)*c;
printf("%d",result);
}

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

int v,result;
int a,b,c;
scanf("%d",&a);
v=if(a)
result=v*b+(1-v)*c;
printf("%d",result);

Comment hidden because of low score. Click to expand.
0
of 0 vote
In case of java where boolean false does not mean 0 and true doesnot mean any number Where true mean true and false mean false.Following code can be implemented {{{ static int assign(Boolean b,int a ,int c) { String str=b.toString(); char[] x=str.toCharArray(); try { char j=x; return c; } catch(Exception ex) { return a; } } }} }
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int assign(Boolean b,int a ,int c)
{
String str=b.toString();
char[] x=str.toCharArray();
try
{
char j=x;
return c;
}
catch(Exception ex)
{
return a;
}

}

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

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

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

wow this is a nice answer.

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

Hey please tell me how compiler does !!a without conditional operation

soori

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

~(a - 1) & b | (a - 1) & c

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

Forgot to force boolean:

~(!!a - 1) & b | (!!a - 1) & c

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

:)

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

unsigned int r = c^((b^c) & -(a));

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

``res = b*(!!a) + c*(!a);``

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

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

switch(a)
case a:
return b;

default:
return c;

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

switch is condition operator

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

switch is a conditional statement not condition operator

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

Hey Dude!!!!
switch is also conditional statement.

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

d = !a;
result = ((!d)*(b)) + ((d)*(c));

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

if a is true i.e 1 then it returns b else it returns c

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

result = (!!a)*b + (1-!!a)*c;

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

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.

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

int a

return b=a||1;
return c=a&&0;

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

while(a) {
result = b;
return;
}
result = c
is it correct?

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

It is correct if you do not view the loop condition of while as a conditional statement. However, then a lot of the other suggested solutions, such as the ones based on boolean expressions, may also be incorrect.

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

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
.

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

Assuming all functions return

``uint8_t``

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

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

Small change to the solution.

``a()``

returns 0 or 1.

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

``/*--------------------------------------------------*/``

Explanation:

Let

``switch = a ^ 0;``

So,

``result = result & [(switch & b) | (!switch & c)]``

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

int max(int a, int b, int c)
{
int m = a;
(m < b) && (m = b); //these are not conditional statements.
(m < c) && (m = c); //these are just boolean expressions.
return m;
}

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

result=((a && 1)*(b-c))+c

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

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

``````a = a*a+1;
int *array = new int[a];
array[a-1] = b;
array = 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 = c overrides b in array[a-1] = b

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

if(a){
return b;
}
else{
return c;
}

is it right...????

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

question says "without using conditional statements"..

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

result = (a!=0)*b + (a==0)*c;

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

If result, a, b and c are (, or can be broken into ) integral type then the simple solution using bitwise operator would be

result = (b & ~((!!a)-1)) | (c & ((!!a)-1));

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

/* 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);

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

since we need a alternative to element1==element2 , then
hashcode(elemet1) - hascode(element2) !=0{
//logic
}

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

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

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

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

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

Use xor to flip the bit

``````unsigned int ternary(unsigned int a, unsigned int b, unsigned int c)
{
unsigned int q = 1 ^ a; //flip the bit
return a * b + q * c;
}``````

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

Well in JavaScript it would be
x = (a && b) || c ;

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

output = xnor(result , a)*b + xor(result,a)*c

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

``output = xnor(result,a)*b + xor(result,b) * c``

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

result=b*(a^0)+c*(a^1)

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

``````unsigned compute(int a, int b, int c)
{
int mask = ~(int)(bool(a)) + 1;

return result;
}``````

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

``````#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;
}``````

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

correction for above code :-
result = (a&&1)*b + !(a&&1)*c; //result = a?b:c;

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

correction for above code :-
int result = (a&&1)*b + !(a&&1)*c; //result = a?b:c;

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

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

}

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

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

a & b || c

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

a & b || c

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

r=(((a*b)|(!a*c))/(a|!a));

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

``r=(((a*b)|(!a*c))/(a|!a));``

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

what is a, b, c are expressions?

while(a) {
b;
break
}

while(!a) {
c;
break;
}

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

result = b;
(a * c) && (result = c);
return result;

This should work.

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

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

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

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

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

for c++
result = bool(a-b);

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

``````if (a == true)
result = b;
else
result = c;``````

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

Isn't these are conditional statement?

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

Hey you are using if and else which are conditional statements

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

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

#include <stdio.h>
int main()
{
int b = 3, c = 4;
int pool_values = {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

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

little addition to roman's code above
int iDecide(int a,int b,int c)
{
a=!a;
a=!a;
return ((a*b) + (!a *c));

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

int iDecide(int a,int b,int c)
{
//Following to make sure the multiplication factor is made to 1 or 0
a=!a;
a=!a;
return ((a*b) + (!a *c));
}

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

result = (a * b) + ((!a) * c);
result = result /a;

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

This will return undefined value if a = 0

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.