## NVIDIA Interview Question for Software Engineer / Developers

• 2

Country: United States
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
9
of 11 vote

Basically you need: ceiling(y / x) iterations, that is:
- you need int(y / x) iterations which copy exactly x bytes
- if remaining bytes != 0 (i.e.: y % x) != 0) then you need another iteration to transfer remaining bytes

The formula rewritten to use only x - / * (drumroll...):
(y + (x-1)) / x

In order to prove correctness you need to check two case sets:
1. y % x == 0 => addition of x-1 doesn't affect the result, so it is y/x (which is ok)
2. y % x > 0 => addition of x-1 increments result with 1 (which is ok because we need another copy for the remaining y % x bytes)

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

Good answer. Worth noting that we assume that x + y does not overflow in this solution.

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

Arghh, just realized that my answer is the same:
(y + (x-1)) / x == (y -1 + x) / x == (y-1)/x + x/x == 1 + (y-1)/ x

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

@Trickster: yours is incorrect for y = 0. However, aside from that case, I actually prefer your solution because it's safer against overflows.

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

yup. I mentioned that down below. It only works for y > 0 .

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

Not exactly. This fails on y=2 and x=1

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

@jerr: why? it gives an answer of 2.

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

Sry yeah your right. Dunno what I was thinking

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

@eugene.yarovoi

1. (y + (x-1)) / x
2. 1 + (y-1)/ x
why do you fell 1st solution is not safer against overflows??

int main()
{
unsigned char result = 0;
unsigned char y = 255;
unsigned char x = 2;

result = (y+x-1)/x;

printf("%d",result);

return 0;
}

the "result" value in the above code will be 128, even though y+x -1 overflows i.e 256.
i think both the solutions are safe against overflows depending on data type of "result".
correct me if am wrong

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

That's because x and y get promoted to int when doing the addition. The same idea would not save integers from overflowing.

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

To clarify, do we want the result of this expression to be the exact integer? Let's say y=11, x=3, then (y + (x-1)) / x = 13/3. Perhaps we are assuming integer primitives in which case result is truncated?

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

@stefanos: yes, I think truncating division is the most plausible interpretation of / in the context of this question.

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

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

int main()
{
int x=111;
int y=11;
(x/(y*1.0))==x/y?printf("%d\n",x/y):printf("%d\n",1+(x/y));
return 0;
}``````

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

(Y+(X-1))/X

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

y/x + (y-y/x*x)/(y-y/x*x)-1

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

y/x + (y-y/x*x)/((y-y/x*x)-1)
sorry forgot braces.

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

y - (y/x * x) are the remaining bytes i.e. 0 <= remaining bytes < x.
if the remaining bytes is 0, then y/x + 0 else y/x + 1

1 is acheived by (remaining bytes)/(remaining bytes - 1)

so answer is Y/X + ((Y - (Y/X*X))/(Y-(Y/X*X) - 1)

let me know if I am doing something wrong here.

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

Not sure if this is correct, but even if it is, this solution is just so needlessly complex in comparison to the top-voted answer

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

It's not even correct actually, because Y-(Y/X*X) can be 1, and then doing (Y-(Y/X*X))/( Y-(Y/X*X) - 1) could result in a division by 0.

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

Also, would not work when remaining bytes

``Y-(Y/X*X)``

is 2.

``(Y-(Y/X*X))/( Y-(Y/X*X) - 1)  = 2/1``

which is not we need.

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

Assuming y > 0:
1 + (y -1)/x

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

y / x + (remainder + x - 1) / x
where remainder of y / x is y - (y / x) * x

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

y /x + (y - y/x*x + x -1)/x

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

(y / x) + !!(y - (y / x) * x);

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

return (y-((y/x)*x) == 0 ? y/x : (y/x)+1);

explanation: If ( y – (y/x) *x) is greater that means some more bytes are still left. So we need to send (y/x)+1.

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

y/x + y/x * x/y

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

if y = 12 and x = 10....your solution gives - 1 + 1 * 0 which doesnt seem correct...number of iterations should be 2 for this example.

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

y/x + 1 - y/x * x/y

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

That's just y/x + 1 - 1 so why no just say y/x?

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

And y/x is incorrect.

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

Also, y/x + 1 - y/x * x/y != y/x, since these are truncating divisions. In fact, unless y = x, one of x/y and y/x is 0, giving y/x +1 as the answer. But if x = y this expression gives y/x.

This is still incorrect, as the answer should be y/x + (x divides y) ? 0 : 1, not y/x + (x == y) ? 0 : 1, like this expression would have it.

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

y/x +1 why is this incorrect? v do get answer from this right? :o

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

(y/x)+1

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

y/x+x/x(which is ofcourse y/x+1)

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

Not quite. If y is divisible by x, the answer should be just y/x.

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

(Y+(X-(Y- (Y/X)*X))/X

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.