Frage im Vorstellungsgespräch bei Palantir Technologies

write a function that finds the remainder between two numbers using only addition, sutraction and multiplication.

Antworten zu Vorstellungsgespräch

Anonym

5. Juli 2012

This is pretty easy, guys. You don't want to be doing extraneous multiplication because it is veryyyy slowwwww. X and Y are the numbers to find the remainder of, assume X > Y. while(x > y) { x = x - y } return x Example: 10, 4 10 > 4 x = 10 - 4 = 6 6 > 4 x = 6 - 4 = 2 2 < 4 return 2

11

Anonym

10. Okt. 2012

but if u do a binary search while multiplying then it will be faster then the subtraction method.

Anonym

2. Juli 2012

the first solution is wrong..(1/y) is division dude.. I think the solution is something like this. Suppose x, y are your numbers and you want to find remainder(y/x). z = x while z < y i++ z = x*i end remainder = y - (z*(i-1))

Anonym

17. Apr. 2012

Oops, forgot to include parseInt()

Anonym

17. Apr. 2012

var x; var y; var solution; var TestSol; var Remainder; x * (1/y) = solution; solution*y = TestSol; if (TestSol = x) { document.write("Remainder is zero"); } else { x - TestAns = Remainder; document.write (Remainder); }

1