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[2] = {b, c};
	Type result = pool[ a == 0];

- Anonymous February 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 votes

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

- Anonymous February 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- soori October 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
6
of 8 vote

b*a + c*(1-a)

- Anonymous February 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if a is 2?

- Wade August 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
5
of 7 vote

This is solution for C++:

result = a * b + !a * c;

- roman.potapkin February 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
10
of 10 votes

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

- John smith May 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awsome one.

- Vivek June 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Learn June 29, 2014 | Flag
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?

- Anonymous August 16, 2014 | Flag
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.

- utpal October 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
4
of 4 vote

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

- Puja February 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- JeffD February 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
4
of 6 vote

how about this:

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

- uuuouou February 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi,

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

- HakunaMatata April 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- karuthedam July 01, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- The Internet August 17, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- The Internet August 17, 2014 | Flag
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 !!!

- Santa March 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- The Internet August 17, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- The Internet August 17, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume b and c are integers:

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

result = array[a];

- Michael.J.Keating February 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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[2];
int[0] = c;
int[1] = b;

result = array[!a];

- Satish July 02, 2015 | Flag
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)

- nivi1991 February 12, 2014 | Flag Reply
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;

- Puja February 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous February 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- vikash February 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- navam May 06, 2020 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- navam May 06, 2020 | Flag
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[4]; return c; } catch(Exception ex) { return a; } } }} } - Abhishek Kumar February 14, 2014 | Flag Reply
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[4];
return c;
}
catch(Exception ex)
{
return a;
}

}

- Abhishek Kumar February 14, 2014 | Flag Reply
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
}

- Using an array makes things easier February 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wow this is a nice answer.

- aka February 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

soori

- Anonymous April 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- donkey code February 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Forgot to force boolean:

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

- donkey code February 17, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

:)

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

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

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

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

- Anonymous February 23, 2014 | Flag Reply
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;

- Anonymous February 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

switch is condition operator

- roman.potapkin February 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

switch is a conditional statement not condition operator

- v.krishna1708 April 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous January 06, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- dreamer February 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- sai krishna June 07, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

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

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.

- dilip.kumar2k6 April 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int a

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

- Anonymous April 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

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

- Faiz Halde April 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- The Internet August 17, 2014 | Flag
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
.

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

Assuming all functions return

uint8_t

, how about:

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

- DVS June 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- DVS June 19, 2014 | Flag
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;
}

- kevytosh July 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- VinothKumar July 15, 2014 | Flag Reply
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[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

- Amit S July 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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


is it right...????

- kamal August 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

question says "without using conditional statements"..

- Hari October 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous August 21, 2014 | Flag Reply
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));

- Toseef September 01, 2014 | Flag Reply
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);

- naveedsm September 16, 2014 | Flag Reply
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
}

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

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

- Skor November 05, 2014 | Flag Reply
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.

- Bhishma January 22, 2015 | Flag Reply
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;
}

- xmatrix February 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Saikat Guha April 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- linliz May 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- linliz May 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- competitivecoder June 05, 2015 | Flag Reply
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;

    unsigned result = (mask & b) + (~mask & c);

    return result;
}

- NB June 19, 2015 | Flag Reply
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;
}

- skum June 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- skum June 23, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- skum June 23, 2015 | Flag
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};

	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

- Ethan July 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a & b || c

- candy August 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a & b || c

- candy August 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- voc_faps September 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- voc_faps September 16, 2015 | Flag Reply
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;
}

- Anonymous November 11, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

This should work.

- murali.bheemineni July 18, 2017 | Flag Reply
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 }

- sandeepkumar211221 August 16, 2017 | Flag Reply
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 }

- sandeepkumar211221 August 16, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- dark_nite77 November 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

- Anonymous February 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Isn't these are conditional statement?

- guruNotCoding February 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey you are using if and else which are conditional statements

- Abhishek Kumar February 14, 2014 | Flag
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[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

- Venkat March 06, 2014 | Flag Reply
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));

- Anonymous March 09, 2014 | Flag Reply
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));
}

- Anonymous March 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

- LoNa March 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This will return undefined value if a = 0

- Ganesh March 29, 2014 | 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