Amazon Interview Question

How to divide a number?

Interview Answers

Anonymous

Feb 21, 2013

I guess the answer he expected was the standard textbook one. I reduced the problem to a search problem and gave him a O(n) answer and refined it to a O(log(n)) answer like a binary search algorithm.

Anonymous

Jun 14, 2014

int divide(int a, int b) { int ris = 0; while a >= b { a -= b; ris++; } return ris; } complexity floor(a/b). The worst case b=1 O(a) = O(n) But the O(nlogn) solution is by shifting bits.