NVIDIA Interview Question for Software Engineer / Developers


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)

- chris October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

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

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

- trickster November 22, 2012 | Flag
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.

- eugene.yarovoi November 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- trickster November 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Jerr November 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- eugene.yarovoi November 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sry yeah your right. Dunno what I was thinking

- Jerr November 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@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

- ram January 13, 2013 | Flag
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.

- eugene.yarovoi January 13, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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?

- stefanos February 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- eugene.yarovoi February 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- gupta September 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

(Y+(X-1))/X

- mahesh September 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

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

- Harsha October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- harsha October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- finisher.sos October 25, 2012 | Flag
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

- Anonymous October 25, 2012 | Flag
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.

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

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.

- dk June 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- trickster November 22, 2012 | Flag Reply
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

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

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

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

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

- Anonymous June 21, 2016 | Flag Reply
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.

- Kaushik February 08, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

y/x + y/x * x/y

- nixfish October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- rahulbmv October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- nixfish October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Hm October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

And y/x is incorrect.

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

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.

- Anonymous October 25, 2012 | Flag
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

- kalyani October 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

(y/x)+1

- kalyani October 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

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

- Karthik Vvs October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- eugene.yarovoi October 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

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

- Anonymous November 07, 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