NVIDIA Interview Question

given 2 unsigned ints a and b, return 1 unsigned int = a/b, rounded to nearest int without float operation

Interview Answers

Anonymous

Jul 4, 2013

the round up operation is not useful to find the"nearest int" you could use: ((a%b)>=(b-(a%b)))?(a/b)+1:(a/b)

4

Anonymous

Feb 21, 2015

return (int) ( ( a + (b>>1) ) / b ) ;

1

Anonymous

Jan 28, 2015

You can derive the answer: in order to round up, you want REMINDER >= b/2 let's play: 2*REMINDER >= b What is REMINDER??? 2*(a%b) >= b So, if TRUE: answer is (a%b)+1 if FALSE, the answer is a%b

Anonymous

Nov 21, 2012

return (a + b - 1) / b (round up operation)

1