LinkedIn Interview Question

In the first interview: they asked me to implement a pow(base, exp) function. I did a linear solution and they asked me to improve it (time complexity). There's a logN solution for this problem.

Interview Answers

Anonymous

Jan 10, 2014

Complexity of log(n) implemented via ruby:- def pow(a,b) if n == 0 return 1 else half_pow = pow(a, b/2) if b%2 == 0 return half_pow * half_pow else return half_pow * half_pow * a end end

2

Anonymous

Jan 29, 2014

The first check should be: if b == 0; The remaining code is perfect. Thanks!