Google Interview Question

(onsite) Implement multiplication without using the multiplication operator

Interview Answers

Anonymous

Jun 23, 2017

At first I struggled to jump into an optimized solution but seeing that I was stumbling the interviewer suggested that I start with a basic implementation (using a for loop and addition). From here I was asked about the performance of this approach. The interviewer then lead me to work on a faster approach which in the 45 minutes I was unable to code a working example.

Anonymous

Apr 25, 2018

Divide by the inverse? Or use some bitwise operators. 5*5 ~= 5/(1/5). (obvious inverse...) or some JS, binary representation -> shift by powers of 2, add the rest (or you could compute out the powers of 2 again and keep adding or something): const mult = (a,b) => { let _a = a.toString(2), _b = b.toString(2) console.log(a,b) const npow2 = Math.floor(Math.log2(b)) const delta = b - Math.pow(2,npow2) _a = parseInt(_a+Array(npow2).fill(1).map($ => 0).join(''), 2) Array(delta).fill(1).map($ => _a+=a) console.log({_a, _b, npow2, delta}) } mult(5,17)